Fundacja Rozwoju Regionu Gołdapedukacja techniczno informatyczna
Access:

» Linux shellcode optimisation

Related categories: C/C++ | Security | Shellcode | Linux

Micha³ Piotrowski
Viewed: 9504 | Article date: 2006-03-24 15:41:03

Let's write four simple shellcodes from scratch, starting with programs in C, then converting them into assembly. Afterwards let's prepare them for shellcode use and finally optimise them.

A shellcode is an essential part of any exploit. During attack, it is injected into the target application and performs the desired actions within it. However, the basic rules for building shellcodes are not too widely known, even though they don't require advanced skills.

About the author

Michal Piotrowski holds an MA in computer science and has many years' experience as network and system administrator. For over three years, he worked as security inspector in an organisation supervising the main Polish PKI certification centre. He is currently working as an IT security consultant at one of Poland's largest financial institutions. His hobbies include programming and cryptography.

What you will learn...

  • how to write a working shellcode,

  • how modify and compact it.

What you should know...

  • you should be familiar with the Linux operating system,

  • the basics of programming in C and assembler.

 

A shellcode (sometimes also called a bytecode) is a sequence of commands in machine code, constituting a vital element of all buffer overflow exploits. During attack, the exploit injects its shellcode into a running application, causing it to execute the intruder's commands within the target program. The name shellcode originates from the earliest codes of this type, whose purpose was to bring up the system shell (in Unix-based system, the shell is the /bin/sh program). The term currently encompasses all manner of codes, performing a huge variety of actions.

Any shellcode has to fulfil a number of requirements. The first is that cannot contain null bytes (0x00), since these signify the end of a character string and terminate processing for many functions commonly exploited for buffer overflows - strcpy(), strcat(), sprintf(), gets() etc. A shellcode must also be autonomous and operate independently of its current address in memory, so static addressing cannot be used. Other features which can occasionally be significant are the size and ASCII character set of the shellcode.

Let's have a look at writing shellcodes in practice. We will create four programs with different functionality and then go on to modify them so as to compact and adapt them for use in actual exploits. Note that we will be looking exclusively at shellcodes, not buffer overflow attacks or writing exploits.

To create an operational shellcode, we'll need a thorough understanding of assembly language for the shellcode's target processor (see Inset Registers and instructions). We'll be working on 32-bit x86 processors running the Linux operating system with the 2.4 kernel - all examples work with 2.6 series of Linux kernel, too - so we have a choice of two main assembler syntax conventions: AT&T and Intel. Although AT&T syntax is used by the majority of compilers and debuggers (including gcc and gdb), we will use Intel syntax for its greater clarity. All examples will be compiled using the Netwide Assembler (nasm) version 0.98.35, available in most popular Linux distributions. We will also use the ndisasm and hexdump utilities.

 

Registers and instructions

Registers (see Table 1) are small memory cells within the CPU, used for storing the numerical values required by the processor during program execution. In 32-bit x86 CPUs, the size of the registers is 32 bits (4 bytes). Registers can be divided according to their purpose into data registers (EAX, EBX, ECX, EDX) and address registers (ESI, EDI, ESP, EBP, EIP).

Data registers are divided up into smaller sections of 16 bits (AX, BX, CX, DX) and 8 bits (AH, AL, BH, BL, CH, CL, DH, DL). The smaller registers can be used to decrease code size and get rid of padding null bytes (see Figure 1). Most of the address registers have their own specific uses and should not be used for storing ordinary data.

 

Page: 1 2 3
Buy article Buy subscription
Buy now add to cart
add to cart
Standard price: 2€/$3 Standard price: 25€/$30
Buy article for as little as (2€/$3) each allow access to individual articles. Buy a full access to our Hakin9 archive portal. You will be able to read the articles from all archive issues from year 2005 and 2006. For just 25€/$30 you get unrestricted access to the entire website for the whole year.
SDJhakin9

.SDJ Users:


.:Login
.:Password

[Register]
[Forgotten your password?]

...hakin9 StarterKit IT Practical Solutions for Newbies

...Shopping Cart

sum: 0 €
Choose currency:

...SUBSCRIBE TO
hakin9 Print Edition


...Advertisement



...Conferences

...Topics

...Advertisement

 

 

Subscribe | Contact Us | Newsletter | See all issues | About Hakin9
Copyright C 2006 by Software Developer's Journal. All rights reserved.