27 Nov
Microsoft Patch Analysis (binary diffing)
hello again to all our patient readers
it’s been a long time since we wrote our last post’s ?! first of all i should say sorry for late in blog updates but the first reason is we are really busy in these days with accomplish our projects . the second reason was changing our server . and finally the third reason is starting abysssec inc with a professional team for accomplish new projects and services . in soon future we have lots of good news may that’s interest you . so please be patient to see our news on our new index (that come soon as soon possible)
===================================================================
today i wanna talk about Microsoft security patch’s analysis . as you know this year and specially last month’s of this year was a nightmare for M$ windows because we saw MS08-067 – MS08-068 – MS08-006 and MS08-001 and etc . and as you know too publishing real and working exploits is going to die and just you can see commercial exploits on time .
i saw this picture in one of Mr Nicolas Waisman presentation and i believe to mind of this picture :
my goal from this introduction is if you want an exploit on publishing time you just have two chose :
1- write your own exploit
2- buy commercial exploit for your requirement vulnerability
- if you are a super millionaire you can buy all commercial exploits from variant security research teams and we are one of them ;)
- and if you are not you and you like and you need an exploit on time you should write your own exploit . and writing exploit for modern operation system’s is not easy because you need bypass a dozen of memory protections (such as DEP / ASLR / SAFSEH / Safe unlinking and etc … (from OS to commercial target software) also i believe this Mr Dave Aitel sentence : Not only are bugs expensive but the techniques for reliably exploiting bugs becomes expensive .
anyway becoming a real exploit coder is not easy but it’s possible and i should quote and notice another sentence that is : Modern Exploits – Do You Still Need To Learn Assembly Language (ASM) ( you can read full post here : (http://www.darknet.org.uk/2008/09/modern-exploits-do-you-still-need-to-learn-assembly-language-asm/)
i,m fully sure learning assembly language will help you in all of exploit development levels from reversing and understanding vulnerability to writing reliable exploit code for modern operation system’s .
after you can understand assembly code you can supposition high level code and thereupon you can identify vulnerability from discrepancy between patched and unpatched binaries (however advanced tools and IDA plugin’s make your life easier and you can identify vulnerable code / function if a few minutes) this technic is called binary diffing. in future i,ll discuss a few advanced trick and methods , that’s improve your speed and analysis but for now i just talk about main of binary diffing on Microsoft security patch’s .
first step is downloading patch from Microsoft . the best way is searching on Microsoft site for your target bulletin . for example see MS08-067 (my favorite bug in this year :D )
just you need click on your target os and download the path.
after you downloaded the patch as you know you should not install the patch and you need extract patch data
with /x command .for example extracting ms08-067 patch :
the output of executing atop command is extract all date inside the patch . and in this example result is :
as you can see in this patch we have just one file and that is a dll named netapi32.dll so we can understand vulnerable function is in this dll .
next step is find vulnerable (unpatched) file (or files) on your system and then you can rename patched file to filename_patched.XXX and then you can analysis and notice changes in patched and unpatched files.
for accomplish this procedure you can use different tools and ways . but using IDA Pro is one of best and logical ways you can use for this procedure . you can understand changes without any plugins and auxiliary tools but for imporving speed and getting better result you have tree choice .
1- using bindiff (exclusive commercial IDA plugin and best auxiliary too analysis
for example you can see patch analysis video for MS08-001 (TCP/IP Kernel Pool Overflow) here :
http://www.zynamics.com/files/ms08001.swf
2- using Eeye DiffingSuite i like this tools because it’s really easy to use and effective .
you can download this tools from following link :
http://research.eeye.com/html/Tools/download/DiffingSuiteSetup.exe
and also you see tree good video about analysis different patched with this tools
- analysing MS06-033 : http://research.eeye.com/html/tools/tutorials/BDS_v_MS06-033.htm
- analysing MS06-007 : http://research.eeye.com/html/tools/tutorials/MS06-007.htm
- analysing MS06-036 : http://research.eeye.com/html/tools/tutorials/MS06-036%20Analysis.htm
after videos please read following link (a good work from Mr stephen lawler) about full reverse of MS08-067 patch using DiffingSuite and IDA pro cheerfully because it contain divisor of work :
http://www.dontstuffbeansupyournose.com/?p=35
3- using tenable security PatchDiff . PatchDiff is another IDA Pro Plugin (like bindiff) but have a big difference with Bindiff this plugin is free !
you can see a video about this plugin here :
http://cgi.tenablesecurity.com/tenable/pdiff2.swf.html
and you can download this plugin from following link :
http://cgi.tenablesecurity.com/tenable/dl.php?p=patchdiff2-2.0.5.zip
using this plugin is so easy but i discuss a few about this plugin . frist of all you need patched and unpatched binaries after this you just first need open unpatched binary IDA and save disassembly in idb file after that you should open patched binary and save disassembly result to another idb file :
since this you just need open unpatched IDB using plugin to understating discrepancy . after this step as Mr Nicolas Pouvesle (pathdiff plugin author) discussed graph nodes can be synchronized by double clicking on a given node. Graphs use the following colors:
- white: identical nodes
- grey: unmatched nodes
- red: matched nodes
- tan: identical nodes (different crc)
for example you see patchdiff result for MS08-067 patch :
and :
if you be smart you can write a high level simulator code for vulnerable function . for example Mr Alexander Sotirov wrote a simulator of vulnerable function :
#include// This is the decompiled function sub_5B86A51B in netapi32.dll on XP SP3
// and sub_6EA11D4D on Vista SP1int ms08_067(wchar_t* path)
{
wchar_t* p;
wchar_t* q;
wchar_t* previous_slash = NULL;
wchar_t* current_slash = NULL;
wchar_t ch;#ifdef VISTA
int len = wcslen(path);
wchar_t* end_of_path = path + len;
#endif// If the path starts with a server name, skip it
if ((path[0] == L’\\’ || path[0] == L’/') &&
(path[1] == L’\\’ || path[1] == L’/'))
{
p = path+2;while (*p != L’\\’ && *p != L’/') {
if (*p == L’\0′)
return 0;
p++;
}p++;
// make path point after the server name
path = p;
// make sure the server name is followed by a single slash
if (path[0] == L’\\’ || path[0] == L’/')
return 0;
}if (path[0] == L’\0′) // return if the path is empty
return 1;// Iterate through the path and canonicalize ..\ and .\
p = path;
while (1) {
if (*p == L’\\’) {
// we have a slashif (current_slash == p-1) // don’t allow consequtive slashes
return 0;// store the locations of the current and previous slashes
previous_slash = current_slash;
current_slash = p;
}
else if (*p == L’.’ && (current_slash == p-1 || p == path)) {
// we have \. or ^.if (p[1] == L’.’ && (p[2] == L’\\’ || p[2] == L’\0′)) {
// we have a \..\, \..$, ^..\ or ^..$ sequenceif (previous_slash == NULL)
return 0;// example: aaa\bbb\..\ccc
// ^ ^ ^
// | | &p[2]
// | |
// | current_slash
// |
// previous_slashch = p[2];
#ifdef VISTA
if (previous_slash >= end_of_path)
return 0;wcscpy_s(previous_slash, (end_of_path-previous_slash)/2, p+2);
#else // XP
wcscpy(previous_slash, &p[2]);
#endifif (ch == L’\0′)
return 1;current_slash = previous_slash;
p = previous_slash;// find the slash before p
// BUG: if previous_slash points to the beginning of the
// string, we’ll go beyond the start of the buffer
//
// example string: \a\..\q = p-1;
while (*q != L’\\’ && q != path)
q–;if (*p == L’\\’)
previous_slash = q;
else
previous_slash = NULL;
}
else if (p[1] == L’\\’) {
// we have \.\ or ^.\#ifdef VISTA
if (current_slash != NULL) {
if (current_slash >= end_of_path)
return 0;
wcscpy_s(current_slash, (end_of_path-current_slash)/2, p+2);
goto end_of_loop;
}
else { // current_slash == NULL
if (p >= end_of_path)
return 0;
wcscpy_s(p, (end_of_path-p)/2, p+2);
goto end_of_loop;
}
#else // XP
if (current_slash != NULL) {
wcscpy(current_slash, p+2);
goto end_of_loop;
}
else { // current_slash == NULL
wcscpy(p, p+2);
goto end_of_loop;
}
#endif
}
else if (p[1] != L’\0′) {
// we have \. or ^. followed by some other charif (current_slash != NULL) {
p = current_slash;
}
*p = L’\0′;
return 1;
}
}p++;
end_of_loop:
if (*p == L’\0′)
return 1;
}
}// Run this program to simulate the MS08-067 vulnerability
int main()
{
return ms08_067(L”\\c\\..\\..\\AAAAAAAAAAAAAAAAAAAAAAAAAAAAA”);
}
final steps are identify vulnerable function / understaning function parameters and write a POC code for controlling EIP .
for example Mr stephen lawler wrote a c program for checking MS08-067 vulnerability by taking the offset between sub_7CDDB23D and the load address of NETAPI32.DLL :
#include#include
int wmain(int argc, wchar_t **argv)
{
HMODULE netapi32 = LoadLibraryW(argv[1]);
void (__stdcall *foo)(PWCHAR);
WCHAR buf[4096];
*(PVOID*)&foo = (PVOID)(((PUCHAR)netapi32) + 0×1b23d);
//__asm { int 3 }
wcscpy(buf, argv[2]);
foo(buf);
wprintf(L”%s\n”, buf);
}
and finnaly he got a crash :
after getting first crash you just need getting eip and write exploit for vulnerability .
finally i should say sorry for disheveled writing . the reason of this is size of this subject in next post i talk directly about patch analysis tricks and i,ll anlysis another interesting Microsoft Patch step by step .
thank you for your time and attention
best regards
shahin.r