sneha rajguru prajwal panchmahalkar - def con con 26/def con 26... · sneha rajguru security...

52
Sneha Rajguru & Prajwal Panchmahalkar

Upload: others

Post on 24-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Sneha Rajguru&Prajwal Panchmahalkar

Page 2: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Sneha Rajguru

Security Consultant, Payatu Technologies Pvt Ltd. @sneharajguru

Prajwal Panchmahalkar

Red Team Lead Security Engineer, VMware @pr4jwal

Page 3: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Introduction to ARM CPU± Architecture± Registers± Modes of Operations

µ ARM Assembly Language± Instruction Set

µ Introduction to ARM functions and workingµ Debugging on ARMµ Stack Overflow on ARMµ How to write a shellcodeµ How to reverse a shellcode

Page 4: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Raspbian running on Qemu± Qemu Kernel for Raspbian Jessie± Raspbian Jessie Image± Qemu-system arm

Page 5: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ SSH and Network

On Ubuntu (Host):

sudo tunctl -t tap0 -u ubuntusudo ifconfig tap0 172.16.100.1/24

µ Start Qemu and Setup network:

sudo qemu-system-arm -kernel ./kernel-qemu-4.4.34-jessie -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda raspbian-jessie-lite.qcow -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -net nic -net tap,ifname=tap0,script=no,downscript=no

On Raspbian(Guest):sudo ifconfig 172.16.100.2/etc/init.d/ssh start //starts SSH on port 2222

Page 6: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Most of the smartphones, smart TVs and many other embedded devices, have the ARM processors (even laptops)

µ ARM is RISC based architecture (Reduced Instruction Set Computer)± And the name Advanced RISC Machine

µ The use is growing significantlyµ Many vulnerabilities, reusable code and memory corruption errors

Page 7: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ 37 Registers

µ 16 visible general purpose registers starting from R0-R15.

µ Out of these 16, five of them are for special purposes.

µ From ARM v8 is 64-bit Architecture ( the registers are 64-bit )

µ We will be conducting this session against the 32-bit architecture.

Page 8: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ ARM; a RISC instruction set architecture

µ A Fixed instruction set 32-bits

µ Multiple 32-bits registers

µ 3 address instruction format

µ Typically ‘fetch-decode-execute’ format

Page 9: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

The instruction cycle:

Fetch Decode Execute Repeat

Start Fetch Next Instruction Execute Stop

Page 10: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

The states of ARM :

µ Instruction Set state± ARM, THUMB, THUMBEE, JAZELLE; determines the instruction set which is being executed± THUMB - 16 bit registers± THUMBEE - Thumb Execution Environment ± Jazelle DBX - Allows direct java bytecode execution (limited to some processors)

µ Execution set state± Usually for decoding the instructions stream, as it consists of instruction set state and

control bits. µ Debug state

± Enters a debug event, when processor is being halted for debug processµ Security state

± There are two sub states :- (i). Secure State & (ii). Non-secure state.

Page 11: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Modes Provides a set of modes for software execution and to handle exceptions. It

handles� The registers available for use� Privilege level of executing software

± Privilege level - (Non-Secure State) � PL0 - Software is executed in User mode (least privileges)� PL1 - Software is executed in any other mode apart from User mode. � PL2 - software is executed in hype mode,

± Privilege level - (Secure State)� PL0 - Software is executed in user mode ( Low privileges) � PL1 - Software is executed in any mode other than user mode.

Page 12: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

REGISTERS

Page 13: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ RISC vs CISC

µ General registers: eax, ebx, ecx, edx and +++

µ Special registers: esp, ebp, eip and +++

µ Segment registers (SS, DS and etc)

µ eip is special, you can’t set a value to eip for example eax - 4byte, ax - 2byte, al -1byte eip

Page 14: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ From R0 to R15

µ R15 is used like EIP (called PC)

µ R14 is Link-Register (called LR)

µ R13 is ESP (called SP)

µ But all registers including R13 to R15 are completely general

µ Can set a value to R15 directly (Impossible on x86)

µ Current program status register (CPSR)

µ Privileged mode can also access saved program state register (SPSR)

Page 15: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

The following are the core registers :

µ R0 …R15,

± CPSR registers

± R15 ~ PC (Program Counter)

� PC holds the address of the instruction being fetched i.e. when executing an ARM instruction PC will hold the address of current instruction plus 8.

� Writing to PC will cause an execution branch to the address written

± R14 ~ LR (Link Register) – Hold the return link address (function calls)

± R13 ~ SP (Stack Pointer) – Points to the process stack

± R12 ~ IP (Intra-Procedure-call scratch register)

± R11 ~ FP (Frame Pointer)

Page 16: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ The Current Program Status Register (CPSR)

µ Hold processor status and control information

µ Condition flags (Negative, Zero, Carry, overflow)

µ Instruction set state

µ Current Endianness

µ Current processor mode

µ And other control bits…

Page 17: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Saved Program Status Register (SPSR)µ Every mode that an exception can be taken to has its own saved copy of CPSRµ Register CPSR: Instruction and Flag

± NZCV – Condition flags N (Negative), Z (Zero), C(Carry), V (Overflow)± Q – Cumulative saturation bit± IT[0:7] – Thumb If-then instruction execution state bits± J – Jazelle execution state bit± Reserved – Reserved bits (Read-As-Zero/Should-Be-Zero-or-Preserved)± GE[3:0] – Greater than or Equal flags (for parallel add|sub SIMD instructions)± E – Endianness execution state bit (0 – little-endian, 1 – big-endian)± AIF – Mask bits± A (Asynchronois abort), I (IRQ), F (FIQ)± Possible values – 0 (Exception not masked), 1 (Exception masked)± T – Thumb state bit and J bit define the processor instruction set state± (J,T): ARM (0,0), Thumb (0,1), Jazelle (1,0), ThumbEE (1,1)± M[4:0] – Mode field. Determines the current processor mod

Page 18: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

1. User Mode [CSPR 10000]

2. FIQ (Fast Interrupt) [CSPR 10001]

a) R8-R15, CPSR for fast completion

3. IRQ (Normal Interrupt) [CSPR 10010]

a) R13-R14, CPSR

4. Supervisor (software instructions SVC or SYS Call) [CSPR 10011]

5. Abort (Access violations, failure to load instruction or data) [CSPR 10111]

6. Undefined (Undefined Instructions) [CSPR 11011]

Page 19: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Diagram of CSPR

Page 20: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Arithmetic operations:

µ ADD r0, r1, r2 //r0 = r1 + r2µ SUB r0, r1, r2 //r0 = r1 - r2

µ RSB r0, r1, r2 //r0 = r2 - r1µ SUB r11,r12,r3,ASR #5 //r11 = r12 - operand2 (Guess operand 2 ?)

Conditional operations:

µ CMP r0, r1 //r0 - r1µ CMN r0, r1 //r1 + r2

µ TST r0, #0x3f8 // AND

Page 21: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Logical instructions:

µ AND r0, r1, r2 // r0 = r1 AND r2µ ORR r0, r1, r2 // r0 = r1 OR r2

µ EOR r0, r1, r2 // r0 = r1 XOR r2µ BIC r0, r1, r2 // r0 = r1 AND NOT r2

Flexible Second Operand:µ MOV r0, r0, LSL #1 //Logical Shift Left, multiply r0 by 2 for second operand µ MOV r0, r0, RSL #1 //Logical Shift Right, multiply r0 by 2 for second operand µ MOV r0, r0, ROR #10 //r0 = r0 rotated right by 10 bits

Page 22: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Memory access instructions:

µ LDR r8, [r10] //load the value at memory address found in r10 to r8

µ STR r10, [r2] //store the value found in r10 into memory address found in r2.

Branch Operation:µ B labelX //Branch to Labelx (Jump to Labelx)

µ BL labelX //Branch with Link to Labelx (Jump to LabelX and return to next instruction)

µ BLX labelX // Branch with Labelx and exchange instruction set.

Labelx………..

Page 23: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

ARM ASSEMBLY LANGUAGE

Page 24: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Basics :

µ ARM assembly uses different commands compared to x86

µ ARM opcodes are of 32-bit size and are word aligned

µ Which means the addresses are divisible by 4; e.g :

µ The PC bits are always 0, as ARM instructions are always word aligned.

µ Where as, the Thumb instructions are half word aligned.

Page 25: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

ARM FUNCTIONS & WORKING

Page 26: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 27: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 28: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

DEBUGGING ON ARM

Page 29: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Quick refresher of Some Useful commands :

µ Breakpoint

µ Disassemble

µ Examine

µ Run

µ Step - single step

µ Next

µ Continue

µ Info

µ Print

µ ptype

Page 30: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 31: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 32: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

STACK OVERFLOW ON ARM

Page 33: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Main():

Page 34: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

main() iamgroot()

notgroot()

UID is 0

Page 35: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

main() iamgroot()

notgroot()

UID is non 0

Page 36: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Exploiting Overflow to execute iamgroot() as a non-root user

Page 37: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

WRITING A SHELLCODE

Page 38: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ A machine instruction µ A payload used to exploit a system - the code attacker wants to execute µ A process independent machine code

How to write a shellcode ? What are the various techniques ?

µ Shellcode depends on processor as it is a machine code µ Objdump, ldµ Machine code output can be reversed in objdump

Page 39: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

INTRODUCTION TO ARM SHELLCODE

Page 40: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 41: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Well, you can flaunt the talent of writing shell codes on your own! µ You can obviously understand .. how ARM assembly functions work, µ You can do you own exploit development research µ And not rely on the existing tools / ready shell codes µ Or no need to search exploits in ‘exploit-db’ or Google!!!

You can call yourself a l337

Page 42: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ Understand and take advantage of the system calls. µ Once we know the syscall number and format , it is easier to craft a shellcodeµ Make sure there are no null-bytes (x86)µ We shall be also using the technique in which we will put our processor in THUMB state. µ In THUMB mode, instructions are of 16 bits, allowing code density.

Page 43: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ A payload needs system interaction µ A shellcode can perform a direct system call .

µ Shellcode instructions :µ In ARM :

Mov r7, #syscallSwi #0

µ Relative addressing

µ You can inject a shellcode at any location

Page 44: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

µ All ARM instructions are of 32-bits.µ i.e they are 4 byte longµ ARM instructions include :

± Instruction± Register number± Shifter ( if any )± A constant

µ Example :± A foo± Ascii : “\x41\x41\x41\x41” .

Page 45: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Write a simple C program to print 1 to 10 numbers.

Page 46: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 47: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

REVERSE A SHELL CODE

A Reverse Shell helps in connecting an internal system to an external one. In other terms, the victim system is made to connect to the attackers system on a listener port running on the attackers system.

Page 48: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 49: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 50: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

Remember: it is important for us to know the system functions, their parameters and syscall numbers and formats.

Then,you would need to identify the specific values of the parameters selected.

To identify it, you can take help of strace

Page 51: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead
Page 52: Sneha Rajguru Prajwal Panchmahalkar - DEF CON CON 26/DEF CON 26... · Sneha Rajguru Security Consultant, PayatuTechnologies Pvt Ltd. @sneharajguru Prajwal Panchmahalkar Red Team Lead

THANK YOU