The Messenger of Allah ﷺ said, "These privies are frequented by devils, so when one of you enters the toilet, let him say, 'I seek refuge in Allah from male and female evil spirits.'"
حدثنا عمرو بن مرزوق أخبرنا شعبة عن قتادة عن النضر بن أنس
عن رسول الله ﷺ قال إن هذه الحشوش محتضرة فإذا أتى أحدكم الخلاء فليقل أعوذ بالله من الخبث والخبائث
[Its isnad is hasan]. Ibn Majah narrated it in the Book of Purification, the chapter on what a man says when entering the toilet, hadith 296, from the hadith of Shubah with this wording. Ibn Khuzayma graded it sahih, hadith 69, as did Ibn Hibban (in al-Ihsan), hadith 1405, and Hakim, 1/187, and Dhahabi agreed with him.