@@ -151,6 +151,21 @@ gdb-peda$
151
151
152
152
We will go through a few of the interesting commands.
153
153
154
+ ### checksec
155
+
156
+ The `checksec` command lists the protections that are enabled for the binary.
157
+ This is useful when figuring out how to craft your exploit.
158
+
159
+ ```shell
160
+ gdb-peda$ checksec
161
+ CANARY : disabled
162
+ FORTIFY : disabled
163
+ NX : ENABLED
164
+ PIE : disabled
165
+ RELRO : Partial
166
+ gdb-peda$
167
+ ```
168
+
154
169
### distance
155
170
156
171
Often, calculating offsets from addresses is required when crafting your payload
@@ -289,3 +304,157 @@ gdb-peda$
289
304
` ` `
290
305
291
306
# # Pwntools
307
+
308
+ Pwntools is a Python library that provides a framework for writing exploits.
309
+ Typically, it is used heavily in CTFs. There are a ton of useful functions
310
+ provided by Pwntools but I will briefly describe the process I personally use.
311
+
312
+ # ## Using Pwntools
313
+
314
+ There are three ways you can use Pwntools:
315
+
316
+ 1. Interactively through the python/iPython consoles
317
+ 2. In a python script
318
+ 3. Pwntools command line tools
319
+
320
+ # ### Interactively through the Console
321
+
322
+ Often, you want to try things out before actually writing an actual script when
323
+ developing your exploit. The iPython console is a great way to explore the
324
+ Pwntools API. For convenience, we will import everything in the ` pwn` package to
325
+ the global namespace.
326
+
327
+ ` ` ` shell
328
+ ubuntu@ubuntu-xenial:/vagrant/lessons/3_intro_to_tools/build$ ipython
329
+ Python 2.7.12 (default, Nov 19 2016, 06:48:10)
330
+ Type " copyright" , " credits" or " license" for more information.
331
+
332
+ IPython 5.1.0 -- An enhanced Interactive Python.
333
+ ? -> Introduction and overview of IPython' s features.
334
+ %quickref -> Quick reference.
335
+ help -> Python' s own help system.
336
+ object? -> Details about ' object' , use ' object??' for extra details.
337
+
338
+ In [1]: from pwn import *
339
+
340
+ In [2]:
341
+ ` ` `
342
+
343
+ iPython provides tab completion and a built-in system to look up documentation
344
+ in docstrings. For example, if we want to look at what the ` p32` function does,
345
+ we can look it up with the ` ? ` sigil.
346
+
347
+ ` ` ` shell
348
+ In [4]: p32?
349
+ Signature: p32(* a, ** kw)
350
+ Docstring:
351
+ p32(number, sign, endian, ...) -> str
352
+
353
+ Packs an 32-bit integer
354
+
355
+ Arguments:
356
+ number (int): Number to convert
357
+ endianness (str): Endianness of the converted integer (" little" /" big" )
358
+ sign (str): Signedness of the converted integer (" unsigned" /" signed" )
359
+ kwargs (dict): Arguments passed to context.local (), such as
360
+ ` ` endian` ` or ` ` signed` ` .
361
+
362
+ Returns:
363
+ The packed number as a string
364
+ File: /usr/local/lib/python2.7/dist-packages/pwnlib/context/__init__.py
365
+ Type: function
366
+
367
+ In [5]: p32(0x41424344)
368
+ Out[5]: ' DCBA'
369
+
370
+ In [6]:
371
+ ` ` `
372
+
373
+ # ### In a Python Script
374
+
375
+ I like to begin with the following [template] when starting a new exploit.
376
+
377
+ ` ` ` python
378
+ #! /usr/bin/python
379
+
380
+ from pwn import *
381
+
382
+ def main ():
383
+ pass
384
+
385
+ if __name__ == ' __main__' :
386
+ main ()
387
+ ` ` `
388
+
389
+ Running the script is as simple as calling python on it. Try running this
390
+ [script]:
391
+
392
+ ` ` ` python
393
+ #! /usr/bin/python
394
+
395
+ from pwn import *
396
+
397
+ def main ():
398
+ p = process(" /bin/sh" )
399
+ p.interactive ()
400
+
401
+ if __name__ == ' __main__' :
402
+ main ()
403
+ ` ` `
404
+
405
+ Running the script:
406
+
407
+ ` ` ` shell
408
+ ubuntu@ubuntu-xenial:/vagrant/lessons/3_intro_to_tools/scripts$ python 2_shellsample.py
409
+ [+] Starting local process ' /bin/sh' : Done
410
+ [* ] Switching to interactive mode
411
+ $ id
412
+ uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),109(netdev),110(lxd)
413
+ $
414
+ ` ` `
415
+
416
+ # ### Pwntools Command Line Tools
417
+
418
+ Pwntools installs the ` pwn` python script in /usr/local/bin. It provides
419
+ frontends to useful features of the library. To get a list of all available
420
+ frontends, you can execute ` pwn -h` .
421
+
422
+ ` ` ` shell
423
+ ubuntu@ubuntu-xenial:/vagrant/lessons/3_intro_to_tools/scripts$ pwn -h
424
+ usage: pwn [-h]
425
+ {asm,checksec,constgrep,cyclic,disasm,elfdiff,elfpatch,errno,hex,phd,pwnstrip,scramble,shellcraft,unhex,update}
426
+ ...
427
+
428
+ Pwntools Command-line Interface
429
+
430
+ positional arguments:
431
+ {asm,checksec,constgrep,cyclic,disasm,elfdiff,elfpatch,errno,hex,phd,pwnstrip,scramble,shellcraft,unhex,update}
432
+ asm Assemble shellcode into bytes
433
+ checksec Check binary security settings
434
+ constgrep Looking up constants from header files. Example:
435
+ constgrep -c freebsd -m ^PROT_ ' 3 + 4'
436
+ cyclic Cyclic pattern creator/finder
437
+ disasm Disassemble bytes into text format
438
+ elfdiff Compare two ELF files
439
+ elfpatch Patch an ELF file
440
+ errno Prints out error messages
441
+ hex Hex-encodes data provided on the command line or stdin
442
+ phd Pwnlib HexDump
443
+ pwnstrip Strip binaries for CTF usage
444
+ scramble Shellcode encoder
445
+ shellcraft Microwave shellcode -- Easy, fast and delicious
446
+ unhex Decodes hex-encoded data provided on the command line
447
+ or via stdin.
448
+ update Check for pwntools updates
449
+
450
+ optional arguments:
451
+ -h, --help show this help message and exit
452
+ ` ` `
453
+
454
+ You can investigate the available options at your own time. Take a look at the
455
+ [documentation] for a more detailed description of each of them.
456
+
457
+
458
+ [template]: ./scripts/1_template.py
459
+ [script]: ./scripts/2_shellsample.py
460
+ [documentation]: https://docs.pwntools.com/en/stable/commandline.html
0 commit comments