كتاب الْمَسَاجِدِ وَمَوَاضِعِ الصَّلاَةِ
5  
The Book of Mosques and Places of Prayer
(8)
Chapter: The permissibility of cursing the Shaitan during prayer, and seeking refuge with Allah from him; and the permissibility of doing a few actions while in salat
(8)
باب جَوَازِ لَعْنِ الشَّيْطَانِ فِي أَثْنَاءِ الصَّلاَةِ وَالتَّعَوُّذِ مِنْهُ وَجَوَازِ الْعَمَلِ الْقَلِيلِ فِي الصَّلاَةِ
Sahih Muslim 542

Abu Darda' reported:

Allah's Messenger (ﷺ) stood up (to pray) and we heard him say:" I seek refuge in Allah from thee." Then said:" curse thee with Allah's curse" three times, then he stretched out his hand as though he was taking hold of something. When he finished the prayer, we said: Messenger of Allah, we heard you say something during the prayer which we have not heard you say before, and we saw you stretch out your hand. He replied: Allah's enemy Iblis came with a flame of fire to put it in my face, so I said three times:" I Seek refuge in Allah from thee." Then I said three times:" I curse thee with Allah's full curse." But he did not retreat (on any one of these) three occasions. Thereafter I meant to seize him. I swear by Allah that had it not been for the supplication of my brother Sulaiman he would have been bound, and made an object of sport for the children of Medina.
حَدَّثَنَا مُحَمَّدُ بْنُ سَلَمَةَ الْمُرَادِيُّ، حَدَّثَنَا عَبْدُ اللَّهِ بْنُ وَهْبٍ، عَنْ مُعَاوِيَةَ بْنِ صَالِحٍ، يَقُولُ حَدَّثَنِي رَبِيعَةُ بْنُ يَزِيدَ، عَنْ أَبِي إِدْرِيسَ الْخَوْلاَنِيِّ، عَنْ أَبِي الدَّرْدَاءِ، قَالَ قَامَ رَسُولُ اللَّهِ صلى الله عليه وسلم فَسَمِعْنَاهُ يَقُولُ ‏"‏ أَعُوذُ بِاللَّهِ مِنْكَ ‏"‏ ‏.‏ ثُمَّ قَالَ ‏"‏ أَلْعَنُكَ بِلَعْنَةِ اللَّهِ ‏"‏ ‏.‏ ثَلاَثًا ‏.‏ وَبَسَطَ يَدَهُ كَأَنَّهُ يَتَنَاوَلُ شَيْئًا فَلَمَّا فَرَغَ مِنَ الصَّلاَةِ قُلْنَا يَا رَسُولَ اللَّهِ قَدْ سَمِعْنَاكَ تَقُولُ فِي الصَّلاَةِ شَيْئًا لَمْ نَسْمَعْكَ تَقُولُهُ قَبْلَ ذَلِكَ وَرَأَيْنَاكَ بَسَطْتَ يَدَكَ ‏.‏ قَالَ ‏"‏ إِنَّ عَدُوَّ اللَّهِ إِبْلِيسَ جَاءَ بِشِهَابٍ مِنْ نَارٍ لِيَجْعَلَهُ فِي وَجْهِي فَقُلْتُ أَعُوذُ بِاللَّهِ مِنْكَ ‏.‏ ثَلاَثَ مَرَّاتٍ ثُمَّ قُلْتُ أَلْعَنُكَ بِلَعْنَةِ اللَّهِ التَّامَّةِ فَلَمْ يَسْتَأْخِرْ ثَلاَثَ مَرَّاتٍ ثُمَّ أَرَدْتُ أَخْذَهُ وَاللَّهِ لَوْلاَ دَعْوَةُ أَخِينَا سُلَيْمَانَ لأَصْبَحَ مُوثَقًا يَلْعَبُ بِهِ وِلْدَانُ أَهْلِ الْمَدِينَةِ ‏"‏ ‏.‏
Reference : Sahih Muslim 542
In-book reference : Book 5, Hadith 51
USC-MSA web (English) reference : Book 4, Hadith 1106
  (deprecated numbering scheme)