ADI and SDI are well-established high-contrast imaging techniques, but their application is challenging for companions at small angular separations. The aim of this paper is to investigate to what extent adaptive-optics assisted, medium-resolution (R$sim$5000) integral field spectrographs (IFS) can be used to directly detect the absorption of molecular species in the spectra of planets and substellar companions when these are not present in the spectrum of the star. We analyzed archival data of $beta$ Pictoris taken with the SINFONI integral field spectrograph (VLT), originally taken to image $beta$ Pic b using ADI techniques. At each spatial position in the field, a scaled instance of the stellar spectrum is subtracted from the data after which the residuals are cross-correlated with model spectra. The cross-correlation co-adds the individual absorption lines of the planet emission spectrum constructively, but not residual telluric and stellar features. Cross-correlation with CO and H$_2$O models results in significant detections of $beta$ Pic b at SNRs of 14.5 and 17.0 respectively. Correlation with a 1700K BT-Settl model provides a signal with an SNR of 25.0. This contrasts with ADI, which barely reveals the planet. While the AO system only achieved modest Strehl ratios of 19-27% leading to a raw contrast of 1:240 at the planet position, cross-correlation achieves a 3$sigma$ contrast limit of $2.5times10^{-5}$ in this 2.5h data set $0.36$ away from the star. AO-assisted, medium-resolution IFS such as SINFONI (VLT) and OSIRIS (Keck), can be used for high-contrast imaging utilizing cross-correlation techniques for planets that are close to their star and embedded in speckle noise. We refer to this method as molecule mapping, and advocate its application to observations with future medium resolution instruments, in particular ERIS (VLT), HARMONI (ELT) and NIRSpec and MIRI (JWST).