Skip to content

Draft of UMassCTF pwn/factory-monitor#93

Open
egkushelevsky wants to merge 1 commit intoCUCTF:mainfrom
egkushelevsky:factory_monitor
Open

Draft of UMassCTF pwn/factory-monitor#93
egkushelevsky wants to merge 1 commit intoCUCTF:mainfrom
egkushelevsky:factory_monitor

Conversation

@egkushelevsky
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
Contributor

@mmstoic mmstoic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Review the comments and you're good to go!


After creating a machine, we can start running it with the `start()` function. This function forks a child process for the machine, fills in the machine‘s PID and changes its state to `STATE_RUNNING`, and sets up bidirectional communication with parent process using the pipes. Once setup is complete, the machine calls its `main_func`, which by default is the `machine_main_demo()` function reproduced below.

```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you briefly explain what the code is doing too?

}
```

There are two key insights from this function.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

## Exploit
The buffer passed to `read_line_fd()` from the machine is stack allocated in `machine_main_demo()`, so we can deterministically write past it to reach the return address saved on the stack and overwrite whither we return.

Since our ultimate goal is to get the flag from the `flag.txt` file, we must find a sequence of commands to open the file, read its contents, and print the flag. We can see from the symbol table that the `open()` function is imported at offset `0x38a40` from the start of the executable, and we have the option of `read()` or `read_line_fd()` to read the flag.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe drop an image of the symbol table?


Since our ultimate goal is to get the flag from the `flag.txt` file, we must find a sequence of commands to open the file, read its contents, and print the flag. We can see from the symbol table that the `open()` function is imported at offset `0x38a40` from the start of the executable, and we have the option of `read()` or `read_line_fd()` to read the flag.

Because the program’s stack is not executable (seen through `readelf -l`), we cannot directly execute shellcode to run these functions. Instead, we can use existing instruction sequences from the programmer’s `.text` section, which is executable, to build an ROP chain and execute our own commands. The binary has sequences to pop a value into `rdi` at `0xc028` (`5f 5d c3`) and into `rsi` at `0x15bc7` (`5e 5d c3`). However, there is no gadget which would give us `rdx` (`5a 5d c3`), so we have to use the two-argument `read_line_fd()` rather than the three-argument `read()`. We will also use a `ret` instruction from `0xa382` to align the stack before calling our functions.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain briefly how read_line_fd is used to give us rdx?

This script, `exploit.py`, gets the flag:
```
UMASS{AsLR_L3Ak}
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great writeup! Only thing I would add is that maybe a diagram or some visual of the ROP chain would be helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants