Windows SMB Ghost£¨CVE-2020-0796£©Îó²îÆÊÎö

Ðû²¼Ê±¼ä 2020-04-09

Îó²îÏÈÈÝ


2020Äê3ÔÂ10ÈÕ£¬Î¢ÈíÔÚÆä¹Ù·½SRCÐû²¼ÁËCVE-2020-0796µÄÇ徲ͨ¸æ£¨ADV200005£¬Microsoft Guidance for Disabling SMBv3 Compression£©,ͨ¸æÌåÏÖÔÚWindows SMBv3°æ±¾µÄ¿Í»§¶ËºÍ·þÎñ¶Ë±£´æÔ¶³Ì´úÂëÖ´ÐÐÎó²î¡£Í¬Ê±Ö¸³ö¸ÃÎó²î±£´æÓÚMicroSoft Server Message Block 3.1.1ЭÒé´¦Öóͷ£Ìض¨ÇëÇó°üµÄ¹¦Ð§ÖУ¬¹¥»÷ÕßʹÓøÃÎó²î¿ÉÔÚÄ¿µÄSMB Server»òÕßClientÖÐÖ´ÐÐí§Òâ´úÂë¡£


ÓÅ·¢¹ú¼ÊÍøÕ¾¹ÙÍøADLabÇå¾²Ñо¿Ö°Ô±ÔÚ¶Ô¸ÃÎó²î¾ÙÐÐÑо¿µÄÀú³ÌÖз¢Ã÷ÏÖÔÚÈö²¥µÄһЩÎó²îÆÊÎö±£´æijЩÎÊÌ⣬Òò´Ë¶Ô¸ÃÎó²î¾ÙÐÐÁËÉîÈëµÄÆÊÎö£¬²¢ÔÚWindows 10ϵͳÉϾÙÐÐÁ˸´ÏÖ¡£



Îó²î¸´ÏÖ


½ÓÄÉWindows 10 1903°æ±¾¾ÙÐи´ÏÖ¡£ÔÚÎó²îʹÓúó£¬ÑéÖ¤³ÌÐòÌáȨ¿¢Êºó½¨ÉèÁËÒ»¸ösystemȨÏÞµÄcmd shell£¬Èçͼ1Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ1 CVE-2020-0796ÍâµØÌáȨ


Îó²î»ùÀ´Ô´Àí


CVE-2020-0796Îó²î±£´æÓÚÊÜÓ°Ïì°æ±¾µÄWindowsÇý¶¯srv2.sysÖС£Windows SMB v3.1.1 °æ±¾ÔöÌíÁ˶ÔѹËõÊý¾ÝµÄÖ§³Ö¡£Í¼2ËùʾΪ´øѹËõÊý¾ÝµÄSMBÊý¾Ý±¨ÎĵÄ×é³É¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ2 ´øѹËõÊý¾ÝµÄSMBÊý¾Ý±¨ÎĽṹ


ƾ֤΢ÈíMS-SMB2ЭÒéÎĵµ£¬SMB Compression Transform HeaderµÄ½á¹¹Èçͼ3Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ3 SMB Compression Transform HeaderÊý¾Ý½á¹¹


ProtocolId£º4×Ö½Ú£¬Àο¿Îª0x424D53FC

OriginalComressedSegmentSize£º4×Ö½Ú£¬Ô­Ê¼µÄδѹËõÊý¾Ý¾Þϸ

CompressionAlgorithm£º2×Ö½Ú£¬Ñ¹ËõËã·¨

Flags £º2×Ö½Ú£¬Ïê¼ûЭÒéÎĵµ

Offset/Length£ºÆ¾Ö¤FlagsµÄȡֵΪOffset»òÕßLength£¬OffsetÌåÏÖÊý¾Ý°üÖÐѹËõÊý¾ÝÏà¹ØÓÚÄ¿½ñ½á¹¹µÄÆ«ÒÆ

srv2.sysÖд¦Öóͷ£SMBv3ѹËõÊý¾Ý°üµÄ½âѹº¯ÊýSrv2DecompressDataδÑÏ¿áУÑéÊý¾Ý°üÖÐOriginalCompressedSegmentSizeºÍOffset/Length×ֶεÄÕýµ±ÐÔ¡£¶øÕâÁ½¸ö×Ö¶ÎÓ°ÏìÁËSrv2DecompressDataÖÐÄÚ´æ·ÖÅɺ¯ÊýSrvNetAllocateBufferµÄ²ÎÊý¡£Èçͼ4ËùʾµÄSrv2DecompressDataº¯Êý·´±àÒë´úÂ룬SrvNetAllocateBufferÏÖʵµÄ²ÎÊýΪOriginalCompressedSegmentSize+Offset¡£ÕâÁ½¸ö²ÎÊý¶¼Ö±½ÓȪԴÓÚÊý¾Ý°üÖÐSMB Compression Transform HeaderÖеÄ×ֶΣ¬¶øº¯Êý²¢Î´ÅжÏÕâÁ½¸ö×Ö¶ÎÊÇ·ñÕýµ±£¬¾ÍÖ±½Ó½«ÆäÏà¼Óºó×÷ΪÄÚ´æ·ÖÅɵIJÎÊý(unsigned intÀàÐÍ£©¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ4 Srv2DecompressDataº¯ÊýµÄÒªº¦´úÂë


ÕâÀOriginalCompressedSegmentSize+Offset¿ÉÄÜСÓÚÏÖʵÐèÒª·ÖÅɵÄÄÚ´æ¾Þϸ£¬´Ó¶øÔÚºóÐøŲÓýâѹº¯ÊýSmbCompressionDecompressÀú³ÌÖб£´æÔ½½ç¶ÁÈ¡»òÕßдÈëµÄΣº¦¡£


ÌáȨʹÓÃÀú³Ì


ÏÖÔÚÒѹûÕæµÄÕë¶Ô¸ÃÎó²îµÄÍâµØÌáȨʹÓðüÀ¨ÈçϵÄÖ÷ÒªÀú³Ì£º

£¨1£©ÑéÖ¤³ÌÐòÊ×ÏȽ¨Éèµ½SMS serverµÄ»á»°ÅþÁ¬£¨¼ÇΪsession£©¡£

£¨2£©ÑéÖ¤³ÌÐò»ñÈ¡×ÔÉítokenÊý¾Ý½á¹¹ÖÐprivilege³ÉÔ±ÔÚÄÚºËÖеĵص㣨¼ÇtokenAddr£©¡£

£¨3£©ÑéÖ¤³ÌÐòͨ¹ýsession·¢ËÍ»ûÐÎѹËõÊý¾Ý£¨¼ÇΪevilData£©¸øSMB server´¥·¢Îó²î¡£ÆäÖУ¬evilData°üÀ¨tokenAddr¡¢È¨ÏÞÊý¾Ý¡¢Òç³öռλÊý¾Ý¡£

£¨4£©SMS serverÊÕµ½evilDataºó´¥·¢Îó²î£¬²¢ÐÞ¸ÄtokenAddrµØµã´¦µÄȨÏÞÊý¾Ý£¬´Ó¶øÌáÉýÑéÖ¤³ÌÐòµÄȨÏÞ¡£

£¨5£©ÑéÖ¤³ÌÐò»ñȡȨÏÞºó¶Ôwinlogon¾ÙÐпØÖÆ£¬À´½¨ÉèsystemÓû§shell¡£


Îó²îÄÚ´æ·ÖÅÉÆÊÎö


Ê×ÏÈ£¬¿´Ò»ÏÂÒѹûÕæʹÓõÄevilDataÊý¾Ý°üµÄÄÚÈÝ£¬Èçͼ5Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ5 ÌáȨpoc·¢Ë͵ĴøѹËõÊý¾ÝµÄSMBÊý¾Ý°ü


Êý¾Ý°üµÄÄÚÈݺܼòÆÓ£¬ÆäÖм¸¸öÒªº¦×Ö¶ÎÊý¾ÝÈçÏ£º

OriginalSize£º0xffffffff

Offset£º0x10

Real compressed data£º13×Ö½ÚµÄѹËõÊý¾Ý£¬½âѹºóӦΪ1108×Ö½Ú¡¯A¡¯¼Ó8×Ö½ÚµÄtokenµØµã¡£

SMB3 raw data£ºÏÖʵÉÏÊÇÓÉ2¸ö8×Ö½ÚµÄ0x1FF2FFFFBC£¨×ܳ¤0x10)¼ÓÉÏ0x13×Ö½ÚµÄѹËõÊý¾Ý×é³É¡£

´ÓÉÏÃæµÄÎó²îÔ­ÀíÆÊÎö¿ÉÖª£¬Îó²î³ÉÒòÊÇSrv2DecompressDataº¯Êý¶Ô±¨ÎÄ×Ö¶Îȱ·¦Õýµ±ÐÔÅжÏÔì³ÉÄÚ´æ·ÖÅɲ»µ±¡£ÔÚ¸ÃÎó²îÊý¾Ý°üÖУ¬OriginalSize ÊÇÒ»¸ö»ûÐÎÖµ¡£OriginalSize + Offset = 0xffffffff + 0x10 = 0xf ÊÇÒ»¸öºÜСµÄÖµ£¬Æ佫»áת´ï¸øSrvNetAllocateBuffer¾ÙÐÐŲÓã¬ÏÂÃæÏêϸÆÊÎöÄÚ´æ·ÖÅÉÇéÐΡ£SrvNetAllocateBufferµÄ·´±àÒë´úÂëÈçͼ6¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ6 SrvNetAllocateBufferÄÚ´æ·ÖÅÉÀú³Ì


ÓÉÓÚ´«¸øSrvNetAllocateBufferµÄ²ÎÊýΪ0xf£¬Æ¾Ö¤SrvNetAllocateBufferµÄ´¦Öóͷ£Á÷³Ì¿ÉÖª£¬¸ÃÇëÇóÄڴ潫´ÓSrvNetBufferLookasides±íÖзÖÅÉ¡£ÕâÀïÐèҪעÖصÄÊÇ£¬±äÁ¿SrvDisableNetBufferLookAsideList¸ú×¢²á±íÏîÏà¹Ø£¬ÏµÍ³Ä¬ÈÏ״̬ÏÂSrvDisableNetBufferLookAsideListΪ0¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ7 SrvDisableNetBufferLookAsideList±äÁ¿³õʼ»¯Àú³Ì


SrvNetBufferLookasides±íͨ¹ýº¯ÊýSrvNetCreateBuffer³õʼ»¯£¬ÏÖʵSrvNetCreateBufferÑ­»·Å²ÓÃÁËSrvNetBufferLookasideAllocate·ÖÅÉÄڴ棬ŲÓÃSrvNetBufferLookasideAllocateµÄ²ÎÊý»®·ÖΪ[¡®0x1100¡¯, ¡®0x2100¡¯, ¡®0x4100¡¯, ¡®0x8100¡¯, ¡®0x10100¡¯, ¡®0x20100¡¯, ¡®0x40100¡¯, ¡®0x80100¡¯, ¡®0x100100¡¯]¡£ÔÚÕâÀÄÚ´æ·ÖÅɲÎÊýΪ0xf£¬¶ÔÓ¦µÄlookaside±íΪ0x1100¾ÞϸµÄ±íÏî¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ8 SrvNetCreateBuffer·´±àÒë´úÂë


SrvNetBufferLookasideAllocateº¯ÊýÏÖʵÊÇŲÓÃSrvNetAllocateBufferFromPoolÀ´·ÖÅÉÄڴ棬Èçͼ9Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ9 SrvNetBufferLookasideAllocate·´±àÒë´úÂë


ÔÚº¯ÊýSrvNetAllocateBufferFromPoolÖУ¬¹ØÓÚÓû§ÇëÇóµÄÄÚ´æ·ÖÅɾÞϸ£¬ÄÚ²¿Í¨¹ýExAllocatePoolWithTagº¯Êý·ÖÅɵÄÄÚ´æÏÖʵҪ´óÓÚÇëÇóÖµ£¨¶à³ö²¿·ÖÓÃÓÚ´æ´¢²¿·ÖÄÚ´æÏà¹ØÊý¾Ý½á¹¹£©¡£ÒÔÇëÇó·ÖÅÉ0x1100¾ÞϸΪÀý£¬¾­ÓÉһϵÁÐÅжϺó£¬×îºó·ÖÅɵÄÄÚ´æ¾Þϸallocate_size = 0x1100 + E8 + 2*(MmSizeOfMdl + 8)¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ10 SrvNetAllocateBufferFromPoolº¯Êý·´±àÒë´úÂë


ÄÚ´æ·ÖÅÉÍê±ÏÖ®ºó£¬SrvNetAllocateBufferFromPoolº¯Êý»¹¶Ô·ÖÅɵÄÄÚ´æ¾ÙÐÐÁËһϵÁгõʼ»¯²Ù×÷£¬×îºó·µ»ØÁËÒ»¸öÄÚ´æÐÅÏ¢½á¹¹ÌåÖ¸Õë×÷Ϊº¯ÊýµÄ·µ»ØÖµ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ11  SrvNetAllocateBufferFromPool³õʼ»¯ÄÚ´æÊý¾Ý


ÕâÀïÐèҪעÖØÈçϵÄÊý¾Ý¹Øϵ£ºSrvNetAllocateBufferFromPoolº¯Êý·µ»ØÖµreturn_bufferÖ¸ÏòÒ»¸öÄÚ´æÊý¾Ý½á¹¹£¬¸ÃÄÚ´æÊý¾Ý½á¹¹ÆðʼµØµãͬÏÖʵ·ÖÅÉÄڴ棨º¯ÊýExAllocatePoolWithTag·ÖÅɵÄÄڴ棩ÆðʼµØµãµÄµÄÆ«ÒÆΪ0x1150£»return_buffer+0x18λÖÃÖ¸ÏòÁËÏÖʵ·ÖÅÉÄÚ´æÆðʼµØµãÆ«ÒÆ0x50λÖô¦£¬¶ø×îÖÕreturn_buffer»á×÷Ϊº¯ÊýSrvNetAllocateBufferµÄ·µ»ØÖµ¡£ÆäÄÚ´æ½á¹¹¹ØϵÈçͼ12¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢


ͼ12 SrvNetAllocateBuffer£¨0xf)·µ»ØµÄÄÚ´æÊý¾Ý½á¹¹


Îó²îÄÚ´æÆÆËðÆÊÎö


»Øµ½Îó²î½âѹº¯ÊýSrv2DecompressData£¬ÔÚ¾ÙÐÐÄÚ´æ·ÖÅÉÖ®ºó£¬Srv2DecompressDataŲÓú¯ÊýSmbCompressionDecompress×îÏȽâѹ±»Ñ¹ËõµÄÊý¾Ý¡£Æ亯ÊýÂß¼­Èçͼ13Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ13 Srv2DecompressData½âѹѹËõÊý¾Ý


ÏÖʵÉÏ£¬¸Ãº¯ÊýŲÓÃÁËWindows¿âº¯ÊýRtlDecompressBufferEx2À´ÊµÏÖ½âѹ£¬Æ¾Ö¤RtlDecompressBufferEx2µÄº¯ÊýÔ­ÐÍÀ´¶ÔÓ¦ÆÊÎöSmbCompressionDecompressº¯ÊýµÄ¸÷¸ö²ÎÊý¡£


SmbCompressionDecompress(CompressAlgo£¬//ѹËõËã·¨

Compressed_buf£¬//Ö¸ÏòÊý¾Ý°üÖеÄѹËõÊý¾Ý  

Compressed_size£¬//Êý¾Ý°üÖÐѹËõÊý¾Ý¾Þϸ£¬ÅÌËã»ñµÃ  

UnCompressedBuf,//½âѹºóµÄÊý¾Ý´æ´¢µØµã£¬*(alloc_buffer+0x18)+0x10    

UnCompressedSize,//ѹËõÊý¾Ýԭʼ¾Þϸ,Ô´ÓÚÊý¾Ý°üOriginalCompressedSegmentSize  

FinalUnCompressedSize)//×îÖÕ½âѹºóÊý¾Ý¾Þϸ


´Ó·´±àÒë´úÂë¿ÉÒÔ¿´³ö£¬º¯ÊýSmbCompressionDecompressÖÐÉúÑĽâѹºóÊý¾ÝµÄµØµãΪ*(alloc_buffer+0x18)+0x10µÄλÖã¬Æ¾Ö¤ÄÚ´æ·ÖÅÉÀú³ÌÆÊÎö£¬alloc_buffer + 0x18Ö¸ÏòÁËÏÖʵÄÚ´æ·ÖÅÉÆðʼλÖÃÆ«ÒÆ0x50´¦£¬ÒÔÊÇ¿½±´Ä¿µÄµØµãΪÏÖʵÄÚ´æ·ÖÅÉÆðʼµØµãÆ«ÒÆ0x60λÖô¦¡£


ÔÚ½âѹÀú³ÌÖУ¬Ñ¹ËõÊý¾Ý½âѹºó½«´æ´¢µ½Õâ¸öµØµãÖ¸ÏòµÄÄÚ´æÖС£Æ¾Ö¤evilDataÊý¾ÝµÄ½á¹¹Àú³Ì£¬½âѹºóµÄÊý¾ÝΪռ¿ÓÊý¾ÝºÍtokenAddr¡£¿½±´µ½¸Ã´¦µØµãºó£¬tokenAddr½«ÁýÕÖÔ­ÄÚ´æÊý¾Ý½á¹¹ÖÐalloc_buffer+0x18´¦µÄÊý¾Ý¡£Ò²¾ÍÊǽâѹËõº¯ÊýSmbCompressionDecompress·µ»Øºó£¬alloc_buffer+0x18½«Ö¸ÏòÑéÖ¤³ÌÐòµÄtokenAddrÄں˵صã¡£¿½±´Àú³ÌÈçͼ14ºÍ15Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ14 ½âѹ¿½±´Àú³Ì


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ15½âѹÍê³ÉºóÄÚ´æ½á¹¹


¼ÌÐø¿´Srv2DecompressDataµÄºóÐø´¦Öóͷ£Á÷³Ì£¬½âѹÀֳɺ󣬺¯ÊýÅжÏoffsetµÄЧ¹û²»Îª0¡£²»Îª0Ôò¾ÙÐÐÄÚ´æÒƶ¯£¬Äڴ濽±´µÄ²ÎÊýÈçÏ£º


memmove(*(alloc_buffer+0x18)£¬SMB_payload£¬offset)

´Ëʱ£¬alloc_buffer+0x18ÒѾ­Ö¸ÏòÑéÖ¤³ÌÐòµÄtokenAddrÄں˵ص㣬¶øSMB_payload´ËʱָÏòevilDataÖеÄȨÏÞÊý¾Ý£¬offsetÔòΪ0x10¡£Òò´Ë£¬Õâ¸öÄÚ´æÒƶ¯Íê³Éºó£¬È¨ÏÞÊý¾Ý½«Ð´ÈëtokenAddr´¦¡£ÕâÒâζ×Å£¬SMS ServerÀÖ³ÉÐÞ¸ÄÁËÑéÖ¤³ÌÐòµÄȨÏÞ£¬´Ó¶øʵÏÖÁËÑéÖ¤³ÌÐòµÄÌáȨ£¡


ÉÐÓÐÒ»¸öϸ½ÚÐèҪעÖØ£¬ÔÚ½âѹʱ£¬Srv2DecompressDataº¯Êý»áÅжÏÏÖʵµÄ½âѹºóÊý¾Ý¾ÞϸFinalUnCompressedSizeÊÇ·ñºÍÊý¾Ý°üÖÐԭʼÊý¾Ý¾ÞϸOriginalCompressedSegmentSizeÒ»Ö£¬Èçͼ16Ëùʾ¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢


ͼ16 Srv2DecompressData¼ì²éѹËõÊý¾Ý¾Þϸ


°´ÀíÀ´ËµÏÖʵ½âѹºóµÄÊý¾Ý¾ÞϸΪ0x1100£¬²»¼´ÊÇÊý¾Ý°üÖеÄԭʼѹËõÊý¾Ý¾Þϸ0xffffffff£¬ÕâÀïÓ¦¸Ã½øÈëµ½ºóÃæÄÚ´æÊͷŵÄÁ÷³Ì¡£È»¶ø£¬ÏÖʵÉÏÔÚº¯ÊýSmbCompressionDecompressÖУ¬Å²ÓÃRtlDecompressBufferEx2Àֳɺó»áÖ±½Ó½«OriginalCompressedSegmentSize¸³Öµ¸øFinalUnCompressedSize¡£ÕâÒ²ÊǸÃÎó²î¹ØÓÚí§ÒâµØµãдÈëÀֳɵÄÒªº¦Ö®Ò»¡£


ÓÅ·¢¹ú¼Ê¡¤ËæÓŶø¶¯Ò»´¥¼´·¢

ͼ17 SmbCompressionDecompres¸³ÖµFinalUnCompressedSize


Îó²îÐÞ¸´½¨Òé


CVE-2020-0796ÊÇÄÚ´æÆÆËðÎó²î£¬È«ÐÄʹÓÿɵ¼ÖÂÔ¶³Ì´úÂëÖ´ÐУ¬Í¬Ê±ÍøÂçÉÏÒѾ­·ºÆð¸ÃÎó²îµÄÍâµØÌáȨʹÓôúÂë¡£ÔÚ´Ë£¬½¨ÒéÊÜÓ°Ïì°æ±¾WindowsÓû§ÊµÊ±Æ¾Ö¤Î¢Èí¹Ù·½Îó²î·À»¤Í¨¸æ¶Ô¸ÃÎó²î¾ÙÐзÀ»¤¡£


²Î¿¼Á´½Ó£º

1.https://fortiguard.com/encyclopedia/ips/48773

2.https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/ADV200005

3.https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/CVE-2020-0796

4.https://www.catalog.update.microsoft.com/Search.aspx?q=KB4551762

5.https://github.com/danigargu/CVE-2020-0796

6.https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/5606ad47-5ee0-437a-817e-70c366052962

7.https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-rtldecompressbufferex2