Expanding Functions when Ida Fails

Posted by Eric | Architecture, Python, Reversing | Tuesday 1 July 2008 10:46 am

Functions all start out with some type of prologue. Powerpc is no different. I was recently taking apart some firmware when I discovered the need to have an IDA Script take care of some function creations. Since the firmware was so large I didn’t really want to continue scrolling down and manually converting the data to code by pressing ‘c’.

In order to have Ida Python create functions automatically you can use two functions.

FindBinary
and
MakeFunction

the concept is to use FindBinary to locate the standard function prologue byte code and from there, create a function using MakeFunction. Using a python while loop you can create a short IdaPython script to expand your disassembly. I ran it on a fairly large piece of firmware and it ended up handling a lot of the disassembly that Ida’s auto analysis failed to complete due to the lack of a true entry point.

When it comes to PowerPC our prologue is going to look something like this

mflr r0 ; Save the link register to r0
stw r0, 4(rsp) ; Save r0 to register commonly used as stack pointer
stwu rsp,-16(rsp) ; Create frame
stw r31, 12(rsp); Save r31

For the particular piece of firmware I was playing with it was not saving the link register. Our prologue began with a stwu instruction. So, we use FindBinary to locate all instances of 0×9421. Although the method is not perfect, on a large scale it’s much easier to automate the creation of functions via IdaPython or IDC and have a few errors, than it is to manually create all the functions.

In terms of a prologue, you are usually left with some type of branching instruction. For most functions it just be a BLR instruction which is “Branch to Link Register”

Python Uniq

Posted by Eric | Code, Python | Wednesday 26 March 2008 1:49 pm

<meta content="OpenOffice.org 2.0 (Linux)" name="GENERATOR" /><meta content="20080326;12392200" name="CREATED" /><meta content="16010101;0" name="CHANGED" /><br /> <style> <!-- @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } --> </style> <p style="margin-bottom: 0in">Python doesn’t have a Uniq for lists, or anything. My problem was, I had a huge text file that I was going through and my regular expression was picking up multiple instances of the same thing. What was happening was I would get 80+ items in a list, which were for the most part nothing but repetitive. I found an example of making your own unique in the python cookbook, but it didn’t work out right for me. When I ran it through the unique function it would either 1) spit back out an empty list, or 2) would spit back out the same think I put in, minus one item. I ended up writing up the following code, which is largely inefficient, but it ended up working. It will throw exceptions based on IndexError and ValueError so I had to place it in a try block. It will also repeat the first item usually. But hey, I can deal with having the first two elements in the returns array being the same, I no longer have 80 elements coming back that are exactly the same!</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">I’d love feedback if anyone has a better solution, Always looking for code optimization <img src='http://hamsterswheel.com/techblog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p> <p style="margin-bottom: 0in"> <blockquote> <p style="margin-bottom: 0in">def unique(self,values):<br /> #pass in a list()<br /> if len(values) == 0:<br /> print “something wrong in dixieland”<br /> sys.exit()<br /> else:<br /> t = values<br /> t.sort()<br /> u = list()</p> <p>i = 0</p> <p>try:<br /> ret = cmp(t[0], t[1])<br /> if ( ret == 0):<br /> u.append(t[0])<br /> while 1:<br /> ret = cmp(t[i], t[i+1])<br /> if ret !=0:<br /> u.append(t[i])<br /> else:<br /> pass<br /> i+=1<br /> except ValueError:<br /> pass</p> <p>except IndexError:<br /> pass</p> <p>return u</p></blockquote> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=54#comments" title="Comment on Python Uniq">Comments (5)</a> </div> </div> </div> <div id="footer"> © Copyright 2009 | <a href="http://hamsterswheel.com/techblog">Phn1x – Hamsterswheel</a> | Theme by <a href="http://clubparexcellancetech.com/">Club Par Excellance</a> | All Rights Reserved | Sponsored by <a href="http://www.voipkit.ca/">VoIP</a> </div> </body> </html>