|
| 1 | +# Pwntools Tutorial |
| 2 | + |
| 3 | +## level 1.0 |
| 4 | + |
| 5 | +```c |
| 6 | +int bypass_me(char *buf) |
| 7 | +{ |
| 8 | + unsigned int magic = 0xdeadbeef; |
| 9 | + |
| 10 | + if (!strncmp(buf, (char *)&magic, 4)) { |
| 11 | + return 1; |
| 12 | + } |
| 13 | + |
| 14 | + return 0; |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +```python |
| 19 | +from pwn import * |
| 20 | +
|
| 21 | +context(arch="amd64", os="linux", log_level="info") |
| 22 | +
|
| 23 | +challenge_path = "/challenge/pwntools-tutorials-level1.0" |
| 24 | +p = process(challenge_path) |
| 25 | +
|
| 26 | +payload = p64(0xdeadbeef)+b"\n" |
| 27 | +p.sendafter(b":)\n###\n", payload) |
| 28 | +
|
| 29 | +flag = p.recvline() |
| 30 | +print(f"flag is: {flag}") |
| 31 | +``` |
| 32 | + |
| 33 | +## level 1.1 |
| 34 | + |
| 35 | +```c |
| 36 | +int bypass_me(char *buf) |
| 37 | +{ |
| 38 | + int flag = 1; |
| 39 | + int num; |
| 40 | + |
| 41 | + if (buf[0] != 'p' || buf[1] != 0x15) { |
| 42 | + flag = 0; |
| 43 | + goto out; |
| 44 | + } |
| 45 | + |
| 46 | + memcpy(&num, buf + 2, 4); |
| 47 | + if (num != 123456789) { |
| 48 | + flag = 0; |
| 49 | + goto out; |
| 50 | + } |
| 51 | + |
| 52 | + if (strncmp(buf + 6, "Bypass Me:)", 11)) { |
| 53 | + flag = 0; |
| 54 | + goto out; |
| 55 | + } |
| 56 | + |
| 57 | +out: |
| 58 | + return flag; |
| 59 | +} |
| 60 | +``` |
| 61 | +
|
| 62 | +```python |
| 63 | +from pwn import * |
| 64 | +
|
| 65 | +context(arch="amd64", os="linux", log_level="info") |
| 66 | +
|
| 67 | +challenge_path = "/challenge/pwntools-tutorials-level1.1" |
| 68 | +p = process(challenge_path) |
| 69 | +
|
| 70 | +payload = b"p" |
| 71 | +payload += p8(0x15) |
| 72 | +payload += p32(123456789) |
| 73 | +payload += b"Bypass Me:)" |
| 74 | +payload += b"\n" |
| 75 | +p.sendafter(b":)\n###\n", payload) |
| 76 | +
|
| 77 | +flag = p.recvline() |
| 78 | +print(f"flag is: {flag}") |
| 79 | +``` |
| 80 | + |
| 81 | +## level 2.0 |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +```python |
| 86 | +while True: |
| 87 | + print(p.recvline()) |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +```python |
| 93 | +from pwn import * |
| 94 | + |
| 95 | +def print_lines(io): |
| 96 | + info("Printing io received lines") |
| 97 | + while True: |
| 98 | + try: |
| 99 | + line = io.recvline() |
| 100 | + success(line) |
| 101 | + except EOFError: |
| 102 | + break |
| 103 | + |
| 104 | +context(arch="amd64", os="linux", log_level="info") |
| 105 | + |
| 106 | +challenge_path = "/challenge/pwntools-tutorials-level2.0" |
| 107 | +p = process(challenge_path) |
| 108 | + |
| 109 | +payload = asm("mov rax, 0x12345678") |
| 110 | + |
| 111 | +p.sendafter(b'Please give me your assembly in bytes \x1b[1m(\x1b[0mup to \x1b[1;36m0x1000\x1b[0m bytes\x1b[1m)\x1b[0m: \n', payload) |
| 112 | +print_lines(p) |
| 113 | +``` |
| 114 | + |
| 115 | +## level 2.1 |
| 116 | + |
| 117 | +```text |
| 118 | +In this level you need to craft assembly code to satisfy the following |
| 119 | +conditions: |
| 120 | +* exchange the value of rax and rbx |
| 121 | +``` |
| 122 | + |
| 123 | +```python |
| 124 | +payload = asm("xchg rax, rbx") |
| 125 | +``` |
| 126 | + |
| 127 | +## level 2.2 |
| 128 | + |
| 129 | +```text |
| 130 | +In this level you need to craft assembly code to complete the following |
| 131 | +operations: |
| 132 | +* rax = rax % rbx + rcx - rsi |
| 133 | +
|
| 134 | +We already set the following in preparation for your code: |
| 135 | +rdx = 0 |
| 136 | +``` |
| 137 | + |
| 138 | +```python |
| 139 | +payload = asm("div rbx") # computes (rdx:rax) / rbx → quotient in rax, remainder in rdx |
| 140 | +payload += asm("mov rax, rdx") # rax = rax % rbx |
| 141 | +payload += asm("add rax, rcx") # rax = rax % rbx + rcx |
| 142 | +payload += asm("sub rax, rsi") # rax = rax % rbx + rcx - rsi |
| 143 | +``` |
| 144 | + |
| 145 | +## level 2.3 |
| 146 | + |
| 147 | +```text |
| 148 | +In this level you need to craft assembly code to complete the following |
| 149 | +operations: |
| 150 | +* copy 8-bytes memory starting at 0x404000 to 8-bytes memory starting at |
| 151 | +0x405000 |
| 152 | +``` |
| 153 | + |
| 154 | +```python |
| 155 | +payload = asm(""" |
| 156 | + mov rax, [0x404000] |
| 157 | + mov [0x405000], rax |
| 158 | +""") |
| 159 | +``` |
| 160 | + |
| 161 | +## level 2.4 |
| 162 | + |
| 163 | +```text |
| 164 | +In this level you need to craft assembly code to complete the following |
| 165 | +operations: |
| 166 | +* the top value of the stack = the top value of the stack - rbx |
| 167 | +
|
| 168 | +Tips: perfer push and pop instructions, other than directly dereference |
| 169 | +``` |
| 170 | + |
| 171 | +```python |
| 172 | +payload = asm(""" |
| 173 | + pop rax |
| 174 | + sub rax, rbx |
| 175 | + push rax |
| 176 | +""") |
| 177 | +``` |
| 178 | + |
| 179 | +## level 2.5 |
| 180 | + |
| 181 | +```text |
| 182 | +In this level you need to craft assembly code to complete the following |
| 183 | +operations: |
| 184 | +* the top value of the stack = abs(the top value of the stack) |
| 185 | +``` |
| 186 | + |
| 187 | +```python |
| 188 | +payload = asm(""" |
| 189 | + pop rax |
| 190 | + test rax, rax |
| 191 | + jns done |
| 192 | + neg rax |
| 193 | +done: |
| 194 | + push rax |
| 195 | +""") |
| 196 | +``` |
| 197 | + |
| 198 | +## level 2.6 |
| 199 | + |
| 200 | +```text |
| 201 | +In this level you need to craft for statement to complete the following |
| 202 | +operations: |
| 203 | +* rax = the sum from 1 to rcx |
| 204 | +``` |
| 205 | + |
| 206 | +```python |
| 207 | +payload = asm(""" |
| 208 | + xor rax, rax |
| 209 | + mov rbx, 1 |
| 210 | +loop_start: |
| 211 | + cmp rbx, rcx |
| 212 | + jg loop_end |
| 213 | + add rax, rbx |
| 214 | + inc rbx |
| 215 | + jmp loop_start |
| 216 | +loop_end: |
| 217 | +""") |
| 218 | +``` |
0 commit comments