Pwncontext
PwnContext
A context class for managing pwn tools state, including IO, ELF, libc, and exploitation helpers.
Attributes:
| Name | Type | Description |
|---|---|---|
io |
IOContext
|
The IO context for interacting with the target. |
elf |
ELF
|
The ELF binary being exploited. |
libc |
ELF
|
The libc library being used. |
prefix |
str
|
Prefix for sending/receiving data. |
_offset |
int
|
Cached offset for buffer overflows. |
_canary |
int
|
Cached canary value. |
Source code in src/pwninit/helpers/pwncontext.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
canary
property
writable
Get the canary value for the current process.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The canary value, or None if not found. |
offset
property
writable
Get the offset for buffer overflows by sending a cyclic pattern and analyzing the corefile.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The offset value. |
binsh()
Find the address of /bin/sh in libc.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The address of |
Source code in src/pwninit/helpers/pwncontext.py
482 483 484 485 486 487 488 489 | |
bof(data, opt=None, bp=None, **kwargs)
Generate a buffer overflow payload with optional canary and base pointer. Canary and offset are set using ctx.offset and ctx.canary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
The data to include in the payload. |
required | |
opt
|
dict
|
Optional overrides for specific offsets. |
None
|
bp
|
Base pointer value to include. |
None
|
|
**kwargs
|
Additional arguments for |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
The generated payload. |
Example:
>>> ctx.offset = 128
>>> bof(b'TEST')
b'aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaazaabbaabcaabdaabeaabfaabgaabTEST'
Source code in src/pwninit/helpers/pwncontext.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
check_leaks(leak_val)
Check if a leaked value corresponds to a known memory region (e.g., libc, elf, canary).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
leak
|
int
|
The leaked value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(name, base) where |
Source code in src/pwninit/helpers/pwncontext.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
format_string(n=100)
Exploit a format string vulnerability to leak memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
The index of the payload in the output. |
Source code in src/pwninit/helpers/pwncontext.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
fsopsh(func=None, arg=b'/bin/sh\x00', file=None, trigger=XSPUTN, lock=None, chain=None)
Generate a fsop payload to call a function (usually system("/bin/sh"))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
int
|
Address of the function to be called, libc's system by default |
None
|
arg
|
bytes
|
First argument of the call, /bin/sh by default |
b'/bin/sh\x00'
|
file
|
int
|
Address of the file structure, libc's stdout by default |
None
|
trigger
|
int
|
Vtable entry to trigger call on, XSPUTN by default |
XSPUTN
|
lock
|
int
|
Value to put as lock (an empty zone), file+0x800 by default |
None
|
Example:
>>> fsopsh()
b'\x01\x01\x01\x01\x01\x01\x01;/bin/sh\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x10\xfb\xd1\r\xadU\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x10\xf3\xd1\r\xadU\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x90\xb2d\xf9\x08\x7f\x00\x00@\x1f~\xf9\x08\x7f\x00\x00x\xf3\xd1\r\xadU\x00\x00'
Source code in src/pwninit/helpers/pwncontext.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
leak(leak_data, leaked=0, name='')
Parse and log a memory leak, optionally assigning it to a context variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
leak
|
bytes or str
|
The leaked data. |
required |
leaked
|
int
|
Value to subtract from the leak. |
0
|
name
|
str
|
Name of the variable to assign the leak to (e.g., "libc", "elf"). |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
The parsed leak value. |
Example:
>>> stack = leak(b'[LEAK] The address of cmd where you are writing to is: 0x7ffeab2e0b90')
[*] [stack]: leak = 0x7ffc710958d0
>> print(hex(stack))
0x7ffc710958d0
Source code in src/pwninit/helpers/pwncontext.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
resolve(symbol)
Resolve a symbol to an address in either the ELF or libc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str or int
|
The symbol name or address. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
The resolved address, or None if not found. |
Source code in src/pwninit/helpers/pwncontext.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
ret2libc(ret=True, **kwargs)
Generate a payload to call system("/bin/sh") using libc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ret
|
bool
|
If ropchain adds a ret before the start of the rop |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
The generated payload. |
Source code in src/pwninit/helpers/pwncontext.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
ret2plt(func='puts', ret2main='main', ret=True, **kwargs)
Generate a payload to leak a libc address using the PLT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
str
|
The function to leak (default: "puts"). |
'puts'
|
ret2main
|
str
|
The function to return to after leaking (default: "main"). |
'main'
|
ret
|
bool
|
If ropchain adds a ret before the start of the rop |
True
|
**kwargs
|
Additional arguments for |
{}
|
Source code in src/pwninit/helpers/pwncontext.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
ret2shellcode(addr, ret=True, **kwargs)
Generate a payload to return to shellcode at the specified address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
addr
|
int
|
The address of the shellcode. |
required |
ret
|
bool
|
If ropchain adds a ret before the start of the rop |
True
|
**kwargs
|
Additional arguments for |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
The generated payload. |
Example:
>>> ctx.offset = 128
>>> payload = ret2shellcode(0x0)
[*] Loaded 12 cached gadgets for './ch15'
[*] ROP :
0x0000: 0x804835a ret
0x0004: 0x25 0x25()
0x0008: 0x804835a ret
>>> payload
b'\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x00\x10\x00\x00jhh///sh/bin\x89\xe3h\x01\x01\x01\x01\x814$ri\x01\x011\xc9Qj\x04Y\x01\xe1Q\x89\xe11\xd2j\x0bX\xcd\x80gaabZ\x83\x04\x08%\x00\x00\x00Z\x83\x04\x08'
Source code in src/pwninit/helpers/pwncontext.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
ret2win(win, params=None, ret=True, **kwargs)
Generate a payload to call a win function with the specified parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
win
|
str or int
|
The |
required |
params
|
list
|
Arguments to pass to the |
None
|
ret
|
bool
|
If ropchain adds a ret before the start of the rop |
True
|
**kwargs
|
Additional arguments for |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
The generated payload. |
Source code in src/pwninit/helpers/pwncontext.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
ropchain(chain, ret=True)
Generate a ROP chain for the specified chain of function calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chain
|
dict
|
A dictionary mapping function names to their arguments. |
required |
ret
|
bool
|
If True, add a |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
The generated ROP chain. |
Example:
>>> payload = ropchain({"shell": []}})
[*] ROP :
0x0000: 0x804835a ret
0x0004: 0x8048516 shell()
0x0008: 0x804835a ret
>>> payload
b'Z\x83\x04\x08\x16\x85\x04\x08Z\x83\x04\x08'
Source code in src/pwninit/helpers/pwncontext.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |