Skip to content

Commit db652ba

Browse files
committed
Updated the Intro to Tools section with more information on pwntools.
1 parent e1d2983 commit db652ba

File tree

5 files changed

+204
-1
lines changed

5 files changed

+204
-1
lines changed

Vagrantfile

+11-1
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ Vagrant.configure("2") do |config|
6666
# documentation for more information about their specific syntax and use.
6767
config.vm.provision "shell", inline: <<-SHELL
6868
dpkg --add-architecture i386
69+
cp /etc/apt/sources.list /etc/apt/sources.list.old
70+
sed -i -e 's/archive\.ubuntu\.com/mirror\.0x\.sg/g' /etc/apt/sources.list
6971
apt-get update
70-
apt-get install -y libc6:i386 libncurses5:i386 libstdc++6:i386 gdb python python-pip libssl-dev gcc git binutils socat
72+
apt-get install -y libc6:i386 libncurses5:i386 libstdc++6:i386 gdb python python-pip libssl-dev gcc git binutils socat apt-transport-https ca-certificates
7173
pip install --upgrade pip
7274
pip install pwntools
7375
pip install ipython
7476
pip install ropper
7577
git clone https://github.com/longld/peda.git /home/ubuntu/peda
7678
echo "source ~/peda/peda.py" >> /home/ubuntu/.gdbinit
79+
apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
80+
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | tee /etc/apt/sources.list.d/docker.list
81+
apt-get update
82+
apt-get install -y linux-image-extra-$(uname -r) linux-image-extra-virtual
83+
apt-get install -y docker-engine
84+
groupadd docker
85+
usermod -aG docker ubuntu
86+
service docker start
7787
SHELL
7888
end

deploydocker.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
docker stop $(docker ps -a -q) 2>/dev/null
4+
5+

lessons/3_intro_to_tools/lessonplan.md

+169
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ gdb-peda$
151151
152152
We will go through a few of the interesting commands.
153153
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+
154169
### distance
155170
156171
Often, calculating offsets from addresses is required when crafting your payload
@@ -289,3 +304,157 @@ gdb-peda$
289304
```
290305

291306
## 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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/python
2+
3+
from pwn import *
4+
5+
def main():
6+
pass
7+
8+
if __name__ == '__main__':
9+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/python
2+
3+
from pwn import *
4+
5+
def main():
6+
p = process("/bin/sh")
7+
p.interactive()
8+
9+
if __name__ == '__main__':
10+
main()

0 commit comments

Comments
 (0)