đź“”
Cyber Security Notes
  • Introduction
  • CVEs
    • CVE-2022-33106
  • Paper Reviews
    • Imperfect Forward Secrecy: How Diffie-Hellman Fails in Practice
  • Security Basics Notes
    • Identification, Authentication and Authorization
  • Enumeration and Initial Compromise
    • Methodology
    • Footprinting
    • Network Protocols
      • FTP
      • SMB
      • DNS
      • NFS
      • SMTP
      • IMAP/POP3
      • SNMP
      • MySQL
      • MSSQL
      • Oracle TNS
      • IPMI
    • Nifty One Liners
    • Brute-Force Web Pages
      • Hydra
    • Network Pentest
      • Quick SMB cheatsheet
      • SSH keypair basics
      • Compromise using SSH Key
      • Networking fundamentals Interview topics
      • nmap quick cheatsheet
      • Metasploit Quick Reference
    • Web Pentest
      • Web Pentest Interview top topics
      • Wordpress Exploitation
      • Joomla Exploitation
      • Login Bypass using Cookie Tampering/Poisoning
      • Subdomain Enumeration
      • CSRF mitigation
      • XSS mitigation
      • CSP bypass with JSONP
      • PHP Vulnerabilities
      • Python Serialization Vulnerabilities - Pickle
      • SQL Injections
        • SQLmap
      • SSTI
      • XSS
    • Buffer Overflow Prep
      • Understanding CPUs
      • Virtual Memory and Paging
      • Syscalls
      • Theorem Proving
      • Stripping readable function names
      • Insecure C functions
      • Stack Canaries
      • Linking - GOT,PLT
      • Return Oriented Programming
    • Active Directory - Basics
      • AD DS
      • Managing OUs
      • Group Policies
      • Authentications
      • Trees, Forests and Trusts
      • Kerberos
      • Attacking Kerberos
      • Priv Esc (Post Exploitation)
    • DNS/Domain Enum Masterguide
  • Post Exploitation
    • Shell Escape Techniques
    • Getting stable shell after compromise
    • Linux Privilege Escalation
      • Sudoers file
      • Sudoers entry - Yum
      • Wildcards - Basics
      • Wildcards - Chown
      • Wildcards - Tar
      • Linux Permissions & SUID/SGID/Sticky Bit
      • SUID - nmap
      • SUID - bash
      • SUID - man
      • NFS no_root_squash
      • SUID - pkexec
      • Bad permissions
    • Windows Privilege Escalation
      • SeImpersonatePrivilege Token Impersonation
      • Firefox Creds
      • Potatoes
      • Print Spooler Basics
      • Print Spooler CVE 2020-1030
      • SpoolFool
    • Data Exfiltration Post Exploitation
  • Port Forwarding Cheatsheet
  • Powershell Essentials
    • Powershell Basics
    • Powershell Enumeration
    • Powershell Port Scanner
    • Powershell One Liner Port Scanning
    • Powershell Port Scan in a given CIDR
  • Application Security
    • System Calls in Linux
    • Buffer Overflow Defenses
    • Format string vulnerabilities
    • Sample Github Actions
    • Basic Bugs in Demo Application
    • Using AFL++
  • Linux 64-bit Assembly
    • GDB Basics
      • My relevant GDB cheatsheet
      • Task 1 - Tamper strcmp logic
      • Breakpoints
      • Always starting with intel flavor
      • GDB TUI Mode
    • Basic Hello World Program
    • Registers in 64-bit
    • global directive
    • Reducing instructions and Removing NULL-> Optimizing memory in Assembly
    • Data Types
    • Endianness
    • Moving Data
    • push, pop, and the stack
    • Analysis - Writing data on memory location and referencing
    • Arithmetic Operations
    • Bitwise Logical Operations
    • Bit-Shifting Operations
    • Control Instructions
    • Loops
    • Procedures
    • Stack-Frames and Procedures
    • String Operations
    • Shellcoding basics
      • Introduction and Common Rules
      • Basic Shellcodes->Exit
      • Testing shellcode->Skeleton Code
      • Techniques-> JMP,CALL,POP
      • Techniques-> Stack
      • Techniques-> (64-bit only) RIP Relative Addressing
      • Shellcode 1 -> execve(/bin/sh) STACK PUSH
      • Shellcode 1 -> execve(/bin/sh) JMP CALL POP
      • Techniques-> XOR-Encoder
  • Cloud Security
    • Foundational Technology
    • Learning Through Project Omega
    • IAM Essentials
      • Deep dive into IAM - Part 1
    • Amazon S3
    • Risk Management & Data Controls
    • Enumeration
      • S3 - Enum Basics - PwnedLabs
      • S3 - Identify the AWS Account ID from a Public S3 Bucket
      • EBS - Loot Public EBS Volumes
      • S3- Exploit Weak Bucket Policies for Privileged Access
  • API Security
    • WSDL
  • Reverse Engineering
    • Some string Operations
    • Numbers and Inputs
    • Address inputs
    • Recursive Function
    • Crackme: level1
    • Crackme: level2
    • CTF: Memory Dereferencing
    • CTF: Monty Python
  • CTF Challenge Learnings
    • vsCTF 2024
      • Sanity Check
      • not-quite-caesar
      • Intro to reversing
    • NCL Individual 2024
      • Web Challenges
        • PiratePals
        • Pierre's Store
    • Pico CTF 2024
      • Web Exploitation
        • Bookmarklet
        • WebDecode
        • Unminify
        • Trickster
      • General Skills
        • Commitment Issues
        • Time Machine
        • Blame Game
        • Collaborative Development
        • Binary Search
        • Dont-you-love-banners
    • Sunshine CTF
      • Knowledge Repository
    • Amazon WiCys CTF
      • I am Lazy
      • Password Locker on the Web
      • Happy Birthday Card Generator
      • Bloggergate
      • simple offer
      • Bad Actor
      • Secret Server
      • Simple PCAP
      • Hidden Message
    • C code using getenv()
    • Command Injection with filter
    • Pwning
      • Shoddy_CMP
      • PLT_PlayIT
  • Applied Cryptography
    • Linear Congruential Generator
  • Tools for everything
Powered by GitBook
On this page

Was this helpful?

  1. Enumeration and Initial Compromise
  2. Buffer Overflow Prep

Return Oriented Programming

PreviousLinking - GOT,PLTNextActive Directory - Basics

Last updated 1 year ago

Was this helpful?

In most challenges so far, NX was disableed, or memory address of a function to jump to was there or system() was there to use.

What to do in the case we don't have these assistances?

That's when ROP comes in to play

ROP is a code resuse attack - We don't need our own shellcode (but we can if we want). We modify the control flow in a way that executes what we want, using nothing but simple "ret" priimitives.

Concept at a high-level:

○ you have smashed the stack, but it’s non-executable

â—‹ there is no address you can jump to in order to pop a shell

â—‹ but, you can still ret somewhere and use the stack data to do other things (i.e., pop)

â—‹ ...and you can ret again after that

○ …...and after that

○ ……..and after that

Reference:

_______________________________________________________________________________________________________

How often does a program contain a sequence of instructions that may help in a shell? Like system() instruction somewhere.

Functions aren’t really a “thing” in x86…

○ You can jump to the middle of a function, and that’s all fine! ● Instructions are variable-length in x86…

â—‹ From 1 to 15 bytes for an instruction

○ You can jump into the middle of an instruction, and that’s fine!

â—‹ Jumping at different points yields different opcodes, which means we have a lot more instructions to use than the programmer intended

â—Ź If we can smash the callstack, we can control return pointers

â—Ź That means we can do evil things like return, then return again, etc...

_______________________________________________________________________________________________________

x86 only kind of understands functions. What it really understands is the "call stack"

->call and ret instructions push and pop to the callstack, for instance

â—Ź What does the ret instruction really do?

->pop rip

â—‹ If we control the stack, we control what it pops. That means we can control multiple returns, each one making small changes

_______________________________________________________________________________________________________

For example:

An instruction is:

mov rax, 0xc30f05 ; ret == 48 c7 c0 05 0f c3 00 c3

Which is moving an address in RAX and then returning to an address.

If we jump IP to just 05 0f c3, we can get a "syscall ; ret"

_______________________________________________________________________________________________________

IMportant thing to note here is that when using POP, essentially the value on the stack currently is assigned to the register told by POP.

Let me explain this with an example. I want to make a program execute: execve(“/bin/sh”, NULL, NULL).

execve is the 59th system call. Which is 0x3b in hex.

Assuming the string "/bin/sh" is available at address 0x600000

Can we make a shellcode only using ROPgadgets to make the program execute a shell?

Now, interesting thing about POP is that it basically sets a register.

  • "pop" removes the last value pushed onto the stack, and stores it in a register.

  • For example, this loads 3 into rax and returns. It's a kinda roundabout way to return a 3, but between the push and the pop you could use rax for something else until you need it.

    push 3
    pop rax
    ret

So essentially by lining up Gadgets with addresses to different pop instructions we can make our own ROP shellcode.

In the question above, need these 6 instructions in this sequence to make our shellcode

A. pop rdi; ret -> "/bin/sh" string's memory location

B. pop rsi; ret ->2nd NULL argument of execve

C. pop rdx;ret -> 3rd NULL argument of execve

D. pop rax;ret -> number assigned here would tell system which syscall. 0x3b is execve

E. syscall; ret -> location of syscall in memory somewhere

  • where A,B,C,D,E are memory addresses of these instructions

Then since we know that POP will assign a register with a value on top of the stack, we can craft a payload.

Let's say EIP offset is 42.

Payload would become:

42*A's + A + 0x600000 + B + 0x0 + C + 0x0 + D + 0x3b + E

And this is how we will essentially create a payload that would execute this command: execve(“/bin/sh”, NULL, NULL)

Assembly would be:

mov rdi, 0x600000 
mov rsi, 0 
mov rdx, 0 
mov rax, 0x3b 
syscall

_______________________________________________________________________________________________________

Again, this begs the doubt-- what if these instructions are not in my binary?

A binary could be small with not many gadgets to play around with. What to do in such case?

As we know that all binaries have linked libraries ( explained in the linking - GOT,PLT page in this section). And these libraries are huge. They have loads and loads of gadgets. One can essentially use these gadgets in their shellcode.

A program that helps finding these little gadgets:

Well, from the website: for execve, first argument we need to set rdi to the 0x600000 location. Then we need to set rsi and rdx to NULL (0x0) and finally we need to set RAX to 0x3b (system call execve's number in hex). We know that all system calls are executed using syscall assembly function. At that time, RAX should be 0x3b to execute execve.

Reference:

https://hovav.net/ucsd/dist/geometry.pdf
https://github.com/JonathanSalwan/ROPgadget
https://filippo.io/linux-syscall-table/
https://www.cs.uaf.edu/2017/fall/cs301/lecture/09_08_stack.html