Pwncontext
PwnContext
A context class for managing pwntools state, including IO, ELF binaries, libc libraries, and common exploitation helpers.
Attributes:
| Name | Type | Description |
|---|---|---|
io |
IOContext
|
The active IO connection or process context. |
config |
Config
|
The configuration setup containing binaries and paths. |
elf |
ELF
|
The binary used. |
libc |
ELF
|
The libc used. |
libs |
ELF
|
The libs used. |
Source code in src/pwninit/pwncontext.py
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 | |
canary
property
writable
Get the canary value for the current process from /proc auxv.
Returns:
| Type | Description |
|---|---|
int | None
|
int | None: The canary value, or None if not found/applicable. |
offset
property
writable
Find the buffer overflow offset dynamically by sending a cyclic pattern and reading the corefile fault address.
Returns:
| Type | Description |
|---|---|
int | None
|
int | None: The found offset length. |
__init__(io)
Initializes the PwnContext with tracking objects and wraps target binaries as ELF objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
io
|
IOContext
|
The current process execution or remote network context wrapper. |
required |
Source code in src/pwninit/pwncontext.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
binsh()
Locate the string constant value of /bin/sh matching references across libc targets.
Source code in src/pwninit/pwncontext.py
472 473 474 | |
bof(data, opt={}, bp=0, **kwargs)
Generate a basic buffer overflow payload injecting optional canary or base pointers based on binary arch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | int
|
Intended execution payload control destination (e.g., return address). |
required |
opt
|
dict
|
Specific index manual offset dictionary adjustments. |
{}
|
bp
|
int
|
Target base pointer (RBP/EBP) replacement value. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Fully structured flat stream buffer padding. |
Example:
>>> ctx.offset = 40
>>> bof(0x401196)
b'aaaabaaacaaadaaaeaaafaaa...\x96\x11\x40\x00\x00\x00\x00\x00'
Source code in src/pwninit/pwncontext.py
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 | |
check_leak(leaked)
Match a raw memory leak value against known virtual memory regions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
leaked
|
int
|
The raw memory address leaked. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
A (name, base_address) pair if a region is matched, else (None, None). |
Source code in src/pwninit/pwncontext.py
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 | |
find_leak(buf)
Extract and isolate an address integer out of standard text or binary buffers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buf
|
int | str | bytes
|
Raw buffer chunk containing the potential leak. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The isolated absolute leak value. |
Example:
>>> find_leak(b'\x00\x00\x00\x9d{\xdar\x90 \xf2\x10.\xf2\x92\xff\x7f\x00\x00\xeeo\x9f\r\x1bV\x00\x00\xd3\x05\x00\x00\x00\x00\x00\x00\x00vu?\xfb\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x9d{\xdar\x90 \xf2\x01\x00\x00\x00\x00\x00\x00\x00\xa8LU?\xfb\x7f\x00\x00\x10/\xf2\x92\xff\x7f\x00\x00zo\x9f\r\x1bV\x00\x000\xf0p?\x01\x00\x00\x00(/')
[*] [canary]: leak[3:10]
[*] [canary]: leak[4:10]
[*] [stack]: leak[10:16]
[*] [boring]: leak[18:24]
[*] [ld-2]: leak[34:40]
[*] [canary]: leak[51:58]
[*] [canary]: leak[52:58]
[*] [libc-2]: leak[66:72]
[*] [stack]: leak[74:80]
[*] [boring]: leak[82:88]
Source code in src/pwninit/pwncontext.py
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 | |
format_string(n=100)
Find the format string offset.
Example:
>>> format_string(n=50)
6
Source code in src/pwninit/pwncontext.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
fsopsh(func='system', arg=b'/bin/sh\x00', file='_IO_2_1_stdout_', trigger=XSPUTN, lock=0, chain=0)
Generate file stream objects to get an arb call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
str | int
|
Target destination routine location address values. |
'system'
|
arg
|
bytes | str
|
Variable string argument properties. |
b'/bin/sh\x00'
|
file
|
str | int
|
Stream object description table base points. |
'_IO_2_1_stdout_'
|
Example:
>>> fsopsh(func="win", file="_IO_2_1_stderr_")
Source code in src/pwninit/pwncontext.py
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 | |
leak(leaked, offset=0, name='')
Parse, apply math adjustments, map to segments, and log an expected leak.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
leaked
|
int | str | bytes
|
Raw string containing a leak, or the address directly. |
required |
offset
|
int
|
Base offset value to subtract from the parsed address. |
0
|
name
|
str
|
Enforce mapping assignment to a known identifier (e.g., "libc"). |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The normalized leak address value. |
Example:
>>> stack = leak(b"b'] The address of cmd where you are writing to is: 0x7fff121e12d0'")
[*] [stack]: 0x7fff121e12d0
>>> hex(stack)
0x7fff121e12d0
>>> libc.address = leak(b"puts address: 0x7ffff7e114a0", offset=libc.sym['puts'])
Source code in src/pwninit/pwncontext.py
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 | |
resolve(symbol)
Resolve a symbol or offset expression within the known ELF context, libc, or extra libraries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | int
|
The symbol name, structural math, or absolute address. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The resolved memory address. |
Example:
>>> ctx.resolve("main")
0x401196
>>> ctx.resolve("system+0x10")
0x7ffff7e12390
Source code in src/pwninit/pwncontext.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
ret2libc(ret=True, **kwargs)
Generate a ret2libc payload.
Example:
>>> ret2libc()
Source code in src/pwninit/pwncontext.py
386 387 388 389 390 391 392 393 394 395 | |
ret2plt(func='puts', ret2main='main', ret=True, **kwargs)
Generate a payload that call func(got[func]), usefull to defeat PIE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
str | int
|
PLT mapping reference to extract details via. |
'puts'
|
ret2main
|
str | int
|
Destination structure to route towards immediately following. |
'main'
|
Example:
>>> ret2plt(func="printf", ret2main="main")
Source code in src/pwninit/pwncontext.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
ret2shellcode(addr, ret=True, **kwargs)
Generate a shellcode and a ropchain to call it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
addr
|
int | str
|
Target point reference calculation indicator context. |
required |
ret
|
bool
|
Include initial stack alignment layout properties. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Complete payload string bytes. |
Example:
>>> ret2shellcode("bss_target")
b'\x90\x90...jhh///sh/bin...'
Source code in src/pwninit/pwncontext.py
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=[], ret=True, **kwargs)
Generate a ret2win payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
win
|
str | int
|
Name identifier or absolute function target. |
required |
params
|
list | tuple
|
Argument values to associate onto target registers. |
[]
|
ret
|
bool
|
Append structural target ret properties. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Assembled operational byte blocks. |
Example:
>>> ret2win("win_secret_func", params=[0xdeadbeef, 0xcafebabe])
Source code in src/pwninit/pwncontext.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
ropchain(chain, ret=True)
Construct a compiled ROP chain given target calls and setup states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chain
|
dict
|
Function labels or addresses mapped to parameter list configurations. |
required |
ret
|
bool
|
Insert stack aligning |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The assembled payload sequence. |
Example:
>>> ropchain({"puts": [0x404000], "main": []})
b'\xaa\xbb...'
Source code in src/pwninit/pwncontext.py
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 | |