The recent development of all-electrical electron spin resonance (ESR) in a scanning tunneling microscope (STM) setup has opened the door to vast applications. Despite the fast growing number of experimental works on STM-ESR, the fundamental principles remains unclear. By using a cotunneling picture, we show that the spin resonance signal can be explained as a time-dependent variation of the tunnel barrier induced by the alternating electric driving field. We demonstrate how this variation translates into the resonant frequency response of the direct current. Our cotunneling theory explains the main experimental findings. Namely, the linear dependence of the Rabi flop rate with the alternating bias amplitude, the absence of resonant response for spin-unpolarized currents, and the weak dependence on the actual atomic species.