How can someone become liberated from shameful thoughts? Let us follow what St Basil has to say Concerning this topic:
We should confront these attacks with intensive care and attentiveness, just as an athlete when he evades his opponent’s blows with the accurate precaution necessary, together with the flexibility of his body. We should entrust the ending of the war and the avoidance of the arrows, to prayer and assistance from above.
And even if the tricky enemy, during the hour of prayer, subjects us to cunning fantasies, the soul should not interrupt its prayer. The soul should also know that it is not responsible for the cunning attacks undertaken by the enemy, in addition to the fantasies emanating from the “paradoxical miracle maker”. On the contrary, he should think of the fact that these thoughts are due to the impertinence of the inventor of evil. That person should then intensify his kneeling to The Lord and should plead to God to dissolve the cunning partition caused by irrational and absurd thoughts, so that, unhindered, he can approach God.
If, however, the harmful attack of the thought becomes more intense due to the impudence of the enemy, we should not turn to cowardice nor quit the battle in its duration, but instead, we should endure up to the point when God will notice our perseverance. He will then enlighten us with the Grace of The Holy Spirit, which will on one hand cause the enemy to flee, and on the other hand flood our mind with Holy Light, in order for the thought to adore God with uninterrupted tranquility and joy”.