30.12.2012 Views

ttern - Free Web Hosting with PHP, MySQL and cPanel

ttern - Free Web Hosting with PHP, MySQL and cPanel

ttern - Free Web Hosting with PHP, MySQL and cPanel

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Editor in Chief: Grzegorz Tabaka<br />

grzegorz.tabaka@hakin9.org<br />

Managing Editor: Natalia Boniewicz<br />

natalia.boniewicz@hakin9.org<br />

team<br />

Editorial Advisory Board: Rebecca Wynn, Matt Jonkman,<br />

Donald Iverson, Michael Munt, Gary S. Milefsky, Julian Evans,<br />

Aby Rao<br />

DTP: Ireneusz Pogroszewski<br />

Art Director: Ireneusz Pogroszewski<br />

ireneusz.pogroszewski@hakin9.org<br />

Proofreaders: Michael Munt, Rebecca Wynn, Elliott Bujan, Bob<br />

Folden, Steve Hodge, Jonathan Edwards, Steven Atcheson<br />

Top Betatesters: Nick Baronian, Rebecca Wynn, Rodrigo Rubira<br />

Branco, Chris Brereton, Gerardo Iglesias Galvan, Jeff rey Smith, Aby<br />

Rao, Jason Duke, Carlos Alaya, Joseph Werns, Shane Hartman,<br />

Jose L. Herrera<br />

Special Thanks to the Beta testers <strong>and</strong> Proofreaders who helped<br />

us <strong>with</strong> this issue. Without their assistance there would not be a<br />

Hakin9 Expoiting Software magazine.<br />

Senior Consultant/Publisher: Paweł Marciniak<br />

CEO: Ewa Dudzic<br />

ewa.dudzic@hakin9.org<br />

Production Director: Andrzej Kuca<br />

<strong>and</strong>rzej.kuca@hakin9.org<br />

Publisher: Software Press Sp. z o.o. SK<br />

02-682 Warszawa, ul. Bokserska 1<br />

Phone: 1 917 338 3631<br />

www.hakin9.org/en<br />

Whilst every effort has been made to ensure the high quality of<br />

the magazine, the editors make no warranty, express or implied,<br />

concerning the results of content usage.<br />

All trade marks presented in the magazine were used only for<br />

informative purposes.<br />

All rights to trade marks presented in the magazine are<br />

reserved by the companies which own them.<br />

To create graphs <strong>and</strong> diagrams we used program<br />

by<br />

Mathematical formulas created by Design Science MathType<br />

DISCLAIMER!<br />

The techniques described in our articles may only<br />

be used in private, local networks. The editors<br />

hold no responsibility for misuse of the presented<br />

techniques or consequent data loss.<br />

03/2011 (03)<br />

Dear Readers,<br />

A password is a secret word or string of characters that is used<br />

for authentication, to prove identity or gain access to a resource.<br />

It should be kept in secret from those not allowed access. In spite<br />

of this your computer <strong>and</strong> files are at risk even if you have a user<br />

account password. In this issue you will learn how to protect your<br />

files.<br />

I greatly encourage you to read A Quick „H<strong>and</strong>s On” Introduction to<br />

Packing of Alain Schneider. In the fascinating article Direct Object<br />

Reference, or How a Toddler Can Hack Your <strong>Web</strong> ApplicationIn Nick<br />

Nikiforakis will investigate one type of <strong>Web</strong> application vulnerability,<br />

namely Direct Object Reference, which occurs when an identifier,<br />

used in the internal implementation of a <strong>Web</strong> application, is exposed<br />

to users. From the article Malware Analysis for Windows Sostems<br />

Administrators Using Sysinternal Tools you will learn about the two<br />

types of malware analysis, static <strong>and</strong> dynamic as well as the tools<br />

<strong>and</strong> processes to perform the analysis. If you want to know how<br />

to deploy software <strong>and</strong> its payload into target computers running<br />

supported operating systems <strong>and</strong> underst<strong>and</strong> policies <strong>and</strong> settings<br />

that already exist to block end users on a corporate, government or<br />

educational network from being able to use this software, read the<br />

article Password, what password? Of Christian Mergiliano. And do<br />

not miss the article The Logic Behind Application. Logic Defects of<br />

Rafal Los, especially if you are curious how the hackers have been<br />

exploiting weaknesses in web applications at an alarmingly high<br />

rate. You will see how a hacker maps out the application flow <strong>and</strong><br />

determine vulnerabilities before... he comes to exploit application<br />

logic. You will also underst<strong>and</strong> how logic defects in an application<br />

can cause serious business challenges. And finally, to know why<br />

traditional security projects show a high degree of falling back read<br />

the interview <strong>with</strong> Jan van Bon.<br />

We wish you nice reading!<br />

Natalia Boniewicz<br />

& Hakin9 Team<br />

4<br />

03/2011


CONTENTS<br />

ATTACK PATTERN<br />

8 Malware Analisys for Windows Systems<br />

Administrators Using Sysinternal Tools<br />

By Dennis Distler<br />

Today administrators deal <strong>with</strong> malware infections<br />

almost daily. Often malware is customized for specific<br />

organizations, departments in an organization, <strong>and</strong> even<br />

individuals in the organization.<br />

This type of malware typically is not identified by antivirus<br />

products, <strong>and</strong> it is up to the administrator to identify<br />

the malware. Locating malware purposely designed not<br />

to be detected is difficult, but detection is not impossible.<br />

To identify malware, systems administrators must be<br />

able to perform malware analysis. To detect malware<br />

administrators should underst<strong>and</strong> the types of analysis,<br />

the process <strong>and</strong> tools used to accomplish malware<br />

analysis. In this article learn about the two types of<br />

analysis, static <strong>and</strong> dynamic as well as the tools <strong>and</strong><br />

processes to perform the analysis.<br />

14 Direct Object Reference or, How a<br />

Toddler Can Hack Your <strong>Web</strong> Application<br />

By Nick Nikiforakis<br />

There is no point in denying that everyday software<br />

is steadily moving from desktop applications to <strong>Web</strong><br />

applications. When you can check your mail, play games,<br />

create documents <strong>and</strong> file your tax report <strong>with</strong>out ever<br />

leaving your browser, then you are indeed a citizen of the<br />

<strong>Web</strong>. In this era, many miscreants have changed their<br />

game. It’s easier for them to impersonate you or steal<br />

your private data from a vulnerable <strong>Web</strong> application than<br />

to take control of the Extended Instruction Pointer (EIP)<br />

register of your CPU.<br />

In this article we will investigate one type of <strong>Web</strong><br />

application vulnerability, namely Direct Object Reference.<br />

A Direct Object Reference occurs when an identifier,<br />

used in the internal implementation of a <strong>Web</strong> application,<br />

is exposed to users. When this is done insecurely, it can<br />

lead to a lot of trouble...<br />

20 The Logic Behind Application. Logic<br />

Defects.<br />

By Rafal Los<br />

It’s no secret that web applications are at the center of the<br />

ongoing conflict between malicious hackers, <strong>and</strong> those<br />

defending the applications. As more <strong>and</strong> more critical<br />

business functions migrate to an Internet presence, web<br />

applications play an extremely vital role in business.<br />

Hackers know this well, <strong>and</strong> have been exploiting<br />

weaknesses in web applications at an alarmingly high<br />

rate. You will see how a hacker maps out the application<br />

flow <strong>and</strong> determine vulnerabilities before... he comes<br />

to exploit application logic. You will also underst<strong>and</strong><br />

how logic defects in an application can cause serious<br />

business challenges.<br />

DEFENSE PATTERN<br />

22 Password, What Password?<br />

By Christian Mergiliano<br />

If you have a computer <strong>with</strong> a non-encrypted hard drive<br />

<strong>and</strong> have not disabled other media devices from booting<br />

before your hard drive or have not password protected<br />

your BIOS listen up! Your computer <strong>and</strong> files are at risk<br />

even if you have a user account password.<br />

Christian explains how to deploy this software <strong>and</strong><br />

its payload into target computers running supported<br />

operating systems. You will underst<strong>and</strong> policies <strong>and</strong><br />

settings that already exist to block end users on a<br />

corporate, government or educational network from<br />

being able to use this software. And you will see what<br />

you, a home user, can do to keep your computer safe<br />

from this attack.<br />

6 03/2011


CONTENTS<br />

REVERSE ENGINEERING<br />

28 A Quick „H<strong>and</strong>s On” Introduction to Packing<br />

By Alain Schneider<br />

On Windows systems, programs are usually available in the PE file format<br />

<strong>with</strong> the EXE extension. Although this file format is quite complex, it is now<br />

well documented, so underst<strong>and</strong>ing how it is globally supposed to work is<br />

pretty easy <strong>and</strong> you can find a lot of programs designed to open/analyze/<br />

modify PE executables.<br />

Those which are designed to modify PE files are often called packers. In<br />

this article we will learn how to write one of them.<br />

INTERVIEW<br />

36 Interview <strong>with</strong> Jan van Bon<br />

By Exploiting Software Team<br />

Traditional security projects show a high degree of falling back specifically<br />

because they are not embedded in a well-functioning management system<br />

– says Jan van Bon. Creating a solid <strong>and</strong> practical architecture under your<br />

IT management approach can greatly reduce the cost of improving quality,<br />

<strong>and</strong> it can speed up your projects. An integrated approach requires a<br />

simple <strong>and</strong> straightforward method that is easy to underst<strong>and</strong>, supported<br />

by available tools in the market, <strong>and</strong> accepted by many providers. This<br />

kind of approach requires thorough knowledge <strong>and</strong> sincere dedication.<br />

As <strong>with</strong> many other initiatives in the field of IT Service Management,<br />

the Netherl<strong>and</strong>s have again produced a fascinating new approach, <strong>with</strong><br />

promising results for IT Security projects. Jan will share his thoughts on<br />

risk managment <strong>and</strong> IT Security development.<br />

www.hakin9.org/en 7


8<br />

ATTACK PATTERN<br />

Malware Analysis<br />

for Widows Systems Administrators Using Sysinternal<br />

Tools<br />

Today administrators deal <strong>with</strong> malware infections almost daily.<br />

Often malware is customized for specific organizations, departments<br />

in an organization, <strong>and</strong> even individuals in the organization.<br />

This type of malware typically is not identified by<br />

anti-virus products, <strong>and</strong> it’s up to the administrator<br />

to identify the malware. Locating malware<br />

purposely designed not to be detected is difficult,<br />

but detection is not impossible. To identify malware,<br />

systems administrators must be able to perform malware<br />

analysis.<br />

Introduction to Behavioral Malware Analysis<br />

for System Administrators<br />

To detect malware administrators should underst<strong>and</strong><br />

the types of analysis, the process <strong>and</strong> tools used to<br />

accomplish malware analysis. In this article learn about<br />

the two types of analysis, static <strong>and</strong> dynamic as well as<br />

the tools <strong>and</strong> processes to perform the analysis.<br />

Malware analysis is typically performed in a<br />

controlled <strong>and</strong> isolated lab. In the malware lab,<br />

analysts perform both code <strong>and</strong> behavioral analysis.<br />

The type of analysis performed first is completely up<br />

to the analyst, <strong>and</strong> is typically based on the analyst<br />

personal preference.<br />

Code analysis typically starts <strong>with</strong> looking for<br />

embedded strings in the malware. Next, the malware<br />

is examined <strong>with</strong> a disassembler, which provides the<br />

analyst <strong>with</strong> the malwares assembly instructions. The<br />

final stage of code analysis is using a debugger to<br />

analyze the malware as it’s running.<br />

The behavioral analysis process begins by taking<br />

baselines of a system. This baseline can include<br />

services/process’s running on the system, registry<br />

settings (for Microsoft operating systems), users <strong>and</strong><br />

groups, file hashes, <strong>and</strong> network information. After<br />

generating the baseline, the malware is executed on<br />

the system. Once the malware has executed for a while,<br />

the same information is collected again, then compared<br />

to the baseline. After analyzing the differences, its<br />

not uncommon to make changes to the lab to get the<br />

malware to reveal more details about how the malware<br />

functions. These changes can include providing files,<br />

web servers, IRC servers, etc.<br />

However, for administrators responding to a<br />

production system infected <strong>with</strong> malware often don’t<br />

have the luxury of having baselines to compare against.<br />

Even if the organization has baselines, typically the<br />

baseline is not current, especially <strong>with</strong> workstations.<br />

Working <strong>with</strong>out current baselines makes it difficult<br />

for the administrator to determine what the malware<br />

installed or what some other software installed. To<br />

assist administrators can use tools such as GMER<br />

(http://www.gmer.net) to assist in help identify malware.<br />

GMER is written to detect rootkits, but can be used<br />

to locate artifacts of malware, but will not locate all<br />

malware. Unfortunately, malware authors use the same<br />

tools as malware analysts to improve the chance their<br />

malware goes undetected. When automated tools fail<br />

to detect, it’s up to the system administrator to detect<br />

the malware.<br />

When analyzing a system infected <strong>with</strong> malware,<br />

it’s best not to trust the operating system. It’s not<br />

uncommon to have operating systems comm<strong>and</strong>s <strong>and</strong><br />

tools modified by malware to help evade detection.<br />

It’s best for administrators to bring their own tools<br />

that can’t be modified by malware. The tools used for<br />

analysis can be used to replace most system tools for<br />

live analysis. A word of caution of when performing<br />

behavioral malware analysis, if system is going to have<br />

any forensics performed on the system, the forensics<br />

image acquisition should be performed first.<br />

When forced to manually perform behavioral<br />

analysis there are several helpful tools used to<br />

identify the malware. All tools used in this article<br />

are free of charge <strong>and</strong> can be downloaded from<br />

03/2011


Malware Analysis for Widows Systems Administrators Using Sysinternal Tools<br />

http://www.sysinternals.com. The following list of tools<br />

will be used to help identify the malware:<br />

• TCPView<br />

• Process Explorer<br />

• Process Monitor<br />

• H<strong>and</strong>les<br />

• ListDLL<br />

• Autoruns<br />

After downloading the tools, they should be burned<br />

to a CD/DVD or copied to write protected USB drive.<br />

Burning the tools to a CD or using a write protected<br />

USB drive ensures the malware does not modify these<br />

tools.<br />

The behavioral process typically begins by examining<br />

outbound connections from the host. The objective<br />

is to identify which process on the system is actually<br />

generating the outbound traffic. Often times this identified<br />

process is the malicious process, but be aware this could<br />

also just be a proxy for another process or another host.<br />

The next step is to examine the suspicious process.<br />

The examination of this process includes reviewing its<br />

network connections, <strong>and</strong> strings in the process. The<br />

strings examination includes the process image itself<br />

<strong>and</strong> strings in memory. Examining strings in memory<br />

can provide more information, especially if the malware<br />

is a packed executable.<br />

Next, any file system or registry changes the malware<br />

makes while running is examined. Additionally any<br />

files, registry keys <strong>and</strong> dynamic link library (DLL’s) the<br />

malware is using will be examined. When examining<br />

DLL’s it will be critical to being able to identify DLL’s<br />

the malware brought <strong>with</strong> it or DLL’s already on the<br />

system.<br />

The last stage in the behavioral analysis process is<br />

identifying how the malware loads. It is possible during<br />

this stage to disable malware from running but leaving<br />

Figure 1. TCPView displaying the process generating the traffic<br />

it on the system. Ideally though the malware should be<br />

removed from the system.<br />

For this article a well-known <strong>and</strong> understood malware<br />

sample known as srvcp.exe was chosen to demonstrate<br />

the behavioral analysis process <strong>and</strong> tools. For this<br />

article it’s assumed an alert was generated detecting<br />

outbound IRC traffic to irc.mcs.net from a host <strong>with</strong> the<br />

IP address of 192.168.183.128.<br />

Typically malware is detected by the network traffic<br />

it generates. The level of detail captured will vary<br />

depending on how the information was obtained. The<br />

important information will be the source address <strong>and</strong><br />

port, <strong>and</strong> the destination address <strong>and</strong> port.<br />

With the system that’s generating the traffic identified<br />

(192.168.183.128), the first tool used will be TCPView<br />

(All tools used in this article should be run <strong>with</strong><br />

Administrator privileges). TCPView can be thought of as<br />

a replacement to netstat <strong>with</strong> a graphical user interface<br />

(GUI). TCPView can be launched using two different<br />

methods. The first method is the st<strong>and</strong>ard double click<br />

on the executable <strong>and</strong> the second method is to run from<br />

the comm<strong>and</strong> line. Regardless of which method used<br />

to launch TCPView, the End User License Agreement<br />

(EULA) will be presented.<br />

Running any Microsoft (Sysinternals) tools such as<br />

TCPView from the comm<strong>and</strong> line allows the tools to<br />

be scripted to automate data collection. When running<br />

these tools from the comm<strong>and</strong> line or in a script, use<br />

the /accepteula flag to suppress the EULA from being<br />

displayed.<br />

After starting TCPView the administrator must<br />

determine if name resolution must be enabled. Some<br />

malware uses host name’s, other malware uses<br />

hardcoded IP addresses. There are two options to<br />

enable or disable name resolution. The first is using<br />

[CTRL-R] keyboard short cut or selecting from the<br />

menu bar Options, Resolve Addresses.<br />

With TCPView started several columns are of interest<br />

to the administrator. The administrator is trying to identify<br />

what is communicating on TCP 6667 to irc.mcs.net. The<br />

first field to examine will be the Remote Address field.<br />

This field will show all remote systems the system is<br />

connected to <strong>and</strong> on busy systems’ the connection<br />

counts can be in the thous<strong>and</strong>s. After identifying the<br />

Remote Address, look at the Remote Port field to<br />

identify the destination port. Next the administrator will<br />

verify the protocol in use by examining the Protocol<br />

column. Once the remote address, remote port, <strong>and</strong><br />

protocol is identified, the two columns labeled Process<br />

<strong>and</strong> PID (Process Identification) show which process is<br />

generating the network traffic. As seen in Figure 1, the<br />

process generating the traffic is srvcp.exe <strong>with</strong> a PID of<br />

1024.<br />

In this example the process name looks somewhat<br />

like a legitimate service, but a quick Google search<br />

www.hakin9.org/en 9


10<br />

shows this process is actually malware. However,<br />

often time Google will not be so helpful or the malware<br />

author will infect a windows process such as lsass.exe<br />

or services.exe to hide the malware. When performing<br />

analysis on this type of malware, the same process’s<br />

<strong>and</strong> tools are used for analysis.<br />

With srvcp.exe process identified, the next steps<br />

will be gathering more information about the malicious<br />

process. The next tool used is Process Explorer, another<br />

tool from Microsoft (Sysinternals). Process Explorer is<br />

a powerful tool that can identify running process <strong>and</strong><br />

resources used by that process. Additionally Process<br />

Explorer can be used to identify h<strong>and</strong>les <strong>and</strong> DLL’s<br />

used by processes, as well as strings in the process<br />

image <strong>and</strong> in the memory used by the process. Finally<br />

Process Explorer can be used to replace the Windows<br />

task manager. To start Process Explorer double click<br />

processexplorer.exe or on the comm<strong>and</strong> line enter in<br />

processexplorer.exe.<br />

With Process Explorer started there are two columns<br />

of interest to the administrator, process <strong>and</strong> PID. With<br />

the process name <strong>and</strong> PID identified by TCPView,<br />

locate the process srvcp.exe <strong>and</strong> it’s corresponding<br />

PID of 1024. With the process identified, right click the<br />

process <strong>and</strong> click properties. This will bring up a dialog<br />

box <strong>with</strong> several tabs. Each of these tabs provide a<br />

different piece of information about the process. The<br />

two tabs of interest for behavioral analysis are the TCP/<br />

IP tab <strong>and</strong> the strings tab.<br />

The first tab to investigate is the TCP/IP tab. When<br />

investigating this tab, the administrator is confirming<br />

ATTACK PATTERN<br />

this is the process to be investigated. Because this<br />

process is talking <strong>with</strong> irc.mcs.net on TCP 6667, it can<br />

be confirmed this is the correct process as seen in<br />

Figure 2. If the malware is not using name resolution<br />

<strong>and</strong> only IP address’s are known, uncheck the Resolve<br />

Address’s. Additionally, Process Explorer can be used<br />

<strong>with</strong>out TCPView to identify suspicious traffic, but in<br />

the author’s experience, TCPView speeds the process<br />

up.<br />

After confirmation this is the correct process, next<br />

investigate the strings tab. The strings tab will display<br />

strings found in the process. The strings tab does<br />

have one option of note, the location of which process<br />

to extract the strings from. The options are either<br />

the process image (the executable) or strings from<br />

memory. The administrator should select both options<br />

<strong>and</strong> making notes about interesting found strings. Items<br />

of interest could include file locations, registry settings,<br />

hostnames, IP addresses, etc. that can provide<br />

more insight into the behavior of malware as seen in<br />

Figure 3.<br />

Next Microsoft (Sysinternals) Process Monitor is<br />

used to identify file, registry, <strong>and</strong> process/thread<br />

activity. Process Monitor is actually a combination of<br />

two older Sysinternal tools, RegMon <strong>and</strong> FileMon.<br />

Process Monitor is a very powerful tool, but can lead to<br />

information overload very quickly. With Process Monitor<br />

any type of change will be recorded. This means<br />

anytime a file is read, a registry key is queried, a TCP<br />

packet is sent or many other types operations occurred,<br />

there all captured by Process Monitor.<br />

Figure 2. Process Explorer showing network traffic Figure 3. Strings in Memory identi�ed by Process Explorer<br />

03/2011


Malware Analysis for Widows Systems Administrators Using Sysinternal Tools<br />

Figure 4. Process Monitor displaying network activity<br />

To help <strong>with</strong> this information overflow, there are two<br />

options that assist. The first is turning Process Monitors<br />

event capture ability on <strong>and</strong> off. However if this is done<br />

it’s possible to miss critical events. The option is to<br />

use Process Monitors built in filter. The built-in filtering<br />

has many options, but the most common ones used in<br />

malware analysis is Process Name or PID.<br />

During the analysis of srvcp.exe the only activity<br />

identified by Process Monitor is srvcp.exe making a<br />

TCP connection to irc.mcs.net every three seconds as<br />

seen in Figure 4.<br />

In this example only packets sent by srvcp.exe to<br />

irc.mcs.net is captured. However when examining other<br />

malware there will be many different operations. When<br />

seeing these operations, its important to record the<br />

operation type <strong>and</strong> path it occurred at. This information<br />

can be used to help clean up the malware.<br />

With the process identified, its important to<br />

determine other files <strong>and</strong> registry keys used by the<br />

malware. There are two excellent tools from Microsoft<br />

(Sysinternals), H<strong>and</strong>les <strong>and</strong> ListDLL’s that will be used<br />

by administrators to identify other files, registry keys<br />

<strong>and</strong> DLL’s used by the malware.<br />

The first tool H<strong>and</strong>les.exe, a comm<strong>and</strong> line only<br />

tool showing any open h<strong>and</strong>le for any process on<br />

the system when H<strong>and</strong>les.exe is ran. It should be<br />

noted, if the process has opened <strong>and</strong> closed a h<strong>and</strong>le<br />

before H<strong>and</strong>les.exe is ran, this h<strong>and</strong>le will not be<br />

seen. H<strong>and</strong>les.exe is useful when examining malware<br />

that has hooked itself into Windows process such as<br />

services.exe or lsass.exe. When using H<strong>and</strong>les.exe<br />

Figure 5. H<strong>and</strong>les.exe ¬–p 1024 –a output showing �les, threads<br />

<strong>and</strong> registry keys used by srvcp.exe malware<br />

there are two options typically used during malware<br />

analysis. The first option is the –a flag that dumps all<br />

h<strong>and</strong>le information. The second option is the –d PID#<br />

that displays the information for that specific process. If<br />

h<strong>and</strong>les.exe is ran <strong>with</strong> out any options all files in use by<br />

all process’s will be displayed.<br />

Examining srvcp.exe <strong>with</strong> h<strong>and</strong>les.exe –a –p 1024<br />

shows a couple of registry keys in use, the directory, the<br />

files <strong>and</strong> threads used by the malware. The output from<br />

h<strong>and</strong>les.exe –a –p 1024 can be seen in figure 5.<br />

Next the administrator should determine what DLL’s<br />

are in use by the malware. The malware may choose<br />

to use DLL’s on system, or it’s own DLL’s. There are a<br />

few reasons malware install’s it’s own DLL’s, such as no<br />

DLL’s on the system provide the required functionality,<br />

or another reason is because the malware will use<br />

hooked versions of installed DLL’s to hide the malware.<br />

To find the DLL’s used by the malware, Listdlls.exe is<br />

used.<br />

Listdlls.exe has several options, but the two most<br />

common flags are the –v <strong>and</strong> ¬–d flags. The –v flag is<br />

used to display version detailed information about DLL’s<br />

used by the malware. The –d DLLNAME flag display’s all<br />

process using the specified DLL.<br />

To determine which DLL’s are being used by a<br />

process, run the listdlls.exe #PROCESSNAME, in this example<br />

its srvcp.exe. The output from running listdlls.exe<br />

srvcp.exe is seen in Figure 6.<br />

The output in Figure 6 displays the base address, file<br />

size, <strong>and</strong> the DLL being used by srvcp.exe.<br />

With a list of DLL’s used by the malware, the<br />

administrator should next run listdlls.exe –d DLLNAME.<br />

This comm<strong>and</strong> is used to identify other process using<br />

the same DLL. This is useful to help identify other<br />

process’s using the DLL. For example if malware is<br />

using kernel32.dll its important to determine if the<br />

malware is using a legitimate version or one modified to<br />

help hide the malware from detection.<br />

Next, the administrator should run listdlls.exe –<br />

v srvcp.exe. The output from this comm<strong>and</strong> shows<br />

Figure 6. Listdlls.exe output displaying DLL’s used by srvcp.exe<br />

www.hakin9.org/en 11


12<br />

Figure 7. Listdlls.exe –v output<br />

information about the version of the executable as seen<br />

in Figure 7. There are several fields in the output, <strong>and</strong><br />

most legitimate DLL’s will have information for each<br />

field. A process that has N/A for most fields should be<br />

considered suspicious, but does not guarantee it’s a<br />

malicious process. In the Verified field, srvcp.exe has<br />

Invalid Signature in it, however this often occurs for<br />

legitimate processes so this field by itself should not be<br />

used as an indicator of malicious software.<br />

With the malicious process identified, files <strong>and</strong> registry<br />

keys used by the malicious process, the last step is to<br />

identify how the malware start’s up. To find out how the<br />

malware starts Autoruns will be used.<br />

Autoruns, the GUI version or Autorunsc, the<br />

comm<strong>and</strong> line version is used to show everything that<br />

starts on the system. Autoruns has multiple tabs, but for<br />

malware analysis the Everything tab as seen in Figure<br />

8 is sufficient. It should be noted the author added<br />

srvcp.exe to startup for this article <strong>and</strong> is not the normal<br />

behavior of this malware.<br />

Next to each process is a checkbox that determines<br />

if the process will start on the next reboot. If the box is<br />

unchecked, the process will not start when the system<br />

boots. The administrator can reboot the system, <strong>and</strong><br />

repeat the analysis to confirm the malware is not<br />

starting on reboot.<br />

With the malware analysis completed the administrator<br />

can use the information to perform several tasks. The<br />

two most critical tasks to performed using the information<br />

collected is to clean the infected systems <strong>and</strong> use the<br />

Figure 8. Output of Autoruns Everything tab<br />

ATTACK PATTERN<br />

information to detect other systems infected <strong>with</strong> the<br />

same malware. Another task, depending on the contract<br />

<strong>with</strong> the organizations Anti-Virus vendor, is submit the<br />

malware for analysis <strong>and</strong> signature creation.<br />

When performing malware clean up, a systematic<br />

clean up must be performed. The best way to perform<br />

clean up on multiple infected systems is create a script<br />

to clean up. This script can be deployed via Group<br />

Policy or use the Microsoft tool PSExec to run the script<br />

remotely. Before deploying the script, especially scripts<br />

replacing or deleting critical operating system files such<br />

as kernel32.dll, should test the script thoroughly. If the<br />

malware has infected critical OS files it’s recommended<br />

the system be rebuilt.<br />

Additionally using information gathered during the<br />

malware analysis can be used to detect other systems<br />

infected <strong>with</strong> the same malware. The information to be<br />

used can include the executable, files such as DLL’s,<br />

or registry keys. These can be detected by a systems<br />

management system such as Microsoft Systems<br />

Center Configuration Manager (SCCM), if they are<br />

configured for software inventory. Another option is to<br />

create a script to detect the presence of the malware<br />

<strong>and</strong> notify the administrator of systems infected <strong>with</strong><br />

the malware.<br />

With an underst<strong>and</strong>ing the process <strong>and</strong> tools,<br />

the reader should now be able perform dynamic<br />

malware analysis. Although dynamic analysis is not<br />

comprehensive, it should be sufficient for the reader to<br />

identify malware on infected systems. Administrators<br />

can use the information learned from the dynamic<br />

analysis to clean systems <strong>and</strong> detect other systems<br />

infected <strong>with</strong> malware.<br />

DENNIS DISTLER<br />

Dennis Distler (@securitylifer) is an information security<br />

professional <strong>with</strong> over 10 years experience. In the past<br />

he has held positions in systems administration, network<br />

engineering, network security, security analysis, security<br />

engineering <strong>and</strong> security consulting in organizations as<br />

small as 6 employees to global enterprises <strong>with</strong> 70,000 plus<br />

employees. Currently Dennis manages the Information<br />

Security Operations <strong>and</strong> Engineering Team for a Fortune 500<br />

company. His many responsibilities include forensics, incident<br />

response <strong>and</strong> malware analysis. Dennis holds multiple<br />

security certi�cations including the GIAC Security Expert<br />

(GSE), the Certi�ed Information Systems Security Professional<br />

(CISSP) <strong>and</strong> Cisco Certi�ed Security Professional (CSSP).<br />

03/2011


WHAT IS A GOOD FUZZING TOOL?<br />

Fuzz testing is the most efficient method for discovering both known <strong>and</strong> unknown vulnerabilities in software. It is<br />

based on sending anomalous (invalid or unexpected) data to the test target - the same method that is used by hackers<br />

<strong>and</strong> security researchers when they look for weaknesses to exploit. There are no false positives, if the anomalous<br />

data causes abnormal reaction such as a crash in the target software, then you have found a critical security flaw.<br />

In this article, we will highlight the most important requirements in a fuzzing tool <strong>and</strong> also look at the most common<br />

mistakes people make <strong>with</strong> fuzzing.<br />

PROPERTIES OF A GOOD<br />

FUZZING TOOL<br />

There are abundance of fuzzing tools available. How to distinguish<br />

a good fuzzer, what are the qualities that a fuzzing tool<br />

should have?<br />

Model-based test suites: R<strong>and</strong>om fuzzing will certainly give you<br />

some results, but to really target the areas that are most at risk, the<br />

test cases need to be based on actual protocol models. This results<br />

in huge improvement in test coverage <strong>and</strong> reduction in test execution<br />

time.<br />

Easy to use: Most fuzzers are built for security experts, but in QA<br />

you cannot expect that all testers underst<strong>and</strong> what buffer<br />

overflows are. Fuzzing tool must come <strong>with</strong> all the security knowhow<br />

built-in, so that testers only need the domain expertise from<br />

the target system to execute tests.<br />

Automated: Creating fuzz test cases manually is a time-consuming<br />

<strong>and</strong> difficult task. A good fuzzer will create test cases automatically.<br />

Automation is also critical when integrating fuzzing into regression<br />

testing <strong>and</strong> bug reporting frameworks.<br />

Test coverage: Better test coverage means more discovered<br />

vulnerabilities. Fuzzer coverage must be measurable in two<br />

aspects: specification coverage <strong>and</strong> anomaly coverage.<br />

Scalable: Time is almost always an issue when it comes to testing.<br />

User must also have control on the fuzzing parameters such as test<br />

coverage. In QA you rarely have much time for testing, <strong>and</strong> therefore<br />

need to run tests fast. Sometimes you can use more time in testing,<br />

<strong>and</strong> can select other test completion criteria.<br />

���������������������<br />

���������������<br />

�������������������������<br />

���������������������<br />

�������������������������<br />

�������������������������������<br />

��������������������<br />

Documented test cases: When a bug is found, it needs to be<br />

documented for your internal developers or for vulnerability<br />

management towards third party developers. When there are<br />

billions of test cases, automated documentation is the only possible<br />

solution.<br />

Remediation: All found issues must be reproduced in order to fix<br />

them. Network recording (PCAP) <strong>and</strong> automated reproduction<br />

packages help you in delivering the exact test setup to the developers<br />

so that they can start developing a fix to the found issues.<br />

MOST COMMON MISTAKES IN<br />

FUZZING<br />

Not maintaining proprietary test scripts: Proprietary tests<br />

scripts are not rewritten even though the communication interfaces<br />

change or the fuzzing platform becomes outdated <strong>and</strong> unsupported.<br />

Ticking off the fuzzing check-box: If the requirement for testers<br />

is to do fuzzing, they almost always choose the quick <strong>and</strong> dirty<br />

solution. This is almost always r<strong>and</strong>om fuzzing. Test requirements<br />

should focus on coverage metrics to ensure that testing aims to<br />

find most flaws in software.<br />

Using hardware test beds: Appliance based fuzzing tools<br />

become outdated really fast, <strong>and</strong> the speed requirements for the<br />

hardware increases each year. Software-based fuzzers are scalable<br />

in performance, <strong>and</strong> can easily travel <strong>with</strong> you where testing is<br />

needed, <strong>and</strong> are not locked to a physical test lab.<br />

Unprepared for cloud: A fixed location for fuzz-testing makes it<br />

hard for people to collaborate <strong>and</strong> scale the tests. Be prepared for<br />

virtual setups, where you can easily copy the setup to your<br />

colleagues, or upload it to cloud setups.<br />

���������������������������������������<br />

���������������������������


14<br />

Direct Object<br />

Reference or,<br />

In this era, many miscreants have changed their<br />

game. It’s easier for them to impersonate you or steal<br />

your private data from a vulnerable <strong>Web</strong> application<br />

than to take control of the Extended Instruction Pointer<br />

(EIP) register of your CPU. The reason is simple. As<br />

a software industry, we have more experience writing<br />

native applications in C <strong>and</strong> C++ than writing <strong>Web</strong><br />

applications in <strong>PHP</strong> <strong>and</strong> JavaScript. People still write<br />

bugs in their code, but they are definitely harder to find<br />

<strong>and</strong> exploit than it was 10 years ago.<br />

In this article we will investigate one type of <strong>Web</strong><br />

application vulnerability, namely Direct Object<br />

Reference. A Direct Object Reference occurs when<br />

an identifier, used in the internal implementation of<br />

a <strong>Web</strong> application, is exposed to users. When this is<br />

done insecurely, it can lead to a lot of trouble. This<br />

vulnerability is probably one of the easiest to exploit but<br />

is so deadly <strong>and</strong> prevalent that it claims the 4th position<br />

in OWASP’s Top 10 <strong>Web</strong> Application security risks [2].<br />

Many institutions have fallen victim to it, <strong>with</strong> the most<br />

recent example of an Australian financial company<br />

which was vulnerable in a way that made it possible<br />

for anyone to access other peoples’ private financial<br />

information [1].<br />

Vulnerable <strong>Web</strong> Application<br />

In order to make explaining easier, we will use as an<br />

example a dummy <strong>Web</strong> application that allows loggedin<br />

users to send personal messages to each other. All<br />

the messages exchanged between members are stored<br />

in a specific table in an SQL database as follows: see<br />

Table 1.<br />

The message_id column contains auto-incremented<br />

values, unique for every message. The columns to<br />

<strong>and</strong> from contain the user identifiers of the sender<br />

<strong>and</strong> recipient of any given message. The title column<br />

ATTACK PATTERN<br />

How a Toddler Can Hack Your <strong>Web</strong> Application<br />

There is no point in denying that everyday software is steadily moving<br />

from desktop applications to <strong>Web</strong> applications. When you can check your<br />

mail, play games, create documents <strong>and</strong> file your tax report <strong>with</strong>out ever<br />

leaving your browser, then you are indeed a citizen of the <strong>Web</strong>.<br />

contains the title of each message <strong>and</strong> lastly the<br />

message column contains the actual message<br />

exchanged between two users. Now lets look at some<br />

of the <strong>PHP</strong> functions used by the <strong>Web</strong> application to<br />

display a user’s Inbox <strong>and</strong> allow him to read incoming<br />

messages (Listing 1).<br />

Viewing the INBOX<br />

The function get_message_titles() is responsible for<br />

providing logged-in users an overview of their Inboxes.<br />

The first thing that the function does is to check whether<br />

the user is logged-in. It does this by checking whether<br />

the superglobal $_SESSION array contains a key titled<br />

user_id. This key is set by the <strong>Web</strong> application when<br />

the user successfully logs in, <strong>and</strong> is typically a unique<br />

identifier in the Users table of the application much like<br />

the unique identifier of each message in the Messages<br />

table. We will not need that function in our discussion<br />

thus for the sake of brevity, it is not shown in Listing 1.<br />

If that key is not set, then the code redirects the user to<br />

the login page of the <strong>Web</strong> application <strong>and</strong> returns.<br />

If the user is indeed logged-in then the user’s<br />

user_id is extracted from his session. Note that the<br />

Figure 1. Table “messages” containing personal messages that<br />

users exchanged <strong>with</strong> each other<br />

Message_id From To Title Message<br />

... ... ... ... ...<br />

776 23 11 “Hey!” “Hey man!<br />

What news?<br />

777 11 25 “Foo...” “U there?”<br />

778 25 42 “No Title” “Kthnxbye!”<br />

779 23 11 “Welcome” “Welcome to our<br />

site!...”<br />

... ... ... ...<br />

03/2011


Direct Object Reference or, How a Toddler Can Hack Your <strong>Web</strong> Application<br />

user_id is passed as a parameter to the intval()<br />

function. The intval() function is a built-in function of<br />

<strong>PHP</strong> that returns the integer value of the parameter<br />

that the programmer passes to it. This function is<br />

very useful for <strong>Web</strong> application developers since it<br />

allows them to easily filter out erroneous requests<br />

or stop malicious users who attempt SQL injections<br />

or Cross-Site Scripting attacks using fields that the<br />

programmer knows should be integers. Since we will<br />

be incorporating this value to an SQL query we want<br />

to make sure that it is an integer <strong>and</strong> nothing more<br />

than that.<br />

In the next step, the <strong>Web</strong> application will query its<br />

database for all messages that have as a recipient the<br />

Listing 1. Code of vulnerable <strong>Web</strong> application<br />


16<br />

the <strong>Web</strong> application to call the function read_message().<br />

This function, like get_message_titles() first checks if the<br />

user who is requesting the reading of a message is<br />

logged-in <strong>and</strong> redirects the user if not. In the next step<br />

the value of the GET parameter named ‘id’ is retrieved<br />

(given that it exists) <strong>and</strong> filtered through the integer value<br />

function. Thus, continuing <strong>with</strong> our previous example, if<br />

the user clicks on the following link: http://example.com/<br />

read_message.php?id=776, the $message_id variable of<br />

the read_message function will contain the number 776.<br />

In the next step, the message id is used to retrieve the<br />

full message from the messages table <strong>and</strong> thus in this<br />

example case the SQL query will be the following:<br />

SELECT from,title,message FROM Messages where message_id = 776;<br />

The <strong>Web</strong> application uses the data <strong>and</strong> prints it out as<br />

HTML to the user. The user reads the message <strong>and</strong> is<br />

happy. Or maybe not ?<br />

Exploiting IT<br />

Those of you who have a security-oriented mindset [4]<br />

may feel a bit uncomfortable <strong>with</strong> the workings of the<br />

Listing 2. A simple Python script which downloads <strong>and</strong> saves the �rst 1024 messages from the<br />

import urllib<br />

import os<br />

os.mkdir("./messages")<br />

for i in range(0,1024):<br />

ATTACK PATTERN<br />

previously described <strong>Web</strong> application. Sure, they check<br />

for SQL injections whenever they use data coming-in<br />

from the user but you feel that something is not quite<br />

right...<br />

Lets look closely at the read_message() function <strong>and</strong> at<br />

the resulting SQL query. We saw that if a user clicks<br />

on link http://example.com/read_message.php?id=776,<br />

the <strong>Web</strong> application will use the id parameter to find <strong>and</strong><br />

retrieve the appropriate message. In fact, this is exactly<br />

where the vulnerability lies-- the <strong>Web</strong> application<br />

uses ONLY the id that the user provided as a means<br />

of reading a message. For the user <strong>with</strong> user_id equal<br />

to 11, the <strong>Web</strong> programmer assumed that he or she<br />

can click only on the links provided by the get_message_<br />

titles() function, thus only click on one of the following<br />

links:<br />

• http://example.com/read_message.php?id=776<br />

• http://example.com/read_message.php?id=779<br />

While it is true that only the these two links will be<br />

available in his INBOX, nothing is stopping the<br />

user from changing the id parameter to any value<br />

current_message = urllib.urlopen("http://example.com/read_message.php?id=%d" % i)<br />

out_file = open("./messages/%d.txt" % i,"w")<br />

out_file.write(current_message.read())<br />

out_file.close()<br />

Listing 3. Adding authorization checks to the vulnerable SQL query<br />

<br />

$result = mysql_query("SELECT * FROM Messages where message_id = {$message_id} <strong>and</strong> to= {$user_id}");<br />

/* The rest of the code is the same <strong>with</strong> the original function*/<br />

[...]<br />

03/2011


Direct Object Reference or, How a Toddler Can Hack Your <strong>Web</strong> Application<br />

at all. Thus, the user <strong>with</strong> user _ id 11, can ask for<br />

http://example.com/read_message.php?id=778 <strong>and</strong> start<br />

reading a message that was sent to another user! Once<br />

the malicious user is convinced that the above works then<br />

just 10 lines of Python code obtains the full database of<br />

messages: Listing 2.<br />

And that’s it! So simple, that even a toddler could do<br />

it! The problem in the design of this <strong>Web</strong> application<br />

is that the user can directly reference messages in<br />

the database. If we generalize this, there is a problem<br />

when a user is allowed to directly reference objects<br />

Listing 4. Adding an extra layer of indirection to defeat DOR attacks<br />

<br />

www.hakin9.org/en 17


18<br />

ATTACK PATTERN<br />

References<br />

• “Financial company heavies researcher for reporting vulnerability” http://www.theregister.co.uk/2011/10/14/�rst_state_super_shoots_messenger/<br />

[1]<br />

• OWASP TOP 10, https://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project [2]<br />

• Nick Nikiforakis, Marco Balduzzi, Steven Van Acker, Wouter Joosen <strong>and</strong> Davide Balzarotti, “Exposing the lack of Privacy in File<br />

<strong>Hosting</strong> Services”, in the Proceedings of the 4th USENIX Workshop of Large-scale Exploits <strong>and</strong> Emergent Threats, 2011 [3]<br />

• Bruce Schneier, “The Security Mindset”, http://www.schneier.com/blog/archives/2008/03/the_security_mi_1.html [4]<br />

Authorization<br />

If a <strong>Web</strong> application knows which objects can be<br />

accessed by which users then the <strong>Web</strong> programmer<br />

can straightforwardly integrate this knowledge into<br />

the code. For example, the messages database<br />

table contains a From column (who sent any given<br />

message) <strong>and</strong> a To column (who is the recipient of any<br />

given message). Thus anytime that a user requests to<br />

read a specific message, the programmer can verify<br />

whether the user was the recipient or the sender of<br />

that message. Since we have limited ourselves in the<br />

above examples to only the user’s Inbox, the SQL<br />

query in function read_message() can be modified as<br />

shown in Listing 3.<br />

As you can see in the modified lines, the SQL query<br />

now asks for all data given a specific message_id <strong>and</strong> a<br />

specific user_id. The user_id variable is out of the user’s<br />

control thus even if a malicious user changes the<br />

message_id to a different value, the SQL query will not<br />

match any row in the table <strong>and</strong> thus return no results.<br />

Additional code could be added at this point to warn<br />

the administrator that someone is attempting to access<br />

resources that are owned by another.<br />

Indirection Layer<br />

Another way of tackling this problem is to add an extra<br />

level of indirection between the references that the<br />

user sees (<strong>and</strong> is in control of) <strong>and</strong> the references used<br />

in the back-end of the application. This technique is<br />

useful when the <strong>Web</strong> application programmer wishes<br />

to hide the implementation details from users. It is<br />

best explained <strong>with</strong> an example. In their vulnerable<br />

implementation, the function get_message_titles()<br />

reads the message identifiers from the database<br />

<strong>and</strong> gives the appropriate ones to the user. Then the<br />

���<br />

� ���<br />

� ���<br />

�<br />

��<br />

�������������� ��<br />

���<br />

Figure 1. The effects of adding an extra layer of indirection for user<br />

<strong>with</strong> user_id equal to 11<br />

function read_message() reads the message identifier<br />

from the user <strong>and</strong> queries the database directly using<br />

the user-provided value. The above protocol could<br />

be modified so that get_message_titles() transforms<br />

the database values before giving them to the user<br />

<strong>and</strong> then read_message() transforms them back before<br />

querying the database. For instance, get_message_<br />

titles() <strong>and</strong> read_message() can be re-written as shown<br />

in Listing 4.<br />

Note the changes done to the code. Before querying<br />

the database we create an array that will hold our<br />

future transformations. For every valid message_id<br />

returned by the database, a new row is created in our<br />

array, that connects an incremented counter ($array_<br />

index) <strong>with</strong> that message_id. Each link given back to the<br />

user, instead of containing the message_id, holds the<br />

array_index <strong>with</strong> which the message_id was associated.<br />

Finally, the message_array is stored in the user’s session<br />

so that it can be retrieved by read_message(). The read_<br />

message() simply reverses the procedure. What is<br />

interesting now is that the user no longer receives the<br />

actual values that the database contains but rather a<br />

set of values that correspond (at the server-side) <strong>with</strong><br />

the actual values. Thus, the user <strong>with</strong> user_id equal to<br />

11 will see the following links generated by get_message_<br />

titles():<br />

• http://example.com/read_message.php?id=0<br />

• http://example.com/read_message.php?id=1<br />

Even if a user changed the id to a new value, say<br />

id=2, there is no 2 in the $message _ array read by read _<br />

message() thus the query will fail. Figure 1, shows this<br />

procedure graphically.<br />

R<strong>and</strong>omized identi�ers<br />

There are cases in which for some reason, the <strong>Web</strong><br />

application has no knowledge of which resource<br />

belongs to which user. While certainly this is not the<br />

case in our vulnerable <strong>Web</strong> application, there are<br />

plenty of real-life examples. One of them is a File<br />

<strong>Hosting</strong> Service (FHS), a <strong>Web</strong> service which a user<br />

can utilize to exchange large files <strong>with</strong> friends. In<br />

the usual scenario, the user visits a FHS, uploads a<br />

file <strong>and</strong> receives a link to the uploaded file. The link<br />

contains a unique identifier that the service generates<br />

03/2011


<strong>and</strong> associates <strong>with</strong> that specific file. The FHS user<br />

can then proceed to share this link <strong>with</strong> any friends<br />

or colleagues. These services typically do not require<br />

users to create accounts, thus they do not have the<br />

means to remember who uploaded a file nor can<br />

they know <strong>with</strong> whom the user shared the link to<br />

the uploaded file. In these cases, the only protection<br />

available is to use r<strong>and</strong>omized identifiers so that users<br />

can not easily guess the identifiers of other resources.<br />

Thus, a FHS can assume that if one has a valid URL<br />

to a file that they should be given access to it simply<br />

because it should be impossible to recreate it [3]. In<br />

our <strong>Web</strong> application example where the message_id<br />

is an auto-incrementing number, a user can find the<br />

previous identifier simply by subtracting one from the<br />

current identifier. If instead, each message identifier<br />

was a r<strong>and</strong>om identifier, the user could no longer<br />

easily guess the identifiers of other users’ messages.<br />

The only way to figure out other valid identifiers is<br />

through brute-force methods which usually take a<br />

great amount of time, hopefully much greater than the<br />

time resources of the attacker.<br />

Conclusion<br />

The evolution of <strong>Web</strong> sites, from simple static HTML<br />

pages to full-blown dynamic <strong>Web</strong> applications marked a<br />

new era, not only for users but also for attackers. Today,<br />

any <strong>Web</strong> application programmer has to keep in mind<br />

tens of ways that a <strong>Web</strong> application can fall victim to<br />

attackers <strong>and</strong> code defensively to avoid exploitations.<br />

In this article, you learned about the Direct Object<br />

Reference vulnerability that’s easy to introduce in<br />

code <strong>and</strong> even easier to exploit. We saw an example<br />

of this vulnerability, the methods to attack it <strong>and</strong> finally<br />

three ways that a <strong>Web</strong> programmer can protect his<br />

applications from it.<br />

NICK NIKIFORAKIS<br />

Nick Nikiforakis is currently a PhD student at the Katholieke<br />

Universiteit of Leuven in Belgium. He is interested in all<br />

aspects of computer security, but he mostly focuses on <strong>Web</strong><br />

security <strong>and</strong> on the protection <strong>and</strong> exploitation of low-level<br />

vulnerabilities in native applications. In the past, Nick has<br />

presented his work in well-known academic conferences (e.g.<br />

Usenix LEET <strong>and</strong> EuroSEC), local OWASP chapters, AppSecDev<br />

<strong>and</strong> BeNeLux OWASP events as well as top European hacking<br />

conferences (CONFidence, BruCON <strong>and</strong> AthCon). All of his<br />

work can be found online at http://www.securitee.org<br />

www.hakin9.org/en 19


20<br />

ATTACK PATTERN<br />

The Logic Behind<br />

Application<br />

Logic Defects<br />

It’s no secret that web applications are at the center of the ongoing<br />

conflict between malicious hackers, <strong>and</strong> those defending the<br />

applications.<br />

As more <strong>and</strong> more critical business functions<br />

migrate to an Internet presence, web applications<br />

play an extremely vital role in business. Hackers<br />

know this well, <strong>and</strong> have been exploiting weaknesses in<br />

web applications at an alarmingly high rate.<br />

While age-old issues like SQL Injection <strong>and</strong><br />

authentication weaknesses continue to plague developers<br />

there is another class of security defects that has been<br />

flying under the radar. <strong>Web</strong> application logic defects are<br />

not new – in fact, the topic has been covered at great<br />

length by various academic <strong>and</strong> research organizations;<br />

rather, this class of issues has not received enough<br />

attention due to the prevalence of much simpler attack<br />

vectors. While hackers may not be exploiting this class of<br />

defects in high volume, they are nevertheless extremely<br />

effective <strong>and</strong> stealthy.<br />

Mapping <strong>and</strong> Hacking<br />

To attack application logic, a hacker must first underst<strong>and</strong><br />

the function of the application itself. Although a small<br />

detail, it makes this type of hacking different. While it is an<br />

advantage to know the application when looking for SQL<br />

Injection <strong>and</strong> like defects, application flow knowledge is<br />

absolutely critical when discovering <strong>and</strong> exploiting logic<br />

defects. Application flow presents a complex <strong>and</strong> often<br />

difficult attack surface, but to a hacker looking to remain<br />

undetected, it is the perfect choice.<br />

A hacker who wants to exploit application logic must first<br />

take the time to map out the application flow <strong>and</strong> determine<br />

vulnerabilities. An experienced hacker would use a visual<br />

graphing tool to draw the application page-flow <strong>and</strong> workflow<br />

to fully underst<strong>and</strong> the application. As the application is<br />

being mapped, the hacker keeps track of page-state, page<br />

parameters <strong>and</strong> available actions. Finding logic defects in<br />

applications is painstaking, requiring a hacker to look for<br />

subtle hints inside these tracked components.<br />

For example, hidden variables that only exist on certain<br />

pages <strong>with</strong>in an application, such as the login page or<br />

during privilege elevation, may point to an exploitable<br />

issue in the authentication or authorization service.<br />

Finding such parameters can be tricky, but once they<br />

are identified they can be studied for pa<strong>ttern</strong>s, or simply<br />

fed to a targeted fuzzer to discover lapses in logic.<br />

There are a number of application logic defects<br />

that are obvious to someone looking for them. For<br />

example, an application that implements a login<br />

lockout mechanism to prevent hackers from guessing<br />

passwords, but stores that information in a cookie, is<br />

easy to manipulate. The hacker simply deletes the<br />

cookie (or never accepts it) <strong>and</strong> can keep grinding<br />

away at the password mechanism. This is yet another<br />

reason to perform all critical functions away from the<br />

client – in other words, at the back-end server where<br />

the logic <strong>and</strong> environment is more likely to be under the<br />

control of the developer. Allowing logic to be stored <strong>and</strong><br />

executed on the client is like a bank building miniature<br />

cash dispensing machines that customers can take<br />

home <strong>with</strong> them. It sounds great in theory because it<br />

allows customers to not have to visit a banking center<br />

– but since the bank no longer has direct control over<br />

the physical component given to the customer to take<br />

home – this will ultimately lead to fraud.<br />

Online gaming systems are a particular target for<br />

logic tampering <strong>and</strong> hacking. An application, such as<br />

an online poker room, which allows re-submission of<br />

an action, in the form of a bet or h<strong>and</strong>, is vulnerable to<br />

being manipulated. For example, if a player feels he has<br />

a winning h<strong>and</strong> <strong>and</strong> places a large bet, then discovers<br />

a sign that their opponent also feels they have a good<br />

h<strong>and</strong>, they would be able to re-submit a previous<br />

request (typically in the form of an AJAX request) <strong>and</strong><br />

make a lower bet. An application that does not have<br />

02/2011


The Logic Behind Application Logic Defects<br />

appropriate logic controls to identify that the player has<br />

already used their turn, can be manipulated, <strong>and</strong> in fact,<br />

has been in the past.<br />

Picking on Parallel Processes<br />

Turning a developer’s time-optimization technique against<br />

the application is also a great way to exploit application<br />

logic defects. Developers are often trying to speed their<br />

applications <strong>and</strong> optimize workflow to accomplish multiple<br />

tasks concurrently. An attacker can exploit this type of<br />

optimization by looking for what appear to be parallel<br />

processes that should logically run in serial.<br />

A good example is a business <strong>with</strong> an online shopping<br />

cart that has also implemented a customer loyalty<br />

system. Once a customer logs in <strong>and</strong> places their order,<br />

their purchases are converted to points which can be<br />

redeemed for goods or services at a later date. A hacker<br />

can attempt to manipulate this system by placing a small<br />

order, then entering an incorrect payment method to<br />

ensure that the transaction fails. Often, the loyalty points<br />

transaction <strong>and</strong> payment transaction are conducted in<br />

parallel for speed. However, even though the payment<br />

transaction fails, the loyalty points are still added to the<br />

customer’s account. This type of attack can be repeated,<br />

or even scripted, to obtain a large volume of loyalty points<br />

while not spending any money <strong>with</strong> the vendor.<br />

Exploiting Numerical H<strong>and</strong>ling Vulnerabilities<br />

It’s important to validate that an application h<strong>and</strong>les<br />

various numeric inputs correctly. Once again, turning to<br />

a shopping cart feature, if a hacker can input a negative<br />

number into an order system various issues can arise,<br />

including something called phantom inventory. If the<br />

application allows a negative number to be used, a<br />

hacker can simply add inventory that doesn’t exist<br />

during the purchase or acquisition process.<br />

For example, a recently discovered <strong>and</strong> patched<br />

issue in a popular Internet application allowed clientside<br />

validation of input, but if the hacker stripped out<br />

the field validation, which was written in JavaScript, or<br />

intercepted <strong>and</strong> manipulated the request, a negative<br />

number could be inserted. Inside the application logic<br />

there were immediately two issues. First, someone<br />

could checkout <strong>with</strong> a negative number prompting a<br />

debit to their account or credit card. This in effect was<br />

stealing money from the vendor. The second issue was<br />

<strong>with</strong> the back-end inventory management system which<br />

now reflected a discrepancy in available stock.<br />

The Devil is in the Details<br />

Logic defects in an application can cause serious<br />

business challenges. Discovering these types of<br />

issues takes time, <strong>and</strong> isn’t a simple matter of coding<br />

up a scanner tool. Each issue has a unique method of<br />

discovery, is different from one application to another,<br />

<strong>and</strong> is often different from one developer to another.<br />

The one thing all logic defects have in common is they<br />

are virtually invisible to modern web attack mitigation<br />

platforms. Whether there is a web application firewall,<br />

an intrusion prevention system, or any combination<br />

thereof – it’s nearly certain that attacking web application<br />

logic will be invisible to the victim –whether that it be an<br />

enterprise, a small business, or a governmental entity.<br />

This is primarily due to the lack of identifiable pa<strong>ttern</strong>s<br />

such as tell-tale SQL Injection markers. Requests <strong>and</strong><br />

responses are often legitimate, <strong>and</strong> don’t trigger an<br />

alarm or raise suspicion even if the hack is successful.<br />

Remaining undetected makes this type of attack very<br />

attractive to hackers, fraudsters, <strong>and</strong> others.<br />

Additionally, these types of security defects in applications<br />

often slip past testing teams as scanning tools <strong>and</strong> formal<br />

logic testing methodologies are still maturing. The best<br />

weapon against these types of security defects is the<br />

Quality Assurance testing organization at each business.<br />

QA testers are well-versed in application logic for the<br />

purposes of functional testing. Rather than developing logic<br />

testing methodology from scratch, security testing teams<br />

should collaborate <strong>with</strong> their QA organizations to add logic<br />

testing into their already well-adopted methodologies.<br />

It Takes a Village<br />

Logic defects are difficult to avoid in code, difficult to<br />

effectively test for, <strong>and</strong> nearly impossible to detect. As<br />

hackers exhaust obvious <strong>and</strong> scriptable defects on the<br />

Internet, web-based applications will likely be their next<br />

attack vector. Rather than waiting for the deluge of<br />

attacks, <strong>and</strong> for the appearance of scathing headlines,<br />

now is the time for development organizations,<br />

application security teams, <strong>and</strong> businesses to work<br />

together to fortify their web-based applications against<br />

these attacks. Focus on discussing this <strong>with</strong> your QA<br />

organization, develop an internal testing methodology<br />

that suits the applications you build, <strong>and</strong> test your<br />

existing applications. You can then apply these lessons<br />

learned to new applications you design before they are<br />

deployed throughout the organization.<br />

RAFAL LOS<br />

Rafal Los Enterprise <strong>and</strong> Cloud Security Strategist for Hewlett-<br />

Packard Software, combines over a decade of deep technical<br />

expertise in information security <strong>and</strong> risk management <strong>with</strong><br />

a critical business perspective. From technical research to<br />

building <strong>and</strong> implementing enterprise application security<br />

programs, Rafal has a proven track record <strong>with</strong> organizations<br />

of diverse sizes <strong>and</strong> verticals.<br />

Prior to joining HP, Los de�ned the framework for a software<br />

security program <strong>and</strong> served as a security lead at a Global<br />

Fortune 100. Los also contributed to the global organization’s<br />

security <strong>and</strong> risk-management strategy internally <strong>and</strong> <strong>with</strong><br />

their customers.<br />

www.hakin9.org/en 21


22<br />

Password, What<br />

Password?<br />

In this article we will be covering high level<br />

functionality of Kryptos Logic’s Kon Boot v1.1<br />

software. I will explain how to deploy this software<br />

<strong>and</strong> its payload into target computers running supported<br />

operating systems; policies <strong>and</strong> settings that already<br />

exist to block end users on a corporate, government<br />

or educational network from being able to use this<br />

software; <strong>and</strong> what you, a home user, can do to keep<br />

your computer safe from this attack.<br />

Lets start off by imagining that you just returned<br />

home from a summer vacation in Maui – you unpack<br />

your belongings, check your social media sites, <strong>and</strong><br />

prepare for the following months work. A couple of<br />

days later you start to notice that your social media<br />

sites have pictures on them that you did not post<br />

as well as other strange anomalies. After you check<br />

your other online accounts, you realize that there is<br />

suspicious activity on all of your web portals including<br />

the loss of $10,000 from your checking account. You<br />

wonder how anything like this could possibly happen.<br />

You have an epiphany that during your flight home you<br />

turned off your laptop <strong>and</strong> visited the lavatory, leaving<br />

your computer out on your tray table. You returned<br />

<strong>and</strong> found your computer just as you left it. You were<br />

not suspicious <strong>and</strong> resumed using your computer for<br />

the duration of your flight. What you did not know<br />

until now is that your fellow passenger had Kon Boot,<br />

broke into your computer, <strong>and</strong> stole the files off your<br />

desktop. You start to panic because you remember<br />

that you had an extensive password list file of every<br />

password you have as well as your work passwords<br />

in that file. Worst of all the attacker has begun locking<br />

you out of your own website accounts by changing<br />

the passwords <strong>and</strong> the security questions, because<br />

for convenience you put the questions <strong>and</strong> answers<br />

in the same file as well. Now you are in an extreme<br />

DEFENSE PATTERN<br />

If you have a computer <strong>with</strong> a non-encrypted hard drive <strong>and</strong> have not<br />

disabled other media devices from booting before your hard drive or<br />

have not password protected your BIOS listen up! Your computer <strong>and</strong><br />

files are at risk even if you have a user account password.<br />

panic. You have a myriad of questions – the most<br />

daunting how did he gain access to my computer<br />

<strong>and</strong> password file? You turned off your laptop <strong>and</strong><br />

your user account was password protected, so you<br />

believed you were safe. Now you have an enormous<br />

mess of work trying to salvage your online accounts<br />

<strong>and</strong> personal websites before the attacker finishes<br />

locking you out.<br />

Just for the sake of argument, <strong>and</strong> to avoid an<br />

onslaught of email, the attack I just mentioned could<br />

also be achieved many other ways including taking<br />

a non-encrypted hard drive out of the computer<br />

hooking it up to your own device using an external<br />

encasement kit, but that takes some time. Imagine<br />

what the passenger might say if they return from the<br />

lavatory <strong>and</strong> you have their computer half apart, trying<br />

to remove the hard drive <strong>and</strong> use it as an external<br />

drive on your own computer. The beauty of Kon Boot<br />

is that it is not obvious that you have used it on a<br />

target PC. Kon Boot neither overwrites the SAM nor<br />

makes any modification to the host operating system.<br />

Kon Boot achieves its goal by hooking the kernel<br />

during boot <strong>and</strong> allows the user to bypass the need<br />

for a password. The added beauty of Kon Boot is the<br />

next time you boot the computer <strong>with</strong>out the Kon Boot<br />

Media the user’s original password will still be on<br />

the system. Unlike other reset tools that completely<br />

remove the password, Kon Boot makes your average<br />

user unaware that you have just broken into their<br />

computer. I have used many other tools that will allow<br />

you to log into another users account or computer but<br />

they, for the most part, modify the operating system or<br />

leave some other kind of obvious trace. If you would<br />

like more information about other reset tools just use a<br />

search engine <strong>and</strong> search bypass/ reset windows user<br />

passwords.<br />

03/2011


Password, What Password?<br />

Computers that are vulnerable to this attack must<br />

have an Intel Pentium III or greater or another<br />

processor br<strong>and</strong> that is comparable. The computer<br />

must also run one of the supported Operating<br />

Systems. Vulnerable operating systems that are at risk<br />

for breach include Windows XP Home Edition, 32 <strong>and</strong><br />

64 bit editions of Windows Vista Home Basic, Home<br />

Premium, Business, <strong>and</strong> Enterprise, 32 <strong>and</strong> 64 bit<br />

editions of Windows 7 Home Premium, Professional,<br />

<strong>and</strong> Ultimate, 32 <strong>and</strong> 64 bit editions of Windows<br />

Server 2003 <strong>Web</strong> Edition, St<strong>and</strong>ard, Enterprise, <strong>and</strong><br />

Datacenter, 32 <strong>and</strong> 64 bit editions of Windows Server<br />

2008 St<strong>and</strong>ard, Enterprise, <strong>and</strong> Datacenter. This<br />

is the official list of vulnerable operating systems,<br />

though more may exist. One example is windows 7<br />

Starter, normally the OS installed on net books, but<br />

this OS remains untested <strong>and</strong> unverified. I have also<br />

personally tested some compatible operating systems<br />

using VMware. I was able to boot Kon Boot using the<br />

virtual CD drive <strong>and</strong> mount the .iso file during boot in<br />

order to hook the kernel. The end result was the same<br />

as running the software on a non virtualized operating<br />

system.<br />

One common question posed to me is how easy is<br />

it for any user to obtain this software. That answer is<br />

fortunately <strong>and</strong> unfortunately very easy. Anyone who<br />

wKants this software can license <strong>and</strong> download a copy<br />

from Kyptros Logics’ website. For your honest home<br />

user this is wonderful. He or she can now instantly<br />

download a piece of software that will allow him or her<br />

to bypass a simple windows logon screen <strong>and</strong> allow<br />

him or her to reset their own password if they have<br />

forgotten it. For the criminal mind, ease of access is<br />

also a beauty. The criminal can download a program<br />

that will allow him or her to gain access to a normal<br />

Figure 1. Using Kon Boot on virtualized operating systems in<br />

VMware<br />

password protected system in about ten seconds.<br />

This allows the criminal to log in as the victim user<br />

<strong>and</strong> access the computer just as if the legitimate user<br />

successfully authenticated into the desktop <strong>with</strong> the<br />

password. Something even more worrying is Kon Boot<br />

can be burned on to a flash media device making the<br />

program is extremely portable. You can carry Kon Boot<br />

in your pocket or on a keychain during your every day<br />

life. You no longer have to carry around optical media<br />

that can easily be damaged.<br />

Another use for this program is by parents wanting<br />

to monitor their child’s usage of a computer. When the<br />

child is away at school or unaware of their computer’s<br />

state, you the parent can log onto their computer <strong>and</strong><br />

review their files <strong>and</strong> pictures. A parent might want to<br />

do this in case they suspect that their child is falling<br />

victim to an Internet predator <strong>with</strong>out betraying the<br />

trust of the child. Having the child forcibly log into<br />

their computer <strong>and</strong> show their parents the contents<br />

may be extremely embarrassing for the child. The<br />

child might also have a file on their desktop that they<br />

delete <strong>with</strong>out you knowing when they log into their<br />

computer.<br />

This would make your average parent (lacking<br />

forensic abilities) computer search moot. Just like<br />

the police of sting operations, you must be subtle <strong>and</strong><br />

go under cover by using Kon Boot to find out what<br />

your children are really doing. Personally I do not<br />

recommend this kind of spying for most parents. My<br />

philosophy is unless your child has recently changed<br />

their personality or has become <strong>with</strong>drawn from their<br />

friends you should not be like a vulture waiting for<br />

something on their computer to pop up. This type of<br />

parental spying also touches some ethical issues I will<br />

not cover in this article.<br />

On the reverse end of that stick, imagine this<br />

family is like most, more or less computer illiterate.<br />

The children seem to know more about computers<br />

than the parents do. Now just think about what your<br />

children could potentially gain access to by using Kon<br />

Boot on the parents computer. This is a threat that<br />

should be taken seriously. It is incredibly easy to make<br />

a bootable media device <strong>with</strong> the payload software<br />

on it. Just think what is on your computer right now.<br />

Would you want your children to have access to those<br />

documents?<br />

After I explain all the features <strong>and</strong> awesomeness of<br />

Kon Boot many people ask me numerous questions. In<br />

this next segment I hope to answer a family of these<br />

questions for example, how long does it take to create<br />

the flash media; how computer savvy do you need to<br />

be in order to create the media; how much time does<br />

it take to create the media; <strong>and</strong> how likely is it that I, a<br />

simple end user, will be to duplicate all the results <strong>and</strong><br />

compromise my own test system.<br />

www.hakin9.org/en 23


24<br />

Ok, lets get started. You are going to make your<br />

own Kon Boot USB flash drive to use on your own test<br />

system. The first thing you will need to do is license<br />

a copy of Kon Boot. You can do this by accessing<br />

www.kryptoslogic.com <strong>and</strong> navigate to the buy Kon<br />

Boot link. Input all necessary licensing information<br />

<strong>and</strong> begin downloading the software. It is important<br />

to note that websites other than Kryptos Logic have<br />

Kon Boot version 1.0. Downloading this from a third<br />

party website may be risky because you never know<br />

if someone has injected their own malicious code<br />

on top of Kon Boot. As another note, version 1.0<br />

does not support 64 bit operating systems. Spend<br />

the money <strong>and</strong> license a legal copy of this software.<br />

Next take a flash drive that does not have any data<br />

saved to it <strong>and</strong> plug the flash drive into your computer.<br />

I personally used a very simple SanDisk br<strong>and</strong> flash<br />

drive to accomplish this task. If you use advanced<br />

flash drives like Ironkey or other br<strong>and</strong>s that require<br />

authentication they will not work properly on boot. You<br />

will not be able to access the information contained in<br />

them because they require input that you will not be<br />

able to provide until your operating system has already<br />

loaded. Open the file folder that you downloaded form<br />

Kyptros Logic <strong>and</strong> click on KONUSB, now navigate<br />

to KonBootInstall.exe <strong>and</strong> select it. This will open a<br />

comm<strong>and</strong> line interface <strong>with</strong> destination options to<br />

write the program. Choose the correct destination for<br />

your flash drive <strong>and</strong> select the corresponding letter<br />

drive. Press enter <strong>and</strong> the program will copy itself to<br />

your flash drive <strong>and</strong> make itself bootable. Now you<br />

have successfully burned the Kon Boot program onto<br />

your flash media, you can now close your programs<br />

<strong>and</strong> power off your computer. Now you will need to<br />

gain access to the BIOS. Power on your computer.<br />

When you see the computer manufactures logo press<br />

the corresponding function key to access the settings<br />

of the BIOS. In the Dell laptop I was using the key<br />

was F2. At this point it is important to note if you see a<br />

DEFENSE PATTERN<br />

password screen similar to that of Figure 2, <strong>and</strong> you do<br />

not know the password, you will not be able to access<br />

the BIOS <strong>and</strong> make any changes.<br />

There are ways to remove BIOS passwords but this<br />

can be time consuming <strong>and</strong> cumbersome depending on<br />

your computer manufacturer, model, <strong>and</strong> ease of access<br />

to the internal components. If you have not previously<br />

set a password this prompt will not appear <strong>and</strong> will just<br />

go the first page of the BIOS. If you can access the<br />

BIOS you need to navigate to the boot order options.<br />

Now you need to reorder the boot devices to make your<br />

media <strong>with</strong> Kon Boot boot before the hard drive. For this<br />

demonstration that would be your USB or removable<br />

media drive but, you can also use a Compact Disc or for<br />

legacy systems a floppy drive.<br />

After you have reordered the boot devices save<br />

<strong>and</strong> exit the BIOS configuration page. Your computer<br />

will restart at this point. If you have completed these<br />

steps correctly <strong>and</strong> your system board <strong>and</strong> chipset are<br />

compatible <strong>with</strong> Kon Boot this should be all you need<br />

to modify in order to use the software. Now, <strong>with</strong> your<br />

computer powered off plug your USB flash media <strong>with</strong><br />

the payload software, Kon Boot, into one of your USB<br />

ports. Next power on your computer. Your computer<br />

should try to boot the removable media device instead<br />

of your hard drive. If your computer successfully boots<br />

off your removable media device <strong>and</strong> loads Kon Boot,<br />

you will see a screen <strong>with</strong> this text on it.<br />

Once this screen appears you have successively<br />

hooked the kernel. Now all you have to do is wait for<br />

the supported Operating system to load <strong>and</strong> choose<br />

an user account to log in <strong>with</strong>. If the user account is<br />

normally password protected, Windows will prompt<br />

you for a password. This is normal. You may type any<br />

password, gibberish, or just leave the password field<br />

blank. Remember Kon Boot allows you to bypass the<br />

need for an actual password. Lastly press enter <strong>and</strong><br />

you should be in the compromised users account, their<br />

desktop should appear, <strong>and</strong> you may access to their<br />

Figure 2. A password Protected BIOS screen Figure 3. Reordered bootable devices in the BIOS<br />

03/2011


Password, What Password?<br />

files. For those who have been actively following along,<br />

you did it. You have just created a very powerful USB<br />

tool. Use it ethically.<br />

Now that I have sufficiently worried some of you, I<br />

am here to tell you there are very easy ways to prevent<br />

this attack on your computer. First you will need to gain<br />

access to your BIOS using the same steps mentioned<br />

above. Next navigate over to the Boot Order options.<br />

Next depending on your BIOS use the corresponding<br />

key to reorder the boot menu. Have your primary hard<br />

drive boot first <strong>and</strong> disable the other boot devices, or if<br />

your BIOS will not let you disable the devices, at least<br />

have the other media boot after the primary hard disk<br />

drive. Next navigate to the security tab or comparable<br />

tab. Now highlight Set Supervisor Password <strong>and</strong><br />

press enter. A menu should appear like this one. See<br />

Figure 5.<br />

Create an unique password, hopefully not 1234, <strong>and</strong><br />

press enter. Lastly you need to exit your BIOS. You will<br />

have several options or prompts when you try to exit.<br />

You want to select the option that says Exit <strong>and</strong> Save<br />

Changes or a similar choice. If you just exit or say<br />

discard changes all of the settings you just inputted will<br />

be ignored. Your computer will reboot after you exit the<br />

BIOS <strong>and</strong> will begin booting your operating system.<br />

That’s it. This will protect you from simple, time limited,<br />

<strong>and</strong> obvious hardware modification Kon Boot attacks,<br />

but will not protect your data if they can reset your<br />

BIOS password or extract the hard drive from your<br />

computer <strong>and</strong> use it as an external drive to steal your<br />

data off it, much like a criminal would if they just steal<br />

your entire computer. The good news is that there are<br />

more advanced ways of protecting your computer<br />

that are relatively easy. One of the ways to really<br />

lock unwanted users out of your computer, especially<br />

laptops, is <strong>with</strong> pre-boot authentication software.<br />

This software will completely lock out unauthorized<br />

users <strong>and</strong> render Kon Boot useless against the target<br />

system. Your system is protected even if you do<br />

not have a BIOS password, even though it is good<br />

practice to set one when you buy a laptop. The preboot<br />

authentication software also encrypts your entire<br />

hard drive, foiling a would be hacker from obtaining<br />

any data off the drive <strong>with</strong> a hard drive encasement<br />

kit. Some users might feel that this is overkill but if you<br />

are protecting sensitive data like your finances you<br />

want to minimize as many data breach opportunities<br />

as possible.<br />

On most educational, enterprise, <strong>and</strong> government<br />

networks this attack will not work. On government<br />

networks the workstations are heavily locked down<br />

<strong>with</strong> enterprise security software. The workstations<br />

are also, depending on base <strong>and</strong> policy, physically<br />

locked. The computer cases have been locked<br />

controlling access to the physical drives <strong>and</strong> hardware<br />

inside. Enterprise networks are very secure <strong>with</strong><br />

heavy auditing abilities. Some enterprise networks<br />

have fewer restrictions than a government network,<br />

but auditing access keeps snooping eyes at bay.<br />

On educational networks security can be heavy<br />

because of the sensitive information that must be<br />

secured. Usually I have seen deep freeze on most<br />

lab computers as well as BIOS Passwords. Preboot<br />

Authentication is not practical on these types<br />

of computers due to the large number of users. If<br />

the institution uses a BIOS Password this will lock<br />

out the students from being able to use software like<br />

Kon Boot. Security cameras, aware staff, <strong>and</strong> fellow<br />

students will notice a student taking a computer out of<br />

its enclosure <strong>and</strong> opening the system during class in<br />

order to reset the BIOS. This human level of security<br />

is not perfect but will usually stop a student when the<br />

threat of expulsion is very real.<br />

Now what happens if for some unforeseen reason a<br />

user is able to use Kon Boot on a network computer<br />

that caches the usernames locally? Unfortunately if<br />

there are not security barriers in place to prevent this<br />

attack the user, will be able to log in as a user that has<br />

Figure 4. Kon Boot successfully hooking the kernel on boot Figure 5. Creation of a BIOS Password<br />

www.hakin9.org/en 25


26<br />

had their usernames cached locally. This part is scary!<br />

That username could be an administrator’s user<br />

name. When the attacker logs in as the victim admin<br />

user, then the attacker has all the normal permissions<br />

of that admin user as set up in your network<br />

environment. This includes access to network drives!<br />

This lack of security can lead to company secrets<br />

being leaked from a rogue employee or spy. This is a<br />

real threat. Can logon auditing help keep an eye out<br />

for this kind of activity? The answer is complicated.<br />

Yes, you can look in logon log file audits <strong>and</strong> see<br />

when an user has logged on. This previous statement<br />

is also the reason why not. In most environments only<br />

security violations <strong>and</strong> invalid logons are logged in<br />

your domain controller. Unless the admin user is a<br />

honey pot <strong>and</strong> you have a global policy red flagging<br />

any activity of this user, you will not know. If you are<br />

able to detect the attacker it will most likely not be in<br />

real time allowing the attacker or spy to slip out <strong>with</strong><br />

information <strong>and</strong> leave the country. If your institution<br />

logs all network logons thinking that you will be able<br />

to detect this, lets be realistic. In a large company <strong>with</strong><br />

1,000 employees or more, a normal logon that does<br />

not throw up a security violation will be ignored as just<br />

your average admin user performing maintenance<br />

on a system. Endless what if scenarios arise. I sat<br />

in a server admin class where we were discussing<br />

access violations <strong>and</strong> logging options. That day most<br />

of the class time was spent trying to capture violation<br />

<strong>and</strong> event data <strong>with</strong>out filling up the hard drives <strong>with</strong><br />

nothing but log files. Other scholars might have the<br />

approach to log everything. This is not necessary in<br />

my mind due to the amount of data you would need<br />

to sift through to find a logon <strong>with</strong> no violation. In this<br />

economy corporations are cutting back staffing. This<br />

is a perfect scenario for the attacker to go unnoticed<br />

for a while or until they release the stolen data. If you<br />

no longer have many admins to support the users <strong>and</strong><br />

network audits, normal activity like correct logons will<br />

probably be ignored due to higher priority tasks. The<br />

massive log files would hinder an investigation plus<br />

you do not know what user you are looking for. No<br />

security violation or excessive use equals, in the real<br />

word, no second thought from a security admin.<br />

Having some level of protection is always a good<br />

plan. I have found in my experience that small mom<br />

<strong>and</strong> pop businesses usually do not have any kind<br />

of protection on their computers. Most of those<br />

businesses also do not even bother <strong>with</strong> user account<br />

passwords! I seriously hope this will change due to<br />

the amount of business information usually stored<br />

<strong>with</strong>in the computer systems. On the other extreme<br />

for large enterprise <strong>and</strong> government systems, I have<br />

seen a tremendous amount of security from heavily<br />

locked down workstations to portable laptops that<br />

DEFENSE PATTERN<br />

had full commercial data encryption protecting the<br />

entire hard drive, pre-boot authentication, <strong>and</strong> lockout<br />

features. I have found some free products that will<br />

do a pretty good job, but when you are protecting<br />

company proprietary information you take no chances.<br />

This also means IronKeys for all data transfer off a<br />

computer. When it comes to the individual computer I<br />

have found that most attackers are looking for crimes<br />

of opportunity. If someone has stolen your laptop they<br />

might quickly browse for your files <strong>and</strong> see if there is<br />

anything that catches their eye. If not they just wipe<br />

your computer <strong>with</strong> a new operating system <strong>and</strong> sell it.<br />

If you protect your computer <strong>with</strong> encryption the ease<br />

of browsing through your computer is eliminated <strong>and</strong><br />

the attacker might find that they do not have the skills<br />

or time to crack your hard drive, <strong>and</strong> just move on to<br />

another computer. For most home users True Crypt<br />

is sufficient. This encryption software is free, making<br />

it attractive to many people that want protection but<br />

are constrained by finances. A real life example of<br />

this in laymen’s terms, is when you lock your car in<br />

the parking lot of a grocery store. A criminal might just<br />

go up <strong>and</strong> down the isle tugging on the door h<strong>and</strong>les<br />

trying to find a car that is not locked. If the car is<br />

locked, there are many ways to break into the cabin<br />

but it might draw some attention <strong>and</strong> take more effort<br />

than just moving on looking for an unlocked car. If the<br />

thieves want in they can get in – all you are doing is<br />

trying to slow them down. You are just adding levels of<br />

security to make it more difficult <strong>and</strong> time consuming<br />

to enter your car. This is the same concept that we are<br />

applying to computers.<br />

I hope this article about one very simple program<br />

can help those who do not think much about computer<br />

security to reconsider their position. In less than five<br />

minutes a rogue or curious user can modify the BIOS<br />

<strong>and</strong> access a vulnerable workstation. It is my goal that<br />

home users will at the very least will set up a BIOS<br />

password to help keep criminals out of their computer<br />

<strong>and</strong> add one more level of protection to their system. All<br />

trademarks mentioned in this article are the property of<br />

their respective owners.<br />

CHRISTIAN A. MERGLIANO<br />

Graduate of Antelope Valley College<br />

Student at CSU Dominguez Hills<br />

CEO of Surgeradio.org<br />

Computer consultant / Technician<br />

cmergliano@surgeradio.org<br />

03/2011


28<br />

REVERSE ENGINEERING<br />

A Quick „H<strong>and</strong>s On”<br />

Introduction to Packing<br />

On Windows systems, programs are usually available in the PE file<br />

format <strong>with</strong> the .EXE extension. Although this file format is quite<br />

complex, it is now well documented, so underst<strong>and</strong>ing how it is<br />

globally supposed to work is pretty easy <strong>and</strong> you can find a lot of<br />

programs designed to open/analyze/modify PE executables.<br />

Those which are designed to modify PE files (for<br />

various purposes) are often called packers. We’ll<br />

learn how to write one of those in this article.<br />

We’ll start by taking a glance at PE file format’s basis;<br />

then we’ll design the principle of a small packer able to<br />

modify our pentest tools in such a way that they are less<br />

detected by AVs; <strong>and</strong> finally we’ll implement it in Python<br />

using both the good old “pefile” module <strong>and</strong> the br<strong>and</strong><br />

new miasm reverse-engineering framework.<br />

The tools<br />

pe�le<br />

The pefile module is a multi-platform Python module<br />

used to read <strong>and</strong> work <strong>with</strong> Portable Executable<br />

(aka PE) files. It can be downloaded here: http://<br />

code.google.com/p/pefile/. We’ll use this specialized<br />

module throughout the article; it is very efficient when<br />

dealing <strong>with</strong> PE files. The drawback of being specialized<br />

is that some steps of writing our packer can’t be done<br />

<strong>with</strong> pefile alone.<br />

Figure 1. Overview of a PE �le’s structure<br />

miasm<br />

miasm is a free <strong>and</strong> open source (GPLv2) reverse<br />

engineering framework for python that can be<br />

downloaded here: http://code.google.com/p/smiasm/.<br />

Being a reverse-engineering framework, it is much more<br />

versatile than pefile, but it is also much younger <strong>and</strong> still a<br />

bit unstable. Despite its youth we’ll see that it is sufficient<br />

to build a complete tiny packer.<br />

PE format<br />

Overview<br />

In their simplest form PE files can be represented as a<br />

collection of sections <strong>and</strong> a bunch of metadata. Sections<br />

are blobs that are mapped in memory when the program<br />

starts. These blobs can contain anything useful to the<br />

program: such as the program code itself, constant<br />

values, icons, etc. The metadata (mostly located in<br />

the PE file headers) contains a lot of information; at<br />

the very least the metadata defines where the sections<br />

are located in the file, what their name is, where they<br />

are supposed to be mapped to in memory, <strong>and</strong> where<br />

the starting point of the program is in memory, once the<br />

sections are mapped.<br />

Listing 1. pe�le’s basis<br />

>>> import pefile #loading the pefile module<br />

>>> pe = pefile.PE("calc.exe") #we open the well-<br />

>>> for s in pe.sections:<br />

... print s.Name<br />

...<br />

.text<br />

.data<br />

.rsrc<br />

known ‘calc.exe’ program<br />

03/2011


A quick “h<strong>and</strong>s on” introduction to packing<br />

Listing 2. smiasm basis<br />

>>> from elfesteem import * #we load the elfesteem module, part of the miasm framework<br />

>>> e = pe_init.PE(open("calc.exe",'rb').read()) #we open the well known ‘calc.exe’ program<br />

# section offset size addr flags rawsize<br />

0 .text 00000400 0126b0 00001000 60000020 00012800<br />

1 .data 00012c00 00101c 00014000 c0000040 00000a00<br />

2 .rsrc 00013600 008a88 00016000 40000040 00008c00<br />

Listing 3. Getting the AddressOfEntryPoint using pe�le<br />

>>> import pefile<br />

>>> pe = pefile.PE("calc.exe")<br />

>>> print "0x%.8X"%pe.OPTIONAL_HEADER.AddressOfEntryPoint # We want the address to be printed in hexa<br />

0x00012475<br />

Sections<br />

Even though there is no written st<strong>and</strong>ard about this,<br />

almost every PE file contains a section called .text<br />

<strong>and</strong> a section called .data. The first one usually holds<br />

the program’s binary code, the second one contains<br />

constants (fixed strings, integer constants, etc.). What<br />

we want to know about those sections is:<br />

• Where it is located <strong>with</strong>in the PE file<br />

• Where it is going to be mapped in memory<br />

• Whether or not it is writable<br />

Either <strong>with</strong> the pefile module or <strong>with</strong> the elfesteem<br />

module (which is shipped <strong>with</strong> miasm) you can answer<br />

those questions in only a couple of lines of python<br />

code: Listing 1.<br />

With miasm, it’s even easier as the sections’ name<br />

<strong>and</strong> some of their properties are automatically displayed<br />

when you instantiate the corresponding object: Listing 2.<br />

Metadata<br />

PE files have multiple headers stacked one over the<br />

other. The first header, called MZ header, is used when<br />

the programs starts from MS-DOS. We won’t discuss<br />

this header here as we want to pack executables<br />

running under Windows. Following the MZ header is a<br />

DOS stub which is a binary that runs when the program<br />

is launched from MS-DOS. We don’t care about this<br />

stub as this stub generally only displays this program<br />

cannot be run in dos mode <strong>and</strong> exits. Following the<br />

DOS stub is the real PE header.<br />

Within the PE header are stacked various structures<br />

such as the IMAGE_NT_HEADER, the IMAGE_FILE_HEADER <strong>and</strong> so<br />

on. The most interesting piece of information that we<br />

can find here is the address where the execution flow<br />

is going to be redirected by Windows just after having<br />

mapped the sections in memory when starting the<br />

program. This option is called AddressOfEntryPoint <strong>and</strong> is<br />

located in the OPTIONAL_HEADER (which is not optional…).<br />

Using pefile: Listing 3. Using miasm: Listing 4.<br />

If we look back at the sections characteristics showed<br />

by miasm in the previous paragraph we can check that<br />

the “.text” section (which is supposed to hold the binary<br />

executable code of the program) starts at address<br />

0x1000 <strong>and</strong> has for size: 0x0126b0. This means that<br />

this section goes from memory address 0x1000 to<br />

memory address 0x0136b0, which actually contains<br />

address 0x12475 which is the AddressOfEntryPoint. Until<br />

now everything’s fine <strong>and</strong> logical!<br />

Packer principle<br />

Now that we’ve learned how to read some characteristics<br />

of PE executable we can start wondering how to take<br />

profit out of it. For the purpose of this article, we’ll focus<br />

on AV detection. Indeed AV software generally loves the<br />

good old way of identifying viruses by finding a fixed<br />

string (called a signature) <strong>with</strong>in raw PE files. Many of<br />

Figure 2. Various headers stacked at the beginning of a PE �le<br />

www.hakin9.org/en 29


30<br />

Listing 4. Getting the AddressOfEntryPoint using smiasm<br />

>>> from elfesteem import *<br />

>>> e = pe_init.PE(open("calc.exe",'rb').read())<br />

(…)<br />

REVERSE ENGINEERING<br />

>>>print "0x%.8X"%e.Opthdr.AddressOfEntryPoint # We want the address to be printed in hexa<br />

0x00012475<br />

Listing 5. Ensuring that the targeted section is writable, using pe�le<br />

>>> import pefile<br />

>>> pe = pefile.PE("calc.exe")<br />

>>> data_section = pe.sections[1] #the ‘.data’ section is usually the second one (just after ‘.text’)<br />

>>> data_section.Characteristics|=0x80000000 #set the writable flag up<br />

Listing 6. Ensuring that the targeted section is writable, using miasm<br />

>>> from elfesteem import *<br />

>>> e = pe_init.PE(open("calc.exe",'rb').read())<br />

(…)<br />

>>> d_section = e.SHList[1] #the ‘.data’ section is usually the second one (just after ‘.text’)<br />

>>> d_section.flag |= 0x80000000 #set the writable flag up<br />

Listing 7. Ciphering a section thanks to pe�le<br />

>>> original = data_section. get_data(text_section.VirtualAddress, length = text_section.SizeOfRawData) # We<br />

read the original content of the ‘.data’ section<br />

>>> ciphered = ‘’ # This variable will hold the ciphered ‘.data’ section<br />

>>> for c in original:<br />

If ord(c)==255:<br />

else:<br />

ciphered+=chr(0)<br />

ciphered+=chr(ord(c)+1)<br />

>>> pe.set_bytes_at_rva(data_section.VirtualAddress, ciphered) # Now we overwrite the ‘.data’ section on disk<br />

<strong>with</strong> our enciphered version<br />

Figure 3. Taking advantage of on-disk padding to add some code<br />

03/2011


A quick “h<strong>and</strong>s on” introduction to packing<br />

the tools we use during pentests are now identified as<br />

a virus raising alerts in our clients IDS (which is quite<br />

annoying). If we could find ways of modifying our PE<br />

files while preserving their original behavior, we may be<br />

able to bypass AV detection <strong>and</strong> avoid spamming our<br />

clients <strong>with</strong> AV alerts during our pentests.<br />

In order to bypass AV detection, we can adopt two<br />

different strategies. The first one consists of identifying<br />

which part of the PE file contains the signature <strong>and</strong><br />

modifying it <strong>with</strong> an equivalent sequences of bytes. The<br />

second one is to modify as much of the PE file as we<br />

can, <strong>with</strong> the hope that the signature was in what we<br />

modified. The first strategy is very efficient but depends<br />

on both the AV <strong>and</strong> the PE file we want to pack; the<br />

second strategy is less efficient but does not depend<br />

on the AV software used nor on the PE file we want to<br />

pack. Therefore we’ll write a small packer implementing<br />

the second strategy in this article.<br />

Ciphering<br />

The principle of the packer we write is to encipher the<br />

content of the .data section so that, if the signature is<br />

located <strong>with</strong>in this section, AV software won’t notice<br />

the file anymore. Of course we will also have to add<br />

a decoding stub that will decipher the .data section<br />

when the app starts <strong>and</strong> redirect the execution flow<br />

to the original starting point once the deciphering is<br />

completed.<br />

For this article we’ll use a very stupid ciphering<br />

algorithm, derived from the Caesar algorithm. We’ll<br />

increment every byte of the .data section by one during<br />

encryption. We will decrement them by one during<br />

decryption. Advanced users can replace this algorithm<br />

<strong>with</strong> anything else (including xor-based algorithm or<br />

more advanced one such as AES) but this trivial one is<br />

sufficient to play a bit.<br />

Deciphering<br />

Our main concern regards the place we’ll write our<br />

decoding stub into. The easy way would be to add a<br />

new section at the end of the file, write our decoding<br />

stub here, <strong>and</strong> change the AddressOfEntryPoint to have<br />

it pointing to the beginning of the decoding stub. This<br />

method has two main drawbacks:<br />

• Having a lonely small section <strong>with</strong> the attribute<br />

executable at the end of a file may ring some bells<br />

in the most advanced AV pieces of software.<br />

• pefile does not h<strong>and</strong>le section adding very well,<br />

<strong>and</strong> I don’t have this feature of miasm working yet<br />

(although it is supposed to do it gracefully).<br />

So where can we write? The solution lies in some<br />

details of the PE file format st<strong>and</strong>ard: sections’ size<br />

on disk have to be a multiple of a certain value (which<br />

www.hakin9.org/en 31


32<br />

is stored in the PE headers). Usually this constraint<br />

causes some garbage to be saved on disk in order to<br />

exp<strong>and</strong> the useful section’s content to the next good<br />

multiple. If we can spot such a garbage place, we<br />

could write our decoding payload there <strong>and</strong> increase<br />

the section’s size to be mapped in memory (in the PE<br />

header) in order to have the whole section’s disk space<br />

to be mapped in RAM.<br />

Packer’s implementation<br />

Now that the principle is clearly defined, we can<br />

step into the implementation. We’ll start by ciphering<br />

the .data section <strong>and</strong> modify its attributes to have it<br />

writable in memory (so that our decoding payload can<br />

rewrite it live). The .data section being fully prepared,<br />

we’ll spot the place where we can write our decoding<br />

payload. Finally we’ll write the payload <strong>and</strong> change the<br />

AddressOfEntryPoint so that it points to the beginning of<br />

Listing 8. Looking for available space using pe�le<br />

>>> import pefile<br />

>>> pe = pefile.PE("calc.exe")<br />

>>> for s in pe.sections:<br />

REVERSE ENGINEERING<br />

the deciphering algorithm. Once all those modifications<br />

are done, our program will be stored on disk <strong>with</strong> its<br />

.data section ciphered, but will still work as usual as its<br />

new AddressOfEntryPoint will be our additional payload<br />

that re-creates the original .data section <strong>and</strong> reroute the<br />

execution flow to the original starting point.<br />

Preparing the “.data” section<br />

The preparation of the .data sections needs two steps:<br />

adding the writable flag to its attributes <strong>and</strong> ciphering<br />

its content. To start up <strong>with</strong> we’ll change the permission<br />

flags of the .data section to have it writable (so that our<br />

decoding stub can overwrite the ciphered version <strong>with</strong><br />

the deciphered one once mapped in memory). Using<br />

pefile: Listing 5. Using miasm: Listing 6.<br />

Now that the section’s writable we just have to<br />

encipher it on disk. Using pefile: Listing 7.<br />

And that’s it. We are done <strong>with</strong> the .data section!<br />

... print s.Name, '- Bytes of ‘on disk padding’=", s.SizeOfRawData-s.Misc_VirtualSize<br />

...<br />

.text : Bytes of ‘on disk padding’ = 336<br />

.data : Bytes of ‘on disk padding’ = -1564<br />

.rsrc : Bytes of ‘on disk padding’ = 376<br />

Listing 9. Looking for available space using miasm<br />

>>> from elfesteem import *<br />

>>> e = pe_init.PE(open("calc.exe",'rb').read())<br />

(…)<br />

>>> for s in e.SHList:<br />

... print s.name,": Bytes of 'on disk padding'=",s.rawsize-s.size<br />

...<br />

.text : Bytes of 'on disk padding'= 336<br />

.data : Bytes of 'on disk padding'= -1564<br />

.rsrc : Bytes of 'on disk padding'= 376<br />

Listing 10. deciphering code, in ASM<br />

decipher_main:<br />

MOV eax, START_ADDR # We store in EAX the address we are currently deciphering<br />

MOV ebx, END_ADDR # We store in EBX the address at which we shall stop deciphering<br />

decipher_loop:<br />

DEC BYTE [EAX] #We decipher the current byte (which address is stored in EAX)<br />

INC EAX #We move on the the next byte to decipher<br />

CMP EAX, EBX #We compare the address to decipher to our STOP address<br />

JNZ decipher_loop # If we haven’t reached the "STOP address" yet we go back to deciphering<br />

JMP ORIGINAL_AddressOfEntryPoint #Now we jump to the original program’s start<br />

03/2011


A quick “h<strong>and</strong>s on” introduction to packing<br />

Where to add our payload<br />

Now that the .data section is ready we can focus on<br />

adding the decoding stub. Firstly we have to check how<br />

much padding trash space we have for each section.<br />

Ideally we’d love to have enough space <strong>with</strong>in the .text<br />

section but if it’s not the case any other section will be<br />

fine as long as we remember to set its executable flag<br />

on. To check the padding trash quantity we enumerate<br />

each section <strong>and</strong> compare its size on disk to its size<br />

once mapped in memory. Using pefile: Listing 8. Using<br />

miasm: Listing 9.<br />

You’ll notice that the .data section gives us a negative<br />

result, indeed some sections (especially those holding<br />

data) can have a larger space reserved in memory<br />

than what they have on disk, this is not a bug. Anyway<br />

Listing 11. assembling our deciphering stub using miasm<br />

>>> from miasm.core import parse_asm<br />

>>> from miasm.arch.ia32_arch import x86_mn<br />

>>> from miasm.core.asmbloc import asm_resolve_final<br />

>>><br />

>>> my_asm+="mov eax,"+hex(Start_Of_Data_Section)+"\n"<br />

>>> my_asm+="mov ebx,"+hex(End_Of_Data_Section)+"\n"<br />

>>> my_asm+="decipher_loop:\n"<br />

>>> my_asm+="dec [eax]\n"<br />

>>> my_asm+="inc eax\n"<br />

>>> my_asm+="cmp eax,ebx\n"<br />

>>> my_asm+="jnz decipher_loop\n"<br />

>>> my_asm+="mov eax,"+old_AddressOfEntryPoint+"\n"<br />

>>> my_asm+="jmp eax\n"<br />

>>><br />

we figured out that we have 336 bytes available in<br />

the .text section (which holds the main binary code of<br />

the program) to write our decoding stub, that’s good.<br />

We’ll have to write a small decoding stub but it looks<br />

possible.<br />

Writing the decoding part<br />

The decoding stub’s goal is very simple: it loops from<br />

the beginning of the .data section to the end of it,<br />

decrementing each byte by one. Once it has finished<br />

it jumps to the original AddressOfEntryPoint. Sadly we’ll<br />

have to write this part in ASM, leaving python for a few<br />

minutes. In ASM: Listing 10.<br />

Now you should be wondering how we’ll inject this<br />

ASM part, in a compiled form, <strong>with</strong>in our PE file. The<br />

>>> all_blocks, symbol_pool = parse_asm.parse_txt(x86_mn,my_asm) #This one <strong>and</strong> the 3 followings are used to<br />

assemble our ASM text written in "my_asm"<br />

>>> symbol_pool.add(asmbloc.asm_label('base_address',0))<br />

>>> symbol_pool.getby_name("decipher_main").offset=0<br />

>>> resolved,patches = asm_resolve_final(x86_mn, all_blocks[0], symbol_pool)<br />

>>><br />

>>> deciphering_stub = ‘’ #This will hold the binary executable of our ASM piece of code<br />

>>> addr = patches.keys()<br />

>>> addr.sort()<br />

>>> for a in addr:<br />

… deciphering_stub += patches[a]<br />

…<br />

Listing 12. �nishing things up <strong>with</strong> pe�le<br />

>>> pe.OPTIONAL_HEADER.AddressOfEntryPoint = tsection.VirtualAddress+tsection.Misc_VirtualSize-len(stub)<br />

>>> pe.OPTIONAL_HEADER.CheckSum = pe.generate_checksum() #we refresh some checksum in the headers (otherwise the<br />

program will not start due to "corruption")<br />

>>> pe.write("packed.exe") #Finally we write our modified executable on disk !<br />

www.hakin9.org/en 33


34<br />

Table 1. Checking if the �les have been packed<br />

answer is: <strong>with</strong> Python of course. Indeed miasm is<br />

shipped <strong>with</strong> disassembling/assembling capabilities that<br />

we’ll use. Sadly there is no way of doing this <strong>with</strong> pefile<br />

so if you really don’t want to use miasm you’ll have to<br />

consider using a different st<strong>and</strong>alone program (such<br />

as fasm) or another python module <strong>with</strong> assembling<br />

capabilities (such as MOSDEF). Let’s get the binary<br />

blob corresponding to this ASM text: Listing 11.<br />

With those few lines of python we have the binary<br />

code corresponding to the ASM deciphering stub we<br />

wrote earlier. A quick check let us know that this binary<br />

will fit <strong>with</strong>out any problem <strong>with</strong>in the trash-padded<br />

space we spotted earlier.<br />

>>> len(deciphering_stub)<br />

25<br />

Executable Original MD5 Packed MD5<br />

We now only have to add this small piece of binary<br />

code at the end of the .text section <strong>and</strong> modify the PE<br />

file header to enlarge the size of this section that has<br />

to be mapped in memory. Using pefile :<br />

>>> pe.set_bytes_at_rva(tsection.VirtualAddress+<br />

tsection.Misc_VirtualSize, stub)<br />

>>> tsection.Misc_VirtualSize+=len(stub)<br />

Now that everything’s ready we still have to change the<br />

AddressOfEntryPoint in order to start the program <strong>with</strong><br />

our decoding stub; to update some checksums stored<br />

in the headers, <strong>and</strong> to save the resulting PE file on disk<br />

(we use only pefile for this point because some kind of<br />

bug currently seems to corrupt the PE files written<br />

from miasm; although this may be corrected once the<br />

article’s published): Listing 12.<br />

Testing <strong>and</strong> conclusion<br />

Using those few lines of code we packed some usual<br />

Windows binaries, confirmed that they still worked<br />

exactly the way they are supposed to, <strong>and</strong> compared<br />

their md5 signatures: Table 1.<br />

The signatures are definitely different which proves that<br />

the files are different. Using this packer we may be able to<br />

REVERSE ENGINEERING<br />

Calc.Exe 5911f4ae105c7469636f7adcea35349f 5f8a5cc3e415ae17dc3f3295138172fb<br />

Notepad.exe 2dcc5c800f51d487178814ca9eada181 571299065675826205d78b56d2dd2adf<br />

Winmine.exe ea682c022f7204cc8e8c9ef5dce29356 efa6921d3f123c62735d850b3e087cfd<br />

Table 2. Efficiency measurement of our tiny packer<br />

Executable Percent modi�ed<br />

Calc.Exe 4.7%<br />

Winmine.exe 6.3%<br />

Notepad.exe 1.1%<br />

bypass a couple of AV if we pack our pentest tools! In order<br />

to estimate our chances of bypassing AV we measured<br />

the amount of the file that was changed: Table 2.<br />

These percentages are quite low <strong>and</strong> you may feel<br />

like ciphering more sections would be a good idea. After<br />

all there is no reason to cipher only the .data section,<br />

you could cipher almost every section this way <strong>and</strong><br />

reach percentages around 90%...but there is a trap.<br />

Indeed when Windows loads a PE file it does some<br />

magic BEFORE jumping to the AddressOfEntryPoint. This<br />

magic includes things such as loading some DLL the<br />

program needs to run; <strong>and</strong> the glitch is that the list of<br />

DLLs can be stored in any section. If you cipher this<br />

list Windows will fail to underst<strong>and</strong> it <strong>and</strong> will refuse to<br />

launch your program (i.e. your packed version will not<br />

work). There are a couple of traps like those <strong>and</strong> if you<br />

want to improve your packer you’ll especially have to<br />

worry about the IAT (the Import Address Table, i.e. the<br />

list of DLL needed) <strong>and</strong> the .rsrc section (the sections<br />

that holds resources …such as the program’s icon).<br />

Once you’ve managed to spot the parts of the program<br />

you don’t want to encipher (such as the IAT) you can<br />

modify the packer to support ciphering of sections that<br />

holds sensitive data that shall be left in the clear (this<br />

is left as an exercise to the reader). And if this is not<br />

sufficient for you, it may be possible to find really nice<br />

ways of finally modifying those parts <strong>with</strong>out ciphering.<br />

As an example the IAT contains, for every DLL-imported<br />

function needed by the program, the offset of the starting<br />

point of the function in the DLL binary. The trick is that if<br />

this offset happened to be wrong Windows will gracefully<br />

search the whole DLL for the good offset <strong>and</strong> the program<br />

will still work! So a nice <strong>and</strong> quick way of modifying the<br />

IAT is to fill every offset <strong>with</strong> r<strong>and</strong>om values.<br />

As far as I’ve gone (~200 lines of Python) I usually<br />

modify around 80% of my PE files, which is sufficient<br />

to have gsecdump/cachedump/NameYourFavoritePentestToo<br />

lHere bypass around 60% of AVs! If you still want to<br />

do better you may consider ciphering the whole file,<br />

dynamically decipher it in RAM, <strong>and</strong> emulate everything<br />

that Windows does when launching a real PE from disk,<br />

but that’s a lot more work!<br />

ALAIN SCHNEIDER<br />

Alain has been a professional pentester for years but also<br />

published a few papers. Those papers focused both on basic<br />

tricks <strong>and</strong> on-the-edge technics. His favorites subjects are<br />

password cracking <strong>and</strong> cryptography<br />

03/2011


36<br />

Interview <strong>with</strong><br />

Jan van Bon<br />

Creating a solid <strong>and</strong> practical architecture under your IT management<br />

approach can greatly reduce the cost of improving quality, <strong>and</strong> it can<br />

speed up your projects. An integrated approach requires a simple<br />

<strong>and</strong> straightforward method that is easy to underst<strong>and</strong>, supported<br />

by available tools in the market, <strong>and</strong> accepted by many providers.<br />

This kind of approach requires thorough knowledge <strong>and</strong> sincere<br />

dedication. As <strong>with</strong> many other initiatives in the field of IT Service<br />

Management, the Netherl<strong>and</strong>s have again produced a fascinating<br />

new approach, <strong>with</strong> promising results for IT Security projects.<br />

Traditional security projects show a high degree of falling back<br />

specifically because they are not embedded in a well-functioning<br />

management system – says Jan van Bon.<br />

Exploiting Software: You are the Director at<br />

BHVB – Bureau Hoving & Van Bon. For the<br />

people who are not familiar <strong>with</strong> your group,<br />

could you tell us a little bit about it?<br />

Jan van Bon: The Bureau Hoving & Van Bon (BHVB)<br />

is a small but highly dedicated team that has focused<br />

on developing a methodological approach to IT Service<br />

Management. It is based in the Netherl<strong>and</strong>s, where<br />

many of the initiatives on IT Service Management have<br />

started. After observing two decades of mostly failing<br />

projects in this field, the team assimilated all available<br />

knowledge in a straightforward aproach, incorporating<br />

existing methods for organizational change <strong>and</strong><br />

improvement into one deeply structured new method.<br />

This approach enabled them to create a method that<br />

was both simple <strong>and</strong> effective, <strong>and</strong> easy to learn.<br />

Exploiting Software: You are also the Director<br />

at Inform-IT , Knowledge Center for Service<br />

Management. What is its mission?<br />

INTERVIEW<br />

Jan van Bon has been a driving force in the field of IT Service Management for the last 20<br />

years. After a decade of academic research he started his work in IT in the late 1980’s, in the<br />

Netherl<strong>and</strong>s. He has been heavily involved in ITIL, ITSMF, <strong>and</strong> several innovative projects<br />

ever since. He produced more than 80 books, in up to 14 languages, <strong>with</strong> thous<strong>and</strong>s of<br />

expert authors <strong>and</strong> reviewers from all over the world, on a broad range of IT Management<br />

topics. He is the founder <strong>and</strong> Chief Editor of the ITSM Library, the ITSM Portal, <strong>and</strong> several<br />

other knowledge portals. As a practitioner he is involved in supporting many organization<br />

improvement projects.<br />

JvB: Inform-IT is the oldest knowledge center on IT<br />

Service Management around. It started in 1996, <strong>and</strong><br />

has been responsible for many of the products of<br />

itSMF Netherl<strong>and</strong>s <strong>and</strong> itSMF International.<br />

Inform-IT’s mission is to develop <strong>and</strong> distribute<br />

knowledge carriers in the field of IT Service Management.<br />

It produced large numbers of conferences <strong>and</strong> books,<br />

<strong>and</strong> is responsible for the first knowledge platform<br />

in the field of IT Service Management – created in<br />

1996, <strong>and</strong> now still present: the ITSM PORTAL<br />

(www.itsmportal.com).<br />

Inform-IT doesn’t do this on its own – no single<br />

person or team can have all the knowledge it takes.<br />

Therefore Inform-IT works <strong>with</strong> a huge community of<br />

experts, from all over the world, <strong>and</strong> from all relevant<br />

disciplines.<br />

Enabling these scattered sources to find eachother<br />

<strong>and</strong> to co-produce the missing piece of knowledge that<br />

Inform-IT is targeting, has been the core of Inform-IT’s<br />

work of the last 15 years.<br />

03/2011


Interview <strong>with</strong> Jan van Bon<br />

Exploiting Software: How did Inform-IT start<br />

<strong>and</strong> how it has evolved to where it is today?<br />

JvB: One of the first assignments of the knowledge<br />

center was to professionalize the itSMF in the<br />

Netherl<strong>and</strong>s, by setting up a chain of events, develop<br />

a magazine, <strong>and</strong> set up relationships <strong>with</strong> other<br />

professional bodies in IT in the Netherl<strong>and</strong>s. From 1996<br />

on, Inform-IT started developing annual books on the<br />

state of the discipline of ITSM. Around the turn of the<br />

century Inform-IT delivered a huge book, titled The<br />

Guide to IT Service management. For many people,<br />

this was their entry into the latest developments of<br />

ITSM, beyond ITIL.<br />

Today, Inform-IT is responsible for maintaining its<br />

portfolio of books, for managing the ITSM Portal, <strong>and</strong><br />

for developing new titles; titles that are filling in the blank<br />

spots in the ITSM discipline. Some of the books Inform-<br />

IT is now working on, are focusing on the subject of<br />

organizational development <strong>and</strong> improvement, a topic<br />

that was largely ignored in all ITIL versions.<br />

Exploiting Software: What is your most<br />

interesting <strong>and</strong> relevant project?<br />

JvB: Inform-IT was involved in many projects, but<br />

the one project that will have most impact of all, is<br />

the ISM Method® (Integrated Service Management:<br />

www.ismportal.nl/en/). Inform-IT contributes heavily to<br />

the development of a method that is commoditizing<br />

the organizational change that is always at the core of<br />

ITIL projects. In the Netherl<strong>and</strong>s the ISM Method has<br />

already been recognized as the next step, that will take<br />

organizations to a much higher level of service delivery,<br />

making much better use of available guidance, like the<br />

practices described in ITIL or COBIT.<br />

Exploiting Software: What is your view<br />

regarding the risk management?<br />

JvB: In the field of IT Service Management, the ITIL<br />

books have been a true hype, for more than a decade<br />

now. Unfortunately, the term risk has been ignored in<br />

ITIL books for a very long time. In all other disciplines,<br />

Risk Management was a logic <strong>and</strong> basic ingredient.<br />

The ITIL authors however reformatted the risk management<br />

activities in practices like Continual Service<br />

Improvement, Problem Management, The Seven Step<br />

Improvement Process, et cetera. This didn’t help to put<br />

Risk Management in its rightful place.<br />

The ISM Method has put Risk Management at the<br />

heart of the process model, covering all proactive<br />

activities aimed at preventing the agreed service levels<br />

to fail. This way, Risk Management is an integrated<br />

element of the management system.<br />

Exploiting Software: What do you think about<br />

the IT Security developement?<br />

JvB: IT Security Management is one of the first<br />

functions you think of in an organization. Other<br />

examples of core functions are Capacity Management,<br />

Service Management, <strong>and</strong> Continuity Management. All<br />

of these are not processes but functions: organizational<br />

capabilities using people, processes <strong>and</strong> products. They<br />

should be based upon the processes that each service<br />

organizations should have in place, but they should<br />

not duplicate these. In practice, the effectiveness <strong>and</strong><br />

efficiency of these functions is highly influenced by the<br />

level of integration that has been achieved.<br />

Once you underst<strong>and</strong> that Security Management<br />

doesn’t have to create redundant processes like<br />

Security Incident Management or Security Change<br />

Management or Security Level Management, you can<br />

easily use the available core process to support secure<br />

IT services. Security Management is involved in any<br />

of these core processes: it is activated whenever a<br />

Security Section has to be designed in an SLA, when<br />

a change involves security measures (<strong>and</strong> when is that<br />

NOT the case?), when there are security incidents, or<br />

security risks, et cetera.<br />

Organizations that have learned to apply an integrated<br />

management system, like the ISM Method, will have a<br />

thorough approach to all their management goals,<br />

whether these apply to speed, capacity, availability,<br />

or security. Having such a system in place will make it<br />

much easier to achieve security goals, e.g. meeting the<br />

requirements of the ISO27000 st<strong>and</strong>ard.<br />

In practice, organizations that apply the ISM<br />

Method, can build on the resulting management<br />

system, to achieve any of these goals. Of course the<br />

implementation of the measures that are required will<br />

have to be done in all cases, but the implementation<br />

will now be much more solid (since they are formal<br />

changes to the IT infrastructure), <strong>and</strong> will be much<br />

more sustainable, since they are built into the regular<br />

management system.<br />

Traditional security projects show a high degree of<br />

falling back specifically because they are not embedded<br />

in a well-functioning management system. Using the<br />

lessons of the ISM Method, new security projects can<br />

now be done at much lower cost, <strong>and</strong> are much more<br />

effective in the long run.<br />

It has taken us over twenty years to learn this.<br />

But now that the methodology is available, security<br />

management’s future is looking much brighter again.<br />

www.hakin9.org/en 37


Next Issue of<br />

Shellcode: From a Simple Bug to OS<br />

Control<br />

DPA Exploitation <strong>and</strong> GOTs <strong>with</strong> Python<br />

And more...<br />

will be available to<br />

download on<br />

November 22 nd<br />

If you would like to contact Hakin9 team, just send an email to<br />

en@hakin9.org. We will reply a.s.a.p.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!