كتاب الإمارة
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 b

It has been narrated (through a different chain of tmnamitters) on the authority of Umm Salama (wife of the Holy Prophet) that he said:

Amirs will be appointed over you, and you will find them doing good as well as bad deeds. One who hates their bad deeds is absolved from blame. One who disapproves of their bad deeds is (also) safe (so far as Divine wrath is concerned). But one who approves of their bad deeds and imitates them (is doomed). People asked: Messenger of Allah, shouldn't we fight against them? He replied: No, as long as they say their prayer. (" Hating and disapproving" refers to liking and disliking from the heart.)
وَحَدَّثَنِي أَبُو غَسَّانَ الْمِسْمَعِيُّ، وَمُحَمَّدُ بْنُ بَشَّارٍ، جَمِيعًا عَنْ مُعَاذٍ، - وَاللَّفْظُ لأَبِي غَسَّانَ - حَدَّثَنَا مُعَاذٌ، - وَهُوَ ابْنُ هِشَامٍ الدَّسْتَوَائِيُّ - حَدَّثَنِي أَبِي، عَنْ قَتَادَةَ، حَدَّثَنَا الْحَسَنُ، عَنْ ضَبَّةَ بْنِ مِحْصَنٍ الْعَنَزِيِّ، عَنْ أُمِّ سَلَمَةَ، زَوْجِ النَّبِيِّ صلى الله عليه وسلم عَنِ النَّبِيِّ صلى الله عليه وسلم أَنَّهُ قَالَ ‏"‏ إِنَّهُ يُسْتَعْمَلُ عَلَيْكُمْ أُمَرَاءُ فَتَعْرِفُونَ وَتُنْكِرُونَ فَمَنْ كَرِهَ فَقَدْ بَرِئَ وَمَنْ أَنْكَرَ فَقَدْ سَلِمَ وَلَكِنْ مَنْ رَضِيَ وَتَابَعَ ‏"‏ ‏.‏ قَالُوا يَا رَسُولَ اللَّهِ أَلاَ نُقَاتِلُهُمْ قَالَ ‏"‏ لاَ مَا صَلَّوْا ‏"‏ ‏.‏ أَىْ مَنْ كَرِهَ بِقَلْبِهِ وَأَنْكَرَ بِقَلْبِهِ ‏.‏
Reference : Sahih Muslim 1854b
In-book reference : Book 33, Hadith 98
USC-MSA web (English) reference : Book 20, Hadith 4570
  (deprecated numbering scheme)