كتاب الإمارة
33  
The Book on Government
(16)
Chapter: The obligation to denounce rulers for that in which they go against Shari'ah, but they should not be fought so long as they pray regularly, etc
(16)
باب وُجُوبِ الإِنْكَارِ عَلَى الأُمَرَاءِ فِيمَا يُخَالِفُ الشَّرْعَ وَتَرْكِ قِتَالِهِمْ مَا صَلَّوْا وَنَحْوِ ذَلِكَ
Sahih Muslim 1854 a

It has been narrated on the authority of Umm Salama that the Messenger of Allah (ﷺ) said:

In the near future there will be Amirs and you will like their good deeds and dislike their bad deeds. One who sees through their bad deeds (and tries to prevent their repetition by his band or through his speech), is absolved from blame, but one who hates their bad deeds (in the heart of his heart, being unable to prevent their recurrence by his hand or his tongue), is (also) fafe ( so far as God's wrath is concerned). But one who approves of their bad deeds and imitates them is spiritually ruined. People asked (the Holy Prophet): Shouldn't we fight against them? He replied: No, as long as they say their prayers.
حَدَّثَنَا هَدَّابُ بْنُ خَالِدٍ الأَزْدِيُّ، حَدَّثَنَا هَمَّامُ بْنُ يَحْيَى، حَدَّثَنَا قَتَادَةُ، عَنِ الْحَسَنِ، عَنْ ضَبَّةَ بْنِ مِحْصَنٍ، عَنْ أُمِّ سَلَمَةَ، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم قَالَ ‏"‏ سَتَكُونُ أُمَرَاءُ فَتَعْرِفُونَ وَتُنْكِرُونَ فَمَنْ عَرَفَ بَرِئَ وَمَنْ أَنْكَرَ سَلِمَ وَلَكِنْ مَنْ رَضِيَ وَتَابَعَ ‏"‏ ‏.‏ قَالُوا أَفَلاَ نُقَاتِلُهُمْ قَالَ ‏"‏ لاَ مَا صَلَّوْا ‏"‏ ‏.‏
Reference : Sahih Muslim 1854a
In-book reference : Book 33, Hadith 97
USC-MSA web (English) reference : Book 20, Hadith 4569
  (deprecated numbering scheme)