computer security group at rpisecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · web...

21
Hands-on Learning Exercise 2 to Accompany Chapter 4: Assembling With Cygwin/NASM and Shellcode Here is a basic helloworld.asm program in x86 assembly. ;msgbox.asm [SECTION .text] global _start _start: ;eax holds return value ;ebx will hold function addresses ;ecx will hold string pointers ;edx will hold NULL xor eax,eax xor ebx,ebx ;zero out the registers xor ecx,ecx xor edx,edx jmp short GetLibrary LibraryReturn: pop ecx ;get the library string mov [ecx + 10], dl ;insert NULL mov ebx, 0x7c801d7b ;LoadLibraryA(libraryname); push ecx ;beginning of user32.dll call ebx ;eax will hold the module handle jmp short FunctionName FunctionReturn: pop ecx ;get the address of the Function string xor edx,edx

Upload: others

Post on 19-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Hands-on Learning Exercise 2 to Accompany Chapter 4:

Assembling With Cygwin/NASM and Shellcode

Here is a basic helloworld.asm program in x86 assembly.

;msgbox.asm [SECTION .text]

global _start

_start:;eax holds return value;ebx will hold function addresses;ecx will hold string pointers;edx will hold NULL

xor eax,eaxxor ebx,ebx ;zero out the registersxor ecx,ecxxor edx,edx

jmp short GetLibraryLibraryReturn:

pop ecx ;get the library stringmov [ecx + 10], dl ;insert NULLmov ebx, 0x7c801d7b ;LoadLibraryA(libraryname);push ecx ;beginning of user32.dllcall ebx ;eax will hold the module handle

jmp short FunctionNameFunctionReturn:

pop ecx ;get the address of the Function stringxor edx,edxmov [ecx + 11],dl ;insert NULLpush ecxpush eaxmov ebx, 0x7c80ae30 ;GetProcAddress(hmodule,functionname);call ebx ;eax now holds the address of MessageBoxA

jmp short MessageMessageReturn:

Page 2: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

pop ecx ;get the message stringxor edx,edxmov [ecx+3],dl ;insert the NULL

xor edx,edx

push edx ;MB_OKpush ecx ;titlepush ecx ;messagepush edx ;NULL window handle

call eax ;MessageBoxA(windowhandle,msg,title,type); Address

ender:xor edx,edxpush eaxmov eax, 0x7c81cafa ;exitprocess(exitcode);call eax ;exit cleanly so we don't crash the parent program

;the N at the end of each string signifies the location of the NULL;character that needs to be inserted

GetLibrary:call LibraryReturndb 'user32.dllN'

FunctionNamecall FunctionReturndb 'MessageBoxAN'

Messagecall MessageReturndb 'HeyN'

Copy and paste this a text editor and name the file hello.asmThis is the same basic program as the first tutorial but is formatted using NASM directives. NASM is another assembler.

Page 3: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Open Cygwin and type the following commands.

This is actually 3 commands together, each separated by a semicolon.

nasm –f elf hello.asm ; This assembles hello.asm as an elf binary

ld –o hello hello.o ; This links hello.o and creates the .executable hello

objdump –d hello ; This disassembles the binary hello

Page 4: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

If all goes correctly, you should get the following output:

Page 5: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

We are just interested in these sections:

Page 6: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

These contain the hexadecimal numbers for our assembly code.

We see it starts with the hex numbers 31 c0 31 db 31 c9 31 d2 eb 37

………

And the last section ends with e8 c2 ff ff ff 48 65 79 4e

Let us now open the hello executable in the hex editor HxD. StartProgramsHxD Hex EditorHxD

Page 7: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Drag and drop the hello file into HxD. My file is located in C:\cygwin\home\User\hello because I just opened the Cygwin command prompt and did my work in the default directory. If you don’t know where your file is, use windows search to find it.

Page 8: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

In the center you may notice the same hex numbers as we identified with objdump as being our assembly code.

Page 9: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Highlight from the 31 c0 to the 79 4e:

Page 10: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Then go to the top toolbar, EditCopy AsC

Now create a new project in Visual Studio, make a Win32 console project, and make sure it’s an empty project. If you need help doing this, see the first tutorial.

Page 11: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Paste the hex you copied as C from HxD. It should look like this.

Now we are going to fill in the main() part of our c program to call this code. It will look like this. Below the picture is the code you can copy and paste.

int main(){

int (*func) ();func = (int (*) ())&code; func();

}

Let’s go over what each line is doing.

Page 12: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

int (*func) () This declares a pointer to function that returns an int and takes a void or no args

func = (int (*) ())&code; This assigns a function pointer to address of code, (int (*) () ) is the type cast.

func(); This calls the function

Build it and run it.

We get another popup box.

Let’s use the debugger and step through the code.

Page 13: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Left click the side bar next to int (*func) (); This will place a breakpoint which is indicated by a big red circle.

Go up to the top toolbar, DebugStart Debugging

A tab should open near the top that says disassembly. If this tab does not open, press alt+8 to open it.

Click the disassembly tab. You should now see your c code in bold black, with the assembly code in grey. The c code is acting like comments to the assembly code. The assembly code is actually being executed.

Now to move forward in the code, press F10. This is step over.

Page 14: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

You can press F10 until the pop-up box says hello and the program ends. But we want to see our character array filled with hex code execute.

Page 15: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

When you reach the line that says:

Call dword, ptr [func]

Press F11. This will step into, which means it will follow the assembly into that function.

If you press F11 when the yellow arrow is on the line that says call dword, ptr [func] you will then jump to that code. That should look like this:

You are now looking at the code then you original assembled using NASM and Cygwin. If has been translated into hex data and entered into our program through the source code.

Page 16: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Going back to when we first started debugging. There are a lot of extra assembly instructions in there that we weren’t expecting. These are security features added to our program by Microsoft.

Page 17: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

We can remove some of these for the sake of clarity by going to projectproperties. Make sure you are no longer debugging.

Then go to Configuration PropertiesC/C++Code GenerationBasic Runtime Checks and set it to default.

Page 18: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

Also set Buffer Security Check to No.

Now rebuild and start debugging. The assembly should be significantly less cluttered.

Page 19: Computer Security Group at RPIsecurity.cs.rpi.edu/courses/malware-spring2013/asmtut2.docx · Web viewThese are security features added to our program by Microsoft. We can remove some

There is a repository of shellcode at http://www.shell-storm.org/shellcode/

See what you can get to run on your computer. I will have the third tutorial up shortly that will show how to take control of a vulnerable program and inject shellcode into it.