كتاب الْمَسَاجِدِ وَمَوَاضِعِ الصَّلاَةِ
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 541 a

Abu Huraira reported that he heard the Messenger of Allah (ﷺ) saying:

A highly wicked one amongst the Jinn escaped yesternight to interrupt my prayer, but Allah gave me power over him, so I seized him and intended to tie him to one of the pillars of the mosque in order that you, all together or all, might look at him, but I remembered the supplication of my brother Sulaiman:" My Lord, forgive me, give me such a kingdom as will not be possible for anyone after me" (Qur'an, xxxvii. 35).
حَدَّثَنَا إِسْحَاقُ بْنُ إِبْرَاهِيمَ، وَإِسْحَاقُ بْنُ مَنْصُورٍ، قَالاَ أَخْبَرَنَا النَّضْرُ بْنُ شُمَيْلٍ، أَخْبَرَنَا شُعْبَةُ، حَدَّثَنَا مُحَمَّدٌ، - وَهُوَ ابْنُ زِيَادٍ - قَالَ سَمِعْتُ أَبَا هُرَيْرَةَ، يَقُولُ قَالَ رَسُولُ اللَّهِ صلى الله عليه وسلم ‏ "‏ إِنَّ عِفْرِيتًا مِنَ الْجِنِّ جَعَلَ يَفْتِكُ عَلَىَّ الْبَارِحَةَ لِيَقْطَعَ عَلَىَّ الصَّلاَةَ وَإِنَّ اللَّهَ أَمْكَنَنِي مِنْهُ فَذَعَتُّهُ فَلَقَدْ هَمَمْتُ أَنْ أَرْبِطَهُ إِلَى جَنْبِ سَارِيَةٍ مِنْ سَوَارِي الْمَسْجِدِ حَتَّى تُصْبِحُوا تَنْظُرُونَ إِلَيْهِ أَجْمَعُونَ - أَوْ كُلُّكُمْ - ثُمَّ ذَكَرْتُ قَوْلَ أَخِي سُلَيْمَانَ رَبِّ اغْفِرْ لِي وَهَبْ لِي مُلْكًا لاَ يَنْبَغِي لأَحَدٍ مِنْ بَعْدِي ‏.‏ فَرَدَّهُ اللَّهُ خَاسِئًا ‏"‏ ‏.‏ وَقَالَ ابْنُ مَنْصُورٍ شُعْبَةُ عَنْ مُحَمَّدِ بْنِ زِيَادٍ ‏.‏
Reference : Sahih Muslim 541a
In-book reference : Book 5, Hadith 49
USC-MSA web (English) reference : Book 4, Hadith 1104
  (deprecated numbering scheme)