All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
Any action that leads to releasing sperm in pleasure without having a contact with one's wife or one's slave woman is forbidden in the Sharee’ah.
So, all kinds of masturbation or other illegal means that lead to releasing sperm are forbidden.
Allah Says (what means): {And they who guard their private parts. Except from their wives or those their right hands possess, for indeed, they will not be blamed - But whoever seeks beyond that, then those are the transgressors -} [Quran 23:5-7]
For more benefit on the prohibition of masturbation, please refer to Fataawa 179859, 376036, 87952, and 81167.
Allah knows best.