Answer
(Fatwa: 1585/L=579/TL=1433)
Masturbation is haram and big sin. If one masturbates in the state of fast his fast shall break. However, in this case only qaza is wajib not kaffarah i.e. you will not have to keep fast of sixty days constantly. You shall be sinful if you masturbate though three or four times a month, you must give up this bad habit permanently. The best way to give it up is to do nikah if you can, but if it is not possible then fast extensively and avoid seeing nude pictures and porn films etc. Also, instead of having bad thoughts at the time of sleeping go to bed reading durood. If you have bad thoughts again then think about the warnings of Allah. Follow the above mentioned instructions, in-shaAllah you shall get rid of this bad practice.
Allah (Subhana Wa Ta’ala) knows Best
Darul Ifta,
Darul Uloom Deoband