Opaque Predicates

Posted by Eric | Research, Reversing | Wednesday 23 July 2008 12:21 pm

An Opaque predicate is a line of code, or lines of code that are basically useless. They actually do something, but in terms of usefulness they don’t do anything. Does that even make sense? Anyway they are designed to make it appear as if the code was doing something to divert the attention of an analyst. The purpose of an opaque predicate is to add a layer of obfuscation into code that makes reversing the code more time consuming. As you sift through useless instruction you realize the code is more of a distraction than anything else. Opaque Predicates make static analysis more time consuming because they are often difficult to discover. You stumble upon a code block only to find after a few minutes that it’s dead.

So what does an opaque predicate look like?

In a high level language such as C/C++ we can have the most basic opaque predicate as a simple IF Insertion. Inserting IF statements to always come back true will surely not be effective on performance but in terms of static analysis it can throw off an analyst for a few minutes as they chase down a rabbit hole.

So we have something similar to this:

1
2
3
4
5
6
7
8
9
if(true)
{
Dosomething</code>
 
}
else
{
Dosomething
}

True will always come back as true. The first branch will always be the one chosen in actual execution. However, the code in the first branch and the second branch will be the same. This adds an additional branch to a graph in IDA, and adds more code the analyst must sort through. Just two are manageable, imagine if you had multiple if else’s in the code!

Ida Graph

And here is the deadlist

You can add an additional level of complexity to the code with math operations. An opaque predicate such as:

1
2
3
4
5
6
int    add,q = 2;</code>
 
if(((q+q^2)%2)==0)
printf("here");
else
add = 2+8;

Utilizes Dead code insertions. The else operation can be anything you want, for simplicity sake I chose the addition of two numbers and the assignment into a variable. However in real examples the code should be more complex. It should be dead code so that it may be functional but does not truly serve a purpose to the program. This will often lead the analyst down a path where they may spend anywhere from 10 minutes to an hour with their attention diverted from the real path. This type of predicate has the general layout of

1
2
3
4
if(math expression that always resolved)
Real work
Else
Dead code

In IDA this would graph similar to:
Graph
The dead listing would look like the following:
Dead List

These examples are rather simple, but adding complex junk code to them can often divert an analyst’s attention for a period of time. Especially if you make the junk or dead code seems more interesting then the code behind the real work!

Malware Dropper

Posted by Eric | Code, Research, Reversing | Friday 11 July 2008 12:08 am

A while back dean sent me some malware that he had been collecting off client systems. He didn’t ask me to do an analysis on them but I started doing it anyway. The first aspect of this malware was a delivery system that actually got some type of client side execution on the system.

The delivery has three aspects of it and is served via javascript.

First in the event javascript is disabled the visited page displays a link for the user to click. The title simply says “Please download”. If javascript works, it attempts to a function in MDAC by creating and ADODB stream and downloading the file. The file downloaded is then named svchosts.exe, and the shellexecute function is called with the svchosts.exe file name passed to it. On a Linux machine, an automatic save box is displayed within about 10 seconds. An example of the javascript decoded by dean can be found here. WARNING – It is set up to automatically download and launch a file called video.exe. I have copied a safe version of calc.exe to the web root so don’t get your pants in a bunch when it either launches, or attempts to downoad… If it does launch, you need to 1) stop using windows/internet explorer and 2) patch your system.

Dean was able to pull a copy of video1.exe from the server hosting this piece of malware. I took a look at it for analysis a few day’s ago. Initial tests for packing came up empty, but it would seem there is slight obfuscation in the code. There are many places where analysis fails, there are absolutely no strings, and some of the code seems to jump around to non existent addresses. I am still attempting to identify what type of encryption/packer was used on the code to optimize my disassembly. Until then I have barely any strings and only a few imports.

The few imports I do have lead me to recreate the file video1.exe. All of the imports came from Wininet which is the windows API for internet data. I set about researching a few of the api’s and pieceing together the logic and code behind video1.exe. I’ve concluded that it downloads and executes a file from another place using the functions in Wininet. Further preparing my infrastructure for penetration testing I wanted to create my own dropper for custom trojans and thinks beyond meterpreter.exe. I wanted something that would not be flagged by Anti Virus and that I can keep relatively small.

What I did was create Droplet. I finished the code last night and It’s pretty light weight. I did not encorporate any of the ftpGetFile, or Gopher functions from Wininet. I simply wanted a file dropper that would download code, copy it to a file and then execute it.

The basic flow of the program is

InternetOpen
InternetConnect
HttpOpenRequest
HttpAddRequestHeaders
HttpQueryInfo
HttpSendRequest
Either InternetReadFile or URLDownloadToFile

From this point if you are using InternetReadFile you can use the Windows API to create the file and copy the data too. I’m still working bugs out with this method.

URLDownloadToFile seems to be the better choice since it is designed to copy the data to a file instead of to a buffer. I think I might head out to the river this weekend but I’m hoping to have some time that I can play with the FTP Functions inside WinInet.

Anyway, Malware Analysis is pretty cool because you can learn how people smarter than you carry out nefarious biddings. Hopefully you learn from it and you can either apply it to your penetration testing, or for your own biddings!

-E

Learn Asm first…

Posted by Eric | General, Research, Reversing | Wednesday 9 July 2008 11:12 pm

As many of the readers here know I’ve been on my grind lately in an effort to become really proficient in reverse engineering. Two of my associates have made comments regarding a skill gap they have in the field of reverse engineering. Everyone has a different learning curve, and everyone certainly has a different skill set. Some skill sets are more advanced than others but when you work in a team effort one skill set can often complement another. Groups from back in the day used to be pretty bad ass. Personally I was never part of a group, I guess I just never found my way into one. I really wish I had been part of one because I’d probably be bad ass right now instead of losing sleep at night in an effort to walk in the presence of some of the top guys in Austin and around the world. There are a lot of dude’s I look up to and often read their material. Indirectly, they are teaching me lots and I appreciate it!

I’ve been holding onto a book I picked up a while back from Wrox publishing titled “Professional Assembly.” The book is part of their programmer to programmer series. I originally found it two years ago when I was doing penetration testing and I figured it would help me out in my efforts to master exploitation. It really did give me a great understanding of functions, and the stack but beyond that I just could not wrap my attention around the book. I’ve finally got into the book and have been walking through many of the examples and creating my own in an effort to really nail down assembly language. I think I received a third rate education in Computer Science because assembly was never on our list of things taught, hell I don’t even think it was mentioned which is pretty sad.

How many times have you opened up a tutorial for reverse engineering or exploitation and saw something along the lines of “Basics of assembly language is assumed” or “No assembly knowledge necessary.” For many they browse over those lines and get lost somewhere in the second or third page. I’ll be honest I was one of them. I found myself referencing instructions far too often it was impeding my process. Now that I’ve read through the book and worked the examples not only do I find myself having the ability to optimize assembly (not that I need to in many cases) but I’ve also found I can breeze through disassembly in binaries a lot faster as I go after relevant code sections.

The morale of the story is don’t be like me and try to learn to reverse without learning assembly first. Get your assembly down packed, and many MANY concepts in reversing and exploitation will come together faster than you realize. As you read the book many concepts will click and you will go “ohhhhhh.”

Anyway, just wanted to mention the book, I found it awesome. The only con I found with it is the lack of exercises at the end of the chapters. I found myself googling for exercises and stealing many from colleges that offered assembly courses. Either way, I learned a lot from this book even recognizing certain code optimizations such as memcpy and strlen type functions. Check it out if you want to learn. Just make sure you do the exercises/examples.



PPC Notes

Posted by Eric | Architecture, Research | Tuesday 24 June 2008 10:48 am

Been working through PowerPC assembly and Mysql as of Late.

As I was browsing through the book store hanging out with my son I noticed a copy of the database hackers handbook for less than $9.00, Figured I can’t go wrong at that price. I never really had an interest in databases but as I flipped through the book I realized that aside from a general ability to run through SQL commands I didn’t really have a good understanding of database systems. I created a nice syllabus to run through Mysql for the moment. Towards the latter half I’ll actually get into SQL Injections so if you’ve got some decent tuts in your link base drop it!

Anyway, PowerPC assembly is Hot, still a bit vexing since I’m used to Intel. However I see how PPC is much cleaner and more efficient. For those interested I’ve been taking notes as I follow through a syllabus for learning PPC that I created. I modeled it off the Professional Assembly programming book by Wrox publishing (which mind you is an awesome book for the Intel architecture). Right now I’ve only dropped the architecture overview notes, I am working through Moving data around and Stacks. PPC is really interesting when you get to stacks since it doesn’t necessarily have hardware support for one. However, using registers and some instructions you can create a stack and manipulate it. However, there are no formally defined instructions for it such as Intel’s PUSH and POP.

Notes are in Here.

Damn thought I had one.

Posted by Eric | Fuzzing, Research, Vulnerabilities | Tuesday 15 April 2008 8:43 am

<meta content="OpenOffice.org 2.0 (Linux)" name="GENERATOR" /><meta content="20080415;7265400" 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">Been busy running around lately, and now the mother in law and family is in town, theoretically the wife will be occupied with that. I created a pop3 request for Sulley and I’ve been back tracking and hitting the pop3 service on a few of the Mail servers that I have downloaded and hit with the SMTP requests. Last week I discovered a weird bug that seemed random at best and after a while of getting frustrated I asked MC for an assist. MC Tracked down the bug despite not being able to get it to crash. Turned out there was already an advisory on it, and it was an incorrect handling of connections. Basically the Application (baby pop3) was not handling multiple connections from the same host correctly and would result in a crash. On the vendors website there were quite a few other applications in this baby series and I’m pretty sure they are using the same template for code because I was able to get the web server to crash also.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Last night I discovered an XSS bug in a vendor site. I actually completely stumbled on it. I was messing with a mail server, browsing around and looking for inputs (this thing opens about 12 ports upon install) and I came across the web application on port 7026. Most of the pages required authentication, but the help pages didn’t. Within the Help index there was a search box for “on line search.” When you put Javascript into the search box and hit enter, you are taken to the vendors site (shown your alert text box) and then get some errors on search string not found. I kicked off an email to the vendor and they responded back in like 15 minutes, but have yet to ACK FIN saying it was fixed.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">I continue hitting up that same application, It’s got an smtp, a pop3 and about 4 web interfaces. I noticed that the webmail is actually accessible via a path that leads to webmail.exe?cmd= . Currently fuzzing the admin.cgi ones, but I plan to start fuzzing the webmail.exe input this evening when I get home.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">I need to start working on a short presentation for AHA! I’m debating on talking about the Fuzzing ( a lot of those guys work for dvlabs so I dunno) or I can talk about embedded debugging. On one hand, If i talk about fuzzing, I can segway that into the question of “How can I analyze these crashes better” because the scripts with sulley haven’t been working out for me. Or the “Has anyone done any Fuzzing on embedded systems, and if so how did you go about analyzing the crash.” On the other hand, I can just go straight into Embedded debugging and ask that question anyway. Of course I still need a few hundred bucks from Dean to sponsor more research….</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Thanks again to MC for checking out that bug.</p> <p style="margin-bottom: 0in"> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=60#respond" title="Comment on Damn thought I had one.">Comments (0)</a> </div> </div> <div class="post"> <h2 id="post-58"><a href="http://hamsterswheel.com/techblog/?p=58" rel="bookmark">Awww Shixxnote</a></h2> <div class="meta">Posted by Eric | <a href="http://hamsterswheel.com/techblog/?cat=15" title="View all posts in Fuzzing" rel="category">Fuzzing</a>, <a href="http://hamsterswheel.com/techblog/?cat=11" title="View all posts in Research" rel="category">Research</a> | Sunday 6 April 2008 1:12 am </div> <div class="storycontent"> <p><meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE" /><title /><meta content="OpenOffice.org 2.0 (Linux)" name="GENERATOR" /><meta content="20080405;23434700" name="CREATED" /><meta content="20080405;23592900" name="CHANGED" /><br /> <style> <!-- @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } --> </style> <p style="margin-bottom: 0in">Recently I watched a Keynote by Dave Aitel, in it he discussed the Hacker mindset. One of the things he pointed out was when a new vulnerability comes out to make sure your fuzzer can pick it up, and if not figure out why and then make the fuzzer pick up the vuln. Shixxnote 6.net had a <a href="http://secunia.com/advisories/12822/">buffer overflow</a>  quite a while ago by Luigi A who is amazing at finding vulns, I’ve Rss’ed to his site and That dude kicks out an average of two per day. I still had the software in my vuln software bank and I decided I’d break it out and play with it for the evening.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">The concept behind shixxnote is simple, you can create notes for yourself and even send them to other uses on a network. If the other user doesn’t have the software, it will default to sending it via the Messenger service. You can assume it runs over messenger service, but in reality it does that by default and has it’s own little protocol going on. No documentation available we turn to wireshark. Wireshark has this amazing ability at decoding data/ protocols. Unfortunately Wireshark had no f00 when it came to shixxnote. I installed shixxnote on two different boxes and sent a message to each other and sniffed the traffic. This is what I got:</p> <p style="margin-bottom: 0in"><img src="http://hamsterswheel.com/blogpics/wireshark.jpeg" /></p> <p style="margin-bottom: 0in">What I did was try to break it down as best as I could into a Sulley request. It ended up being too large to run on my VM image so I had to find some more ram for the only box in the house running windows. Eventually I got it up and going, and Sulley started kicking out the Fuzz! Somewhere around 618, and in the low 2000’s Shixxnote crashed. I’ve since started Sulley’s processmon and am monitoring for the exact cause of the crash. Anyway, I found it kind of cool taking apart the protocol (even though its not really a protocol per say, just a format of data sent over the wire)</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Request can be found <a href="http://hamsterswheel.com/code/sulley/shixxnote.py">here</a></p> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=58#respond" title="Comment on Awww Shixxnote">Comments (0)</a> </div> </div> <div class="post"> <h2 id="post-51"><a href="http://hamsterswheel.com/techblog/?p=51" rel="bookmark">Fuzzy wuzzy wuz a fuzz</a></h2> <div class="meta">Posted by Eric | <a href="http://hamsterswheel.com/techblog/?cat=15" title="View all posts in Fuzzing" rel="category">Fuzzing</a>, <a href="http://hamsterswheel.com/techblog/?cat=11" title="View all posts in Research" rel="category">Research</a> | Monday 17 March 2008 8:10 pm </div> <div class="storycontent"> <p><meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE" /><title /><meta content="OpenOffice.org 2.0 (Linux)" name="GENERATOR" /><meta content="20080317;18274300" name="CREATED" /><meta content="20080317;19062400" name="CHANGED" /><br /> <style> <!-- @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } --> </style> <p style="margin-bottom: 0in">I suppose it’s been a few day’s since my last post. So much for going with the flow of exploitation on Linux. I’ve been working on that over here, but I’ve had some other stuff to work on as well. I’ve also been pretty caught up with family obligations. It’s hard to balance the time between everything.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">One thing I’ve been hitting pretty hard is Fuzzing. I was never too intrigued by fuzzing until a few months ago. I started on a small scale with Comraider, which is a fuzzer but the need for knowledge to fuzz is virtually non existent. Hell, to an extent Comraider will even write the proof of concept code for you. SPIKE Fuzzer from Immunitysec is pretty old school, I played with that momentarily but the documentation was lacking at best, and it was more or less a lesson in code reading than anything else. You seriously have to walk through all the code of the other fuzzing templates and then attempt to decipher why certain variables are declared. My next attempt was Peach Fuzzer. Peach at first gave me great hope, despite it running on Windows only. I was using Peach to Fuzz some web servers since the HTTP protocol is somewhat easy to build simple templates for. Once I started going, I ran into an error with Peach, what’s worse it was an error with their code, and their HTTP Example. I did a quick fix on the error (that was, I cast the messed up variable to unicode) and I got passed that. However, I kept hitting snags. One snag after another until I finally just gave up on Peach. I even attempted to write to the author, he responded to my first email, but didn’t respond to my last two. The one really cool thing I have to say about peach was the ability to easily create XML files for the fuzzer. Peach works by accepting XML files of the protocol. Using Peachshark you can easily create the XML file based on a pdml file from Wireshark… Sniff, export, script and you’ve got a fuzzable XML file of the protocol. Nice!</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Finally Sulley, Sulley is the new Rave. Uses block based fuzzing, is written in Python, Was released last year at Blackhat… Or Defcon. I can’t remember, that week seems fuzzy! Crazy people from work, crazy stories. Probably the most drinking I did all of 2007 no lie! Anyhow, Once you get past the whole path issues with python, and you RTFM Sulley really kicks ass. Sulley comes documented enough that if you read the API and the quick intro you can figure out how to work it. Sulley also comes with the SPIKE Method of documentation via two sample requests in the request folder, these allow you to explore more complex protocol fuzzing.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">So far I’m really impressed with sulley, I was using it to Fuzz some web servers that I knew were vulnerable from the past. The first time I ran Sulley was using the (included) HTTP Request to go against Savant Web Server. Not even 100 tests in and I started to get results. The Next step I’m trying to take is to get post AUTH fuzzing going on protocols such as IMAP, FTP and POP3. Although not quite as delicious as Pre AUTH, POST Auth still count for something. I’ve been talking with Tebo on IRC and inspired him to do a little testing. He emailed me late in the evening saturday and woke my wife up when the blackberry started chirping. All in all it was a good email, with some code built off the stuff I had sent him. I noticed he enjoys using groups, whereas I build everything in blocks for my ftp example. I went to test some of the POST AUTH stuff he had sent me, and some of my POST AUTH code I was playing with saturday evening but my Box at work decided to not play well with others today. I didn’t really have time to look into it since I’ve been assigned a programming project at work. For taskings = sucks;</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Example of my quick solution that seemed to work for POST AUTH can be found in hamsterswheel.com/code.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">If you have solutions for the best method to hit POST AUTH on a protocol with sulley, let me know please!</p> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=51#comments" title="Comment on Fuzzy wuzzy wuz a fuzz">Comments (1)</a> </div> </div> <div class="post"> <h2 id="post-46"><a href="http://hamsterswheel.com/techblog/?p=46" rel="bookmark">The Briefest ELF intro EVAR!</a></h2> <div class="meta">Posted by Eric | <a href="http://hamsterswheel.com/techblog/?cat=3" title="View all posts in Linux" rel="category">Linux</a>, <a href="http://hamsterswheel.com/techblog/?cat=11" title="View all posts in Research" rel="category">Research</a> | Sunday 2 March 2008 1:15 am </div> <div class="storycontent"> <p><meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE" /><title /><meta content="OpenOffice.org 2.0 (Linux)" name="GENERATOR" /><meta content="20080301;22530800" 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">March marks a new month and lately that means a new topic for discussion and research on the blog. This month I’ve chosen a going back to basics tour dealing with linux exploitation. I’ve created a general outline to follow and will begin this evening with a few words on the Elf Format. There are a few tutorials out there already so I will make this brief.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">The Elf Format is a common standard for object files in linux, unix and unix like operating systems. The three main types of object files are:</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Relocatable file which holds code and data suitable for linking with other files</p> <p style="margin-bottom: 0in">Executable files- which hold a program suitable for execution.</p> <p style="margin-bottom: 0in">Share objects – which holds code and data suitable for linking</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">The ELF Format can be view visually as the following:</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"><a href="http://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Elf-layout--en.svg/200px-Elf-layout--en.svg.png"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Elf-layout--en.svg/200px-Elf-layout--en.svg.png" /><br /> </a></p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Each ELF file has an elf header followed by data which includes:</p> <ul> <li> <p style="margin-bottom: 0in">program header table</p> </li> <li> <p style="margin-bottom: 0in">section header table</p> </li> <li> <p style="margin-bottom: 0in">data referred to by entries in the program</p> </li> </ul> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">The ELF Header is located at the beginning of the file and is used to describe the files internal organization.</p> <p style="margin-bottom: 0in">Similar to PECOFF, The Sections hold the bulk of information such as instructions, data, symbol table and location info.</p> <p style="margin-bottom: 0in"> <p> <!-- Begin Google Adsense code --> <script type="text/javascript"><!-- google_ad_client = "pub-9774791470740882"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; google_ad_channel =""; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <!-- End Google Adsense code --> </p> <p style="margin-bottom: 0in">For more information on ELF you can visit the following links, although some of them are rather dry.</p> <p style="margin-bottom: 0in"><a href="http://www.linux-foundation.org/spec/book/ELF-generic/ELF-generic/book1.html">http://www.linux-foundation.org/spec/book/ELF-generic/ELF-generic/book1.html</a></p> <p style="margin-bottom: 0in"><a href="http://www.linux-foundation.org/spec/book/ELF-IA32/ELF-IA32/book1.html">http://www.linux-foundation.org/spec/book/ELF-IA32/ELF-IA32/book1.html</a></p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Next post we will be going into Linux Memory Management</p> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=46#respond" title="Comment on The Briefest ELF intro EVAR!">Comments (0)</a> </div> </div> <div class="post"> <h2 id="post-43"><a href="http://hamsterswheel.com/techblog/?p=43" rel="bookmark">Intro to Dumpbin and PEfile for detection of Packers</a></h2> <div class="meta">Posted by Eric | <a href="http://hamsterswheel.com/techblog/?cat=11" title="View all posts in Research" rel="category">Research</a>, <a href="http://hamsterswheel.com/techblog/?cat=13" title="View all posts in Reversing" rel="category">Reversing</a>, <a href="http://hamsterswheel.com/techblog/?cat=2" title="View all posts in Windows" rel="category">Windows</a> | Tuesday 19 February 2008 11:53 am </div> <div class="storycontent"> <p><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" /><title /><meta name="GENERATOR" content="OpenOffice.org 2.0 (Linux)" /><meta name="CREATED" content="20080219;8313200" /><meta name="CHANGED" content="16010101;0" /><br /> <style> <!-- @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } --> </style> <p style="margin-bottom: 0in">After reading the last post we have some general idea’s of what we are looking for, let’s look at using dumpbin and pefile to analyze a file and see if we can determine the presence of a packer.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">For the sake of the post/ learning I have copied notepad.exe from c:\windows\system32 to a local working directory. I have downloaded the latest copy of UPX and packed the notepad file using the command line:</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">upx -9 -o noteupx.exe notepad.exe</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Doing this allows us to compare and contrast differences as we look at the packed file. This will enhance your learning experience. Afterall, if you don’t know what the data should look like, how will you be able to tell if something is not right.</p> <p style="margin-bottom: 0in">Dumpbin comes with Microsoft Visual c++. The utility provides information about the format and symbols provided in an executable, library or DLL file. A full description of dumpbin can be found here. <a href="http://support.microsoft.com/kb/177429">http://support.microsoft.com/kb/177429</a>.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Using the /imports option we can dump a list of imports for our file.</p> <p style="margin-bottom: 0in"><img src="http://www.hamsterswheel.com/blogpics/dumpbin-imp.JPG" /></p> <p style="margin-bottom: 0in">We see that our file has hardly any imports. Looking at the original file, we dump the imports and we see quite a few imports in the file. Going back to the last blog post, We know a lack of imports is indicative of a packer.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Utilizing strings from sysinternals, or on a Linux machine we find that the original PE file has quite a few strings in it. When we dump the strings on the packed file however, we find what appears to be mostly garbage data. Strings can be used simply by typing the following on the windows or Linux command line.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">strings</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Next we want to take a look at the sections. We know what sections should be there, but we don’t know if they will be. After all if the PE is Packed, there is a chance the packer changed the section names. Using Dumpbin we can obtain a list of the sections names. On the command line we simply type</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">dumpbin /HEADERS noteupx.exe</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">We recieve some output as follows:</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"><img src="http://www.hamsterswheel.com/blogpics/dumpbin-header.JPG" /></p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">We see that within out headers we have a few sections with weird names. First section that comes to our attention is UPX0, The Second is UPX1. The .RSRC section is our Resources Section and we would expect that to be there. If you are following along with the notes on PE format, you will note that the UPX sections are obviously different from what we would expect. A little googling will reveal that UPX (although we already knew that) is the name of a packer that is very common.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">With the information we just obtained we could easily go on to Tuts4you.com and grab a tutorial on unpacking UPX. Of course, for UPX there are some automated utilities that will help in unpacking the PE for you.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Moving on, let’s look at pefile. Pefile is a “multiplatorm python module to read and work with PE files….” pefile is hosted on google code and can be located at <a href="http://code.google.com/p/pefile/">http://code.google.com/p/pefile/</a>. To install pefile simply download the code, browse to the directory on the windows command line and execute the setup.py file by running python setup.py install. From here on out we will be writing some code in python to utilize pefile. All our python scripts will start with</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">import pefile</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Lets use pefile to look at our sections:</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"><code /></p> <p style="margin-bottom: 0in">import pefile</p> <p style="margin-bottom: 0in">import sys</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">file = pefile.PE(sys.argv[1])</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">for section in file.sections:</p> <p style="margin-bottom: 0in">print(section.Name, hex(section.VirtualAddress),</p> <p style="margin-bottom: 0in">hex(section.Misc_VirtualSize), section.SizeOfRawData)</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">When run against each of our PE files, we recieve a nice dump of the sections along with their virtual addresses, sizes and the size of the raw data. From the screen shot below, you can see the differences not only in the section names, but also the sizes.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"><img src="http://www.hamsterswheel.com/blogpics/pefile-sections.JPG" /></p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Next we will use pefile to take a look at the imports of the PE. Like the previous example this one is pretty simple to code up and works very well.</p> <p style="margin-bottom: 0in"><code /></p> <p style="margin-bottom: 0in">import pefile</p> <p style="margin-bottom: 0in">import sys</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">file = pefile.PE(sys.argv[1])</p> <pre>for entry in file.DIRECTORY_ENTRY_IMPORT: print entry.dll for imp in entry.imports: print 't', hex(imp.address), imp.name</pre> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in"><img src="http://www.hamsterswheel.com/blogpics/pefile-imports.JPG" /></p> <p style="margin-bottom: 0in">running our code against both executables again reveals what we figured it would. The packed PE has very few imports, the unpacked PE on the other hand scrolled off the screen with all the imports.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">This post was a rough introduction to Pefile and dumpbin. You can do a lot of cool stuff with Pefile and I recommend you go check out the wiki and play around with it a bit. <a href="http://code.google.com/p/pefile/w/list">http://code.google.com/p/pefile/w/list</a></p> <p style="margin-bottom: 0in"> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=43#respond" title="Comment on Intro to Dumpbin and PEfile for detection of Packers">Comments (0)</a> </div> </div> <div class="post"> <h2 id="post-41"><a href="http://hamsterswheel.com/techblog/?p=41" rel="bookmark">Detecting Packers</a></h2> <div class="meta">Posted by Eric | <a href="http://hamsterswheel.com/techblog/?cat=1" title="View all posts in General" rel="category">General</a>, <a href="http://hamsterswheel.com/techblog/?cat=11" title="View all posts in Research" rel="category">Research</a>, <a href="http://hamsterswheel.com/techblog/?cat=13" title="View all posts in Reversing" rel="category">Reversing</a> | Thursday 14 February 2008 12:27 am </div> <div class="storycontent"> <p><meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE" /><title /><meta content="OpenOffice.org 2.0 (Linux)" name="GENERATOR" /><meta content="20080213;21561600" 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">My wife’s been back for almost a week and I’ve gotten little computer time. Between her and the baby I’ve been trying to dish out time and my lab has not gotten the best of the time allocation! Next month I need to do a little tour back to Basics and lock down my Linux and Bsd Exploitation. After that I need to get spun up on Sparc Architecture and exploitation on Sparcs and the Solaris Operating system. Orange County DC group (DC949) is hosting the Actf again this year, but its now known as the Octf, which stands for Open Capture the Flag. If I can get out to Defcon this year I have every intention on participating. They had a short spoiler posted to their website a few day’s ago that outlined a Sparc server someone donated, so I fully expect to see sparc out there this year. Aside from that, I’ve got a few penetration test’s coming up and as it would turn out they will be outside of my world of HIPPA. This means I have to spin myself up on I.T Governance outside of the Medical field.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Back to Packers!</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">We’ve discussed what packers are, We’ve addressed how they function. Now let’s discuss how to detect them. There are manual methods of detecting, and automated analysis tools. It should be advised that the automated analysis will work often, However, they can be circumvented and should be used cautiously. Furthermore, if the Packing Algorithm is custom you are pretty much out of luck because an automated tool will not help you. Usually you will want to begin by analyzing the PE header and layout. According to Paul Craig of Security-assessment.com there are essentially four way in which we can detect the presence of a Packer.</p> <p style="margin-bottom: 0in"> <ol> <li> <p style="margin-bottom: 0in">The Import table</p> </li> <li> <p style="margin-bottom: 0in">String Table</p> </li> <li> <p style="margin-bottom: 0in">Code body</p> </li> <li> <p style="margin-bottom: 0in">Section Names</p> </li> </ol> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">The Import Table.</p> <p style="margin-bottom: 0in">Recall from previous notes that the import table is a table of external functions that an application wants to use. Normally even a small executable will contain a good amount of imports depending on its functionality. If you notice only a few imports Its time to be suspect. Furthermore, if those few imports are functions that locate other functions, or depend on other functions… Chances are you have a packer.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">String Table.</p> <p style="margin-bottom: 0in">As you will recall a string table is a table of commonly used strings in the application. When you use strings on an executable you usually get a pretty decent list. However, if this list goes missing, is corrupt or is encrypted it becomes apparent that a packer has been used. Beyond that, Packers also tend to add their own entries into a string table.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Code Body.</p> <p style="margin-bottom: 0in">When you disassemble a normal application, even a small one the code body can be big. If you disassemble a binary and the code body is a lot smaller than normal Theres a chance you’ve been packed. The disassembly would only show the PE Packer stub routine, you may see a large amount of data in the executable, but since it’s packed you don’t see it as code.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">Section Names.</p> <p style="margin-bottom: 0in">Compilers and linkers have standard naming conventions for each code and data section. Once you become familiar with these naming conventions it becomes easy to spot something out of the ordinary. Also, in the case of UPX the Packer will nicely Label itself in a section name.</p> <p style="margin-bottom: 0in"> <p style="margin-bottom: 0in">There are a few Automatic detectors out there, but I’ve already outlined their shortcomings. It’s advised to use caution with them. Peid is one, GT2 is another. I’ve got other stuff to do so feel free to google and comment on any others you know of or find!</p> <p style="margin-bottom: 0in"> </div> <div class="feedback"> <a href="http://hamsterswheel.com/techblog/?p=41#respond" title="Comment on Detecting Packers">Comments (0)</a> </div> </div> <a href="http://hamsterswheel.com/techblog/?cat=11&paged=2" >Next Page »</a> </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>