Pwntools
Pwntools
Release 2.2
Gallopsled et al
Contents
Getting Started
1.1 About pwntools . . .
1.2 Installation . . . . . .
1.3 Getting Started . . . .
1.4 from pwn import
1.5 Command Line Tools
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
3
4
6
9
12
Module Index
2.1 [Link] Assembler functions . . . . . . . . . . . . . . . . . .
2.2 [Link] Callbacks on unhandled exception . . . .
2.3 [Link] Replacement for atexit . . . . . . . . . . . . . . .
2.4 [Link] Easy access to header file constants . . . . .
2.5 [Link] Setting runtime variables . . . . . . . . . . . .
2.6 [Link] Resolving remote functions using leaks . . . . . .
2.7 [Link] Working with ELF binaries . . . . . . . . . . . . . .
2.8 [Link] Pwnlib exceptions . . . . . . . . . . . . . .
2.9 [Link] Working with GDB . . . . . . . . . . . . . . . . . . .
2.10 [Link] and Logging stuff . . . . . . . . . . . . . . . . . . . .
2.11 [Link] Helper class for leaking memory . . . . . . . .
2.12 [Link] Replacements for various functions . . .
2.13 [Link] Return Oriented Programming . . . . . . . . . . . . .
2.14 [Link] Shellcode generation . . . . . . . . . . . .
2.15 [Link] Terminal handling . . . . . . . . . . . . . . . . . .
2.16 [Link] Timeout handling . . . . . . . . . . . . . . . .
2.17 [Link] Talking to the World! . . . . . . . . . . . . . . . .
2.18 [Link] Functions for user interaction . . . . . . . . . . . . . .
2.19 [Link] A database of useragent strings . . . . . . .
2.20 [Link] Calculating CRC-sums . . . . . . . . . . . . .
2.21 [Link] Generation of unique sequences . . . . . .
2.22 [Link] Utilities bit fiddling . . . . . . . . . . .
2.23 [Link] Hashing functions . . . . . . . . . . . . .
2.24 [Link] Extension of standard module itertools
2.25 [Link] Operations on lists . . . . . . . . . . . . .
2.26 [Link] We could not fit it any other place . . . . . .
2.27 [Link] Networking interfaces . . . . . . . . . . . . .
2.28 [Link] Packing and unpacking of strings . . . .
2.29 [Link] Working with /proc/ . . . . . . . . . . . .
2.30 [Link] Safe evaluation of python code . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
17
19
20
20
21
29
31
35
35
37
38
43
44
45
56
56
58
76
76
77
107
108
112
114
124
126
129
130
136
138
. .
. .
. .
*
. .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
141
143
ii
pwntools is a CTF framework and exploit development library. Written in Python, it is designed for rapid prototyping and development, and intended to make exploit writing as simple as possible.
Contents
Contents
CHAPTER 1
Getting Started
Though there are a few exceptions (such as [Link]), that does not quite fit the goals of being simple
and clean, but they can still be imported without implicit side-effects.
1.2 Installation
pwntools is best supported on Ubuntu 12.04 and 14.04, but most functionality should work on any Posix-like distribution (Debian, Arch, FreeBSD, OSX, etc.).
1.2.1 Prerequisites
In order to get the most out of pwntools, you should have the following system libraries installed.
Binutils
Assembly of foreign architectures (e.g. assembling Sparc shellcode on Mac OS X) requires cross-compiled versions
of binutils to be installed. Weve made this process as smooth as we can.
In these examples, replace $ARCH with your target architecture (e.g., arm, mips64, vax, etc.).
Ubuntu
Mac OS X
Mac OS X is just as easy, but requires building binutils from source. However, weve made homebrew recipes to
make this a single command. After installing brew, grab the appropriate recipe from our binutils repo.
Alternate OSes
If you want to build everything by hand, or dont use any of the above OSes, binutils is simple to build by hand.
#!/usr/bin/env bash
V=2.25
# Binutils Version
ARCH=arm # Target architecture
cd /tmp
wget -nc [Link]
wget -nc [Link]
gpg --keyserver [Link] --recv-keys 4AE55E93
gpg --verify binutils-$[Link]
rm -rf binutils-*
tar xf binutils-$[Link]
mkdir binutils-build
cd binutils-build
export AR=ar
export AS=as
../binutils-$V/configure \
--prefix=/usr/local \
--target=$ARCH-unknown-linux-gnu \
--disable-static \
--disable-multilib \
--disable-werror \
--disable-nls
MAKE=gmake
hash gmake || MAKE=make
$MAKE -j
sudo $MAKE install
Capstone
Capstone is a disassembly library required for gathering ROP gadgets and ROP chain generation.
Its a separate requirement from binutils because its used by Jon Salwans ROPgadget tool which we use under
the covers.
In particular, version 2.1.2 should be used. Capstone can be downloaded here, or installed with the steps below.
Ubuntu
$ wget -nc [Link]
$ apt-get install capstone-2.1.2_amd64.deb
Mac OS X
$ brew install capstone
1.2. Installation
Mac OS X
No action needed.
This imports a lot of functionality into the global namespace. You can now assemble, disassemble, pack, unpack, and
many other things with a single function.
A full list of everything that is imported is available on from pwn import *.
>>> l = listen()
>>> r = remote(localhost, [Link])
>>> c = l.wait_for_connection()
>>> [Link](hello)
>>> [Link]()
hello
>>> [Link](timeout=5)
hello world\n
>>> [Link]()
Not only can you interact with processes programmatically, but you can actually interact with processes.
>>> [Link]()
$ whoami
user
Theres even an SSH module for when youve got to SSH into a box to perform a local/setuid exploit with
[Link]. You can quickly spawn processes and grab the output, or spawn a process and interact
iwth it like a process tube.
>>> shell = ssh(bandit0, [Link], password=bandit0)
>>> shell[whoami]
bandit0
>>> shell.download_file(/etc/motd)
>>> sh = [Link](sh)
>>> [Link](sleep 3; echo hello world;)
>>> [Link](timeout=1)
>>> [Link](timeout=5)
hello world\n
>>> [Link]()
However, it can also be set once in the global context. The operating system, word size, and endianness can also be
set here.
>>>
>>>
>>>
>>>
[Link]
[Link]
[Link]
context.word_size
=
=
=
=
i386
linux
little
32
Additionally, you can use a shorthand to set all of the values at once.
>>> asm(nop)
\x90
>>> context(arch=arm, os=linux, endian=big, word_size=32)
>>> asm(nop)
\xe3 \xf0\x00
Will cause all of the data sent and received by a tube to be printed to the screen.
However, you shouldnt even need to write your own shellcode most of the time! Pwntools comes with the
[Link] module, which is loaded with useful time-saving shellcodes.
Lets say that we want to setreuid(getuid(), getuid()) followed by duping file descriptor 4 to stdin, stdout, and stderr,
and then pop a shell!
0:
c3
ret
Which imports a bazillion things into the global namespace to make your life easier.
This is a quick list of most of the objects and routines imported, in rough order of importance and frequency of use.
context
[Link]
Responsible for most of the pwntools convenience settings
Set context.log_level = debug when troubleshooting your exploit
Scope-aware,
so you can disable logging
[Link]
for
subsection
of
code
via
10
DynELF
[Link]
Dynamically resolve functions given only a pointer to any loaded module, and a function which can
leak data at any address
ROP
[Link]
Automatically generate ROP chains using a DSL to describe what you want to do, rather than raw
addresses
[Link] and [Link]
[Link]
Launch a binary under GDB and pop up a new terminal to interact with it. Automates setting breakpoints and makes iteration on exploits MUCH faster.
Alternately, attach to a running process given a PID, [Link] object, or even just a socket
thats connected to it
args
Dictionary contining all-caps command-line arguments for quick access
Run via python [Link] REMOTE=1 and args[REMOTE] == 1.
Can also control logging verbosity and terminal fancyness
* NOTERM
* SILENT
* DEBUG
randoms, rol, ror, xor, bits
[Link]
Useful utilities for generating random data from a given alphabet, or simplifying math operations that
usually require masking off with 0xffffffff or calling ord and chr an ugly number of times
net
[Link]
Routines for querying about network interfaces
proc
[Link]
Routines for querying about processes
pause
Its the new getch
safeeval
[Link]
Functions for safely evalutaing python code without nasty side-effects.
These are all pretty self explanatory, but are useful to have in the global namespace.
hexdump
11
1.5.1 asm
Assemble shellcode into bytes
line
Lines to assemble. If none are supplied, use stdin
-h, --help
show this help message and exit
-f <format>, --format <format>
Output format (defaults to hex for ttys, otherwise raw)
-o <file>, --output <file>
Output file (defaults to stdout)
-c <<opt>>, --context <<opt>>
The os/architecture the shellcode will run in (default: linux/i386), choose from: aarch64, alpha, amd64, arm,
avr, cris, freebsd, i386, ia64, linux, m68k, mips, mips64, msp430, powerpc, powerpc64, s390, sparc, sparc64,
thumb, vax, windows
12
1.5.2 constgrep
Looking up constants from header files.
Example: constgrep -c freebsd -m ^PROT_ 3 + 4
regex
The regex matching constant you want to find
constant
The constant to find
-h, --help
show this help message and exit
-e <<constant name>>, --exact <<constant name>>
Do an exact match for a constant instead of searching for a regex
-i, --case-insensitive
Search case insensitive
-m, --mask-mode
Instead of searching for a specific constant value, search for values not containing strictly less bits that the given
value.
-c <<opt>>, --context <<opt>>
The os/architecture to find constants for (default: linux/i386), choose from: aarch64, alpha, amd64, arm, avr,
cris, freebsd, i386, ia64, linux, m68k, mips, mips64, msp430, powerpc, powerpc64, s390, sparc, sparc64, thumb,
vax, windows
1.5.3 cyclic
Cyclic pattern creator/finder
count
Number of characters to print
-h, --help
show this help message and exit
-a <alphabet>, --alphabet <alphabet>
The alphabet to use in the cyclic pattern (defaults to all lower case letters)
-n <length>, --length <length>
Size of the unique subsequences (defaults to 4).
-l <<lookup value>>, -o <<lookup value>>, --offset <<lookup value>>, --lookup <<lookup valu
Do a lookup instead printing the alphabet
1.5.4 disasm
Disassemble bytes into text format
hex
Hex-string to disasemble. If none are supplied, then it uses stdin in non-hex mode.
-h, --help
show this help message and exit
13
1.5.5 elfdiff
a
b
-h, --help
show this help message and exit
1.5.6 elfpatch
elf
File to patch
offset
Offset to patch in virtual address (hex encoded)
bytes
Bytes to patch (hex encoded)
-h, --help
show this help message and exit
1.5.7 hex
Hex-encodes data provided on the command line or via stdin.
data
Data to convert into hex
-h, --help
show this help message and exit
1.5.8 phd
Pwnlib HexDump
file
File to hexdump. Reads from stdin if missing.
-h, --help
show this help message and exit
-w <width>, --width <width>
Number of bytes per line.
-l <highlight>, --highlight <highlight>
Byte to highlight.
-s <skip>, --skip <skip>
Skip this many initial bytes.
14
1.5.9 shellcraft
Microwave shellcode Easy, fast and delicious
<shellcode>
The shellcode you want
<arg>
Argument to the chosen shellcode
-h, --help
show this help message and exit
-?, --show
Show shellcode documentation
-o <<file>>, --out <<file>>
Output file (default: stdout)
-f <<format>>, --format <<format>>
Output format (default: hex), choose from {r}aw, {s}tring, {c}-style array, {h}ex string, hex{i}i, {a}ssembly
code, {p}reprocssed code
1.5.10 unhex
Decodes hex-encoded data provided on the command line or via stdin.
hex
Hex bytes to decode
-h, --help
show this help message and exit
15
16
CHAPTER 2
Module Index
2.1.2 Assembly
To assemble code, simply invoke asm() on the code to assemble.
>>> asm(mov eax, 0)
\xb8\x00\x00\x00\x00
Finally, asm() is used to assemble shellcode provided by pwntools in the shellcraft module.
>>> asm([Link]())
jhh///sh/bin\x89\xe31\xc9j\x0bX\x99\xcd\x80
2.1.3 Disassembly
To disassemble code, simply invoke disasm() on the bytes to disassemble.
>>> disasm(\xb8\x0b\x00\x00\x00)
0:
b8 0b 00 00 00
mov
eax,0xb
17
= i386, os = freebsd)
= amd64, os = linux)
= amd64, os = linux)
= arm, os = linux, bits=32)
Examples
>>> print disasm([Link](hex), arch = i386)
0:
b8 5d 00 00 00
mov
eax,0x5d
>>> print disasm([Link](hex), arch = amd64)
0:
b8 17 00 00 00
mov
eax,0x17
>>> print disasm([Link](hex), arch = amd64)
0:
48 c7 c0 17 00 00 00
mov
rax,0x17
>>> print disasm([Link](hex), arch = arm)
0:
e51f0004
ldr
r0, [pc, #-4]
; 0x4
4:
00900052
addseq r0, r0, r2, asr r0
>>> print disasm([Link](hex), arch = thumb, bits=32)
0:
f04f 0005
mov.w
r0, #5
[Link].which_binutils(util, **kwargs)
Finds a binutils in the PATH somewhere. Expects that the utility is prefixed with the architecture name.
Examples
>>> import platform
>>> which_binutils(as, arch=[Link]())
.../bin/as
>>> which_binutils(as, arch=arm)
.../bin/arm-...-as
>>> which_binutils(as, arch=powerpc)
.../bin/powerpc...-as
>>> which_binutils(as, arch=msp430)
...
Traceback (most recent call last):
...
Exception: Could not find as installed for ContextType(arch = msp430)
Traceback (most recent call last):
...
Exception: Could not find as installed for ContextType(arch = msp430)
19
@[Link]
def handler():
...
Notice however that this will bind handler to the identifier and not the actual exception-handler. The
exception-handler can then be unregistered with:
[Link](handler)
Notice however that this will bind handler to the identifier and not the actual exit-handler. The exit-handler
can then be unregistered with:
[Link](handler)
Example
>>> print [Link].SYS_stat
188
>>> print [Link].i386.SYS_stat
106
>>> print [Link].amd64.SYS_stat
4
The submodule freebsd contains all constants for FreeBSD, while the constants for Linux have been split up by
architecture.
The variables of the submodules will be lifted up by setting the [Link] or
[Link] in a manner similar to what happens in [Link].
Example
>>> with [Link](os = freebsd):
...
print constants.SYS_stat
188
>>> with [Link](os = linux, arch = i386):
...
print constants.SYS_stat
106
>>> with [Link](os = linux, arch = amd64):
...
print constants.SYS_stat
4
21
Examples
>>> [Link]()
>>> [Link](os=linux)
>>> [Link] == linux
True
>>> [Link] = arm
>>> vars(context) == {arch: arm, bits: 32, endian: little, os: linux}
True
>>> [Link]
little
>>> [Link]
32
>>> def nop():
...
print [Link](nop).encode(hex)
>>> nop()
00f020e3
>>> with [Link](arch = i386):
...
nop()
90
>>> from [Link] import Thread as PwnThread
>>> from threading
import Thread as NormalThread
>>> with [Link](arch = mips):
...
pwnthread = PwnThread(target=nop)
...
thread
= NormalThread(target=nop)
>>> # Normal thread uses the default value for arch, i386
>>> _=([Link](), [Link]())
90
>>> # Pwnthread uses the correct context from creation-time
>>> _=([Link](), [Link]())
00000000
>>> nop()
00f020e3
22
[Link]()
[Link](arch=arm)
def p():
print [Link]
[Link] = mips
print [Link]
# Note that a normal Thread starts with a clean context
# (i386 is the default architecture)
>>> t = [Link](target=p)
>>> _=([Link](), [Link]())
i386
mips
>>> # Note that the main Threads context is unchanged
>>> print [Link]
arm
>>> # Note that a context-aware Thread receives a copy of the context
>>> t = [Link](target=p)
>>> _=([Link](), [Link]())
arm
mips
>>> # Again, the main thread is unchanged
>>> print [Link]
arm
Implementation Details:
This
class
implemented
by
hooking
the
private
function
[Link]._Thread_bootstrap(), which is called before passing control to [Link]().
This could be done by overriding run itself, but we would have to ensure that all uses of the
class would only ever use the keyword target= for __init__, or that all subclasses invoke
super([Link]).set_up_context() or similar.
ContextType.__call__(**kwargs)
Alias for [Link]()
[Link]
Target machine architecture.
Allowed values are listed in [Link].
Side Effects:
If an architecture is specified which also implies additional attributes (e.g. amd64 implies 64-bit
words, powerpc implies big-endian), these attributes will be set on the context if a user has not
already set a value.
The following properties may be modified.
bits
endian
Raises AttributeError An invalid architecture was specified
Examples
>>> [Link]()
>>> [Link] == i386 # Default architecture
True
>>> [Link] = mips
>>> [Link] == mips
True
23
Note that expressly setting bits means that we use that value instead of the default
>>> [Link]()
>>> [Link] = 32
>>> [Link] = amd64
>>> [Link] == 32
True
Setting the architecture can override the defaults for both endian and bits
>>> [Link]()
>>> [Link] = powerpc64
>>> vars(context) == {arch: powerpc64, bits: 64, endian: big}
True
[Link] = OrderedDict([(powerpc64, {bits: 64, endian: big}), (aarch64, {bits: 64, end
Keys are valid values for [Link](). Values are defaults which are
set when [Link] is set
[Link]
Target machine word size, in bits (i.e. the size of general purpose registers).
The default value is 32, but changes according to arch.
Examples
>>> [Link]()
>>> [Link] == 32
True
>>> [Link] = 64
>>> [Link] == 64
True
>>> [Link] = -1
Traceback (most recent call last):
...
AttributeError: bits must be >= 0 (-1)
Traceback (most recent call last):
...
AttributeError: bits must be >= 0 (-1)
24
[Link]
Target machine word size, in bytes (i.e. the size of general purpose registers).
This is a convenience wrapper around bits / 8.
Examples
>>> [Link] = 1
>>> [Link] == 8
True
>>> [Link] = 0
Traceback (most recent call last):
...
AttributeError: bits must be >= 0 (0)
Traceback (most recent call last):
...
AttributeError: bits must be >= 0 (0)
[Link]()
Clears the contents of the context. All values are set to their defaults.
Examples
>>> # Default value
>>> [Link] == i386
True
>>> [Link] = arm
>>> [Link] == i386
False
>>> [Link]()
>>> [Link] == i386
True
[Link]() dict
Returns a copy of the current context as a dictionary.
Examples
>>> [Link]()
>>> [Link]
= linux
>>> vars(context) == {os: linux}
True
[Link] = {newline: \n, arch: i386, log_level: 20, bits: 32, signed: False, timeout: 1048576
Default values for [Link]
[Link]
Endianness of the target machine.
The default value is little, but changes according to arch.
Raises AttributeError An invalid endianness was provided
25
Examples
>>> [Link]()
>>> [Link] == little
True
>>> [Link] = big
>>> [Link]
big
>>> [Link] = be
>>> [Link] == big
True
>>> [Link] = foobar
Traceback (most recent call last):
...
AttributeError: endian must be one of [be, big, eb, el, le, little]
Traceback (most recent call last):
...
AttributeError: endian must be one of [be, big, eb, el, le, little]
[Link]
Legacy alias for endian.
Examples
>>> [Link] == [Link]
True
[Link] = OrderedDict([(little, little), (big, big), (el, little), (le, little), (be, big), (e
Valid values for endian
[Link](**kwargs) context manager
Create a context manager for use with the with statement.
For more information, see the example below or PEP 343.
Parameters kwargs Variables to be assigned in the new environment.
Returns ContextType manager for managing the old and new environment.
Examples
>>> [Link]()
>>> [Link] = 1
>>> [Link] == 1
True
>>> print [Link]
1.0
>>> with [Link](timeout = 2):
...
print [Link]
...
[Link] = 3
...
print [Link]
2.0
3.0
26
ContextType.log_level
Sets the verbosity of pwntools logging mechanism.
Valid values are specified by the standard Python logging module.
Default value is set to INFO.
Examples
[Link]
Operating system of the target machine.
The default value is linux.
Allowed values are listed in [Link].
Examples
>>> [Link] = linux
>>> [Link] = foobar
Traceback (most recent call last):
...
AttributeError: os must be one of [freebsd, linux, windows]
Traceback (most recent call last):
...
AttributeError: os must be one of [freebsd, linux, windows]
27
Examples
>>> [Link]
False
>>> [Link] = 1
>>> [Link]
True
>>> [Link] = signed
>>> [Link]
True
>>> [Link] = unsigned
>>> [Link]
False
>>> [Link] = foobar
Traceback (most recent call last):
...
AttributeError: signed must be one of [no, signed, unsigned, yes] or a non-string tr
Traceback (most recent call last):
...
AttributeError: signed must be one of [no, signed, unsigned, yes] or a non-string tr
[Link]
Alias for signed
[Link] = {yes: True, unsigned: False, signed: True, no: False}
Valid string values for signed
[Link]
Default amount of time to wait for a blocking operation before it times out, specified in seconds.
The default value is to have an infinite timeout.
See [Link] for additional information on valid values.
[Link](*args, **kwargs)
Convenience function, which is shorthand for setting multiple variables at once.
It is a simple shorthand such that:
[Link](os = linux, arch = arm, ...)
is equivalent to:
[Link]
= linux
[Link] = arm
...
28
ContextType.word_size
Alias for bits
class [Link](*args, **kwargs)
Instantiates a context-aware thread, which inherit its context when it is instantiated. The class can be accessed
both on the context module as [Link] and on the context singleton object inside the context
module as [Link].
Threads created by using the native :[Link] will have a clean (default) context.
Regardless of the mechanism used to create any thread, the context is de-coupled from the parent thread, so
changes do not cascade to child or parent.
Saves a copy of the context when instantiated (at __init__) and updates the new threads context before
passing control to the user code via run or target=.
Examples
>>> [Link]()
>>> [Link](arch=arm)
>>> def p():
...
print [Link]
...
[Link] = mips
...
print [Link]
>>> # Note that a normal Thread starts with a clean context
>>> # (i386 is the default architecture)
>>> t = [Link](target=p)
>>> _=([Link](), [Link]())
i386
mips
>>> # Note that the main Threads context is unchanged
>>> print [Link]
arm
>>> # Note that a context-aware Thread receives a copy of the context
>>> t = [Link](target=p)
>>> _=([Link](), [Link]())
arm
mips
>>> # Again, the main thread is unchanged
>>> print [Link]
arm
Implementation Details:
This class implemented by hooking the private function [Link]._Thread_bootstrap(),
which is called before passing control to [Link]().
This could be done by overriding run itself, but we would have to ensure that all uses of the
class would only ever use the keyword target= for __init__, or that all subclasses invoke
super([Link]).set_up_context() or similar.
29
2.6.1 Example
# Assume a process or remote connection
p = process(./pwnme)
# Declare a function that takes a single address, and
# leaks at least one byte at that address.
def leak(address):
data = [Link](address, 4)
[Link]("%#x => %s" % (address, (data or ).encode(hex)))
return data
# For the sake of this example, lets say that we
# have any of these pointers. One is a pointer into
# the target binary, the other two are pointers into libc
main
= 0xfeedf4ce
libc
= 0xdeadb000
system = 0xdeadbeef
# With our leaker, and a pointer into our target binary,
# we can resolve the address of anything.
#
# We do not actually need to have a copy of the target
# binary for this to work.
d = DynELF(leak, main)
assert [Link](None,
libc) == libc
assert [Link](system, libc) == system
# However, if we *do* have a copy of the target binary,
# we can speed up some of the steps.
d = DynELF(leak, main, elf=ELF(./pwnme))
assert [Link](None,
libc) == libc
assert [Link](system, libc) == system
# Alternately, we can resolve symbols inside another library,
# given a pointer into it.
d = DynELF(leak, libc + 0x1234)
assert [Link](system)
== system
DynELF
class [Link](leak, pointer=None, elf=None)
DynELF knows how to resolve symbols in remote processes via an infoleak or memleak vulnerability encapsulated by [Link].
Implementation Details:
Resolving Functions:
In all ELFs which export symbols for importing by other libraries, (e.g. [Link]) there
are a series of tables which give exported symbol names, exported symbol addresses, and
the hash of those exported symbols. By applying a hash function to the name of the desired
symbol (e.g., printf), it can be located in the hash table. Its location in the hash table
provides an index into the string name table (strtab), and the symbol address (symtab).
Assuming we have the base address of [Link], the way to resolve the address of
printf is to locate the symtab, strtab, and hash table. The string "printf" is
hashed according to the style of the hash table (SYSV or GNU), and the hash table is
walked until a matching entry is located. We can verify an exact match by checking the
30
string table, and then get the offset into [Link] from the symtab.
Resolving Library Addresses:
If we have a pointer into a dynamically-linked executable, we can leverage an internal linker
structure called the link map. This is a linked list structure which contains information about
each loaded library, including its full path and base address.
A pointer to the link map can be found in two ways. Both are referenced from entries in
the DYNAMIC array.
In non-RELRO binaries, a pointer is placed in the .[Link] area in the binary. This is
marked by finding the DT_PLTGOT_ area in the binary.
In all binaries, a pointer can be found in the area described by the DT_DEBUG area.
This exists even in stripped binaries.
For maximum flexibility, both mechanisms are used exhaustively.
bases()
Resolve base addresses of all loaded libraries.
Return a dictionary mapping library path to its base address.
dynamic
Returns Pointer to the .DYNAMIC area.
elfclass
32 or 64
static find_base(leak, ptr)
Given a [Link] object and a pointer into a library, find its base address.
link_map
Pointer to the runtime link_map object
lookup(symb = None, lib = None) int
Find the address of symbol, which is found in lib.
Parameters
symb (str) Named routine to look up
lib (str) Substring to match for the library name. If omitted, the current library is
searched. If set to libc, [Link] is assumed.
Returns Address of the named symbol, or None.
[Link].gnu_hash(str) int
Function used to generated GNU-style hashes for strings.
[Link].sysv_hash(str) int
Function used to generate SYSV-style hashes for strings.
31
class [Link](path)
Encapsulates information about an ELF file.
Variables
path Path to the binary on disk
symbols Dictionary of {name: address} for all symbols in the ELF
plt Dictionary of {name: address} for all functions in the PLT
got Dictionary of {name: address} for all function pointers in the GOT
libs Dictionary of {path: address} for each shared object required to load the ELF
Example:
bash = ELF(which(bash))
hex([Link][read])
# 0x41dac0
hex([Link][read])
# 0x41dac0
u32([Link]([Link][read], 4))
# 0x41dac6
print disasm([Link]([Link][read],16), arch=amd64)
# 0:
ff 25 1a 18 2d 00
jmp
QWORD PTR [rip+0x2d181a]
# 6:
68 59 00 00 00
push
0x59
# b:
e9 50 fa ff ff
jmp
0xfffffffffffffa60
# 0x2d1820
address
Address of the lowest segment loaded in the ELF. When updated, cascades updates to segment vaddrs,
section addrs, symbols, plt, and got.
>>> bash = ELF(which(bash))
>>> old = [Link][read]
>>> [Link] += 0x1000
>>> [Link][read] == old + 0x1000
True
asm(address, assembly)
Assembles the specified instructions and inserts them into the ELF at the specified address.
The resulting binary can be saved with [Link]()
bss(offset=0)
Returns an index into the .bss segment
disasm(address, n_bytes)
Returns a string of disassembled instructions at the specified virtual memory address
dwarf
DWARF info for the elf
elfclass
ELF class (32 or 64).
Note: Set during ELFFile._identify_file
elftype
ELF type (EXEC, DYN, etc)
entry
Entry point to the ELF
32
entrypoint
Entry point to the ELF
executable_segments
Returns: list of all segments which are executable.
get_data()
Retrieve the raw data from the ELF file.
>>> bash = ELF(which(bash))
>>> fd
= open(which(bash))
>>> bash.get_data() == [Link]()
True
non_writable_segments
Returns: list of all segments which are NOT writeable
offset_to_vaddr(offset)
Translates the specified offset to a virtual address.
Parameters offset (int) Offset to translate
Returns Virtual address which corresponds to the file offset, or None
Examples
>>> bash = ELF(which(bash))
>>> [Link] == bash.offset_to_vaddr(0)
True
>>> [Link] += 0x123456
>>> [Link] == bash.offset_to_vaddr(0)
True
read(address, count)
Read data from the specified virtual address
Parameters
address (int) Virtual address to read
count (int) Number of bytes to read
Returns A string of bytes, or None
Examples
>>> bash = ELF(which(bash))
>>> [Link]([Link]+1, 3)
ELF
save(path)
Save the ELF to a file
>>> bash = ELF(which(bash))
>>> [Link](/tmp/bash_copy)
>>> copy = file(/tmp/bash_copy)
>>> bash = file(which(bash))
>>> [Link]() == [Link]()
True
33
section(name)
Gets data for the named section
Parameters name (str) Name of the section
Returns String containing the bytes for that section
sections
A list of all sections in the ELF
segments
A list of all segments in the ELF
start
Entry point to the ELF
vaddr_to_offset(address)
Translates the specified virtual address to a file address
Parameters address (int) Virtual address to translate
Returns Offset within the ELF file which corresponds to the address, or None.
Examples
>>> bash = ELF(which(bash))
>>> 0 == bash.vaddr_to_offset([Link])
True
>>> [Link] += 0x123456
>>> 0 == bash.vaddr_to_offset([Link])
True
writable_segments
Returns: list of all segments which are writeable
34
write(address, data)
Writes data to the specified virtual address
Parameters
address (int) Virtual address to write
data (str) Bytes to write
Note: This routine does not check the bounds on the write to ensure that it stays in the same segment.
Examples
>>> bash = ELF(which(bash))
>>> [Link]([Link]+1, 3)
ELF
>>> [Link]([Link], "HELO")
>>> [Link]([Link], 4)
HELO
35
Note that the raw addresses provided by info sharedlibrary are actually the address of the .text
segment, not the image base address.
This routine automates the entire process of:
[Link] the binaries from the remote server
[Link] GDB for the information
[Link] each library into an ELF
[Link] up the base address vs. the .text segment address
Parameters
binary (str) Path to the binary on the remote server
ssh ([Link]) SSH connection through which to load the libraries. If left as
None, will use a [Link].
ulimit (bool) Set to True to run ulimit -s unlimited before GDB.
Returns A list of [Link] objects, with correct base addresses.
Example:
>>> with [Link](log_level=9999):
...
shell = ssh(host=[Link],user=bandit0,password=bandit0)
...
bash_libs = gdb.find_module_addresses(/bin/bash, shell)
>>> [Link](bash_libs[0].path)
[Link].6
>>> hex(bash_libs[0].symbols[system])
0x7ffff7634660
36
prints:
[*] Hello, world!
Additionally, there are some nifty mechanisms for performing status updates on a running job (e.g. when bruteforcing).:
p = [Link](Working)
[Link](Reticulating splines)
[Link](1)
[Link](Got a shell!)
The verbosity of logging can be most easily controlled by setting context.log_level on the global context
object.:
[Link]("No you see me")
context.log_level = error
[Link]("Now you dont")
This provides an easy way to filter logging programmatically or via a configuration file for debugging.
Theres no need to expressly import this log module.
When using progress, you should use the with keyword to manage scoping, to ensure the spinner stops if an
exception is thrown.
class [Link](*args, **kwargs)
Specialization of [Link] which uses [Link].log_level to infer verbosity.
Also adds some pwnlib flavor via:
progress()
success()
failure()
indented()
37
Adds pwnlib-specific information for coloring and indentation to the log records passed to the
[Link].
Internal:
Permits prepending a string to each message, by means of msg_prefix. This is leveraged for
progress messages.
debug(message)
Logs a debug message.
error(message)
To be called outside an exception handler.
Logs an error message, then raises a PwnlibException.
failure(message)
Logs a failure message. If the Logger is animated, the animation is stopped.
indented(message, level=[Link])
Log an info message without the line prefix.
Parameters level (int) Alternate log level at which to set the indented message.
info(message)
Logs an info message.
info_once(message)
Logs an info message. The same message is never printed again.
progress(self ) Logger
Creates a Logger with a progress animation, which can be stopped via success(), and failure().
The Logger returned is also a scope manager. Using scope managers ensures that the animation is
stopped, even if an exception is thrown.
::
with [Link](Trying something...) as p:
for i in range(10): [Link](At %i % i) [Link](0.5)
x = 1/0
success(message)
Logs a success message. If the Logger is animated, the animation is stopped.
warn(message)
Logs a warning message.
warn_once(message)
Logs a warning message. The same message is never printed again.
38
It will cache leaked memory (which requires either non-randomized static data or a continouous session). If
required, dynamic or known data can be set with the set-functions, but this is usually not required. If a byte
cannot be recovered, it will try to leak nearby bytes in the hope that the byte is recovered as a side-effect.
Parameters
f (function) The leaker function.
search_range (int) How many bytes to search backwards in case an address does not
work.
reraise (bool) Whether to reraise call [Link]() in case the leaker
function throws an exception.
Example
>>> import pwnlib
>>> binsh = [Link](/bin/sh)
>>> @[Link]
... def leaker(addr):
...
print "leaking 0x%x" % addr
...
return binsh[addr:addr+4]
>>> leaker.s(0)[:4]
leaking 0x0
leaking 0x4
\x7fELF
>>> hex(leaker.d(0))
0x464c457f
>>> hex([Link](1))
0x45
>>> hex(leaker.d(0))
leaking 0x1
0x464c457f
39
Examples
>>> l = MemLeak(lambda a: None)
>>> [Link] = {0:a}
>>> l.n(0,1) == a
True
>>> [Link](0) == unpack(a, 8)
True
>>> [Link]
{}
>>> [Link](0) is None
True
40
>>> [Link]
{}
field(address, obj)
call(address, field) => int or str
Leak an entire structure, or structure field.
Parameters
address (int) Base address to calculate offsets from
field (obj) Instance of a ctypes field
Return Value: The type of the return value will be dictated by the type of field.
n(addr, ndx = 0) str
Leak numb bytes at addr.
Returns A string with the leaked bytes, will return None if any are missing
Examples
>>> import string
>>> data = string.ascii_lowercase
>>> l = MemLeak(lambda a: data[a:a+4], reraise=False)
>>> l.n(0,1) == a
True
>>> l.n(0,26) == data
True
>>> len(l.n(0,26)) == 26
True
>>> l.n(0,27) is None
True
41
Examples
>>> import string
>>> data = string.ascii_lowercase
>>> l = MemLeak(lambda a: data[a:a+16], reraise=False)
>>> l.q(0) == unpack(abcdefgh, 64)
True
>>> l.q(18) == unpack(stuvwxyz, 64)
True
>>> l.q(19) is None
True
See setw().
setq(addr, val, ndx=0)
Sets qword at ((uint64_t*)addr)[ndx] to val in the cache.
42
Examples
See setw().
sets(addr, val, null_terminate=True)
Set known string at addr, which will be optionally be null-terminated
Note that this method is a bit dumb about how it handles the data. It will null-terminate the data, but it will
not stop at the first null.
Examples
>>> l = MemLeak(lambda x: )
>>> [Link] == {}
True
>>> [Link](0, H\x00ello)
>>> [Link] == {0: H, 1: \x00, 2: e, 3: l, 4: l, 5: o, 6: \x00}
True
43
__getattr__(attr)
Helper to make finding ROP gadets easier.
Also provides a shorthand for .call(): [Link](args) ==> [Link](function,
args)
>>> elf=ELF(which(bash))
>>> rop=ROP([elf])
>>> [Link]
== [Link](regs=[rdi], order = regs)
True
>>> rop.r13_r14_r15_rbp == [Link](regs=[r13,r14,r15,rbp], order = regs)
True
>>> [Link]
== [Link](move=[Link])
True
>>> rop.ret_8
== [Link](move=8)
True
>>> [Link]
!= None
True
__str__()
Returns: Raw bytes of the ROP chain
build(base=None)
Build the ROP chain into a list (addr, int/string, bool), where the last value is True iff the value was an
internal reference.
It is guaranteed that the individual parts are next to each other.
If there is no base available, then the returned addresses are indexed from 0.
Parameters base (int) The base address to build the rop-chain from. Defaults to [Link].
call(resolvable, arguments=())
Add a call to the ROP chain
Parameters
resolvable (str,int) Value which can be looked up via resolve, or is already an integer.
44
arguments (list) List of arguments which can be passed to pack(). Alternately, if a base
address is set, arbitrarily nested structures of strings or integers can be provided.
chain()
Build the ROP chain
Returns str containging raw ROP bytes
dump()
Dump the ROP chain in an easy-to-read manner
migrate(next_base)
Explicitly set $sp, by using a leave; ret gadget
raw(value)
Adds a raw integer or string to the ROP chain.
If your architecture requires aligned values, then make sure that any given string is aligned!
Parameters data (int/str) The raw value to put onto the rop chain.
resolve(resolvable)
Resolves a symbol to an address
Parameters resolvable (str,int) Thing to convert into an address
Returns int containing address of resolvable, or None
search(move=0, regs=None, order=size)
Search for a gadget which matches the specified criteria.
Parameters
move (int) Minimum number of bytes by which the stack pointer is adjusted.
regs (list) Minimum list of registers which are popped off the stack.
order (str) Either the string size or regs. Decides how to order multiple gadgets the
fulfill the requirements.
The search will try to minimize the number of bytes popped more than requested, the number of registers
touched besides the requested and the address.
If order == size, then gadgets are compared lexicographically by (total_moves,
total_regs, addr), otherwise by (total_regs, total_moves, addr).
Returns A tuple of (address, info) in the same format as [Link]().
unresolve(value)
Inverts resolve. Given an address, it attempts to find a symbol for it in the loaded ELF files. If none is
found, it searches all known gadgets, and returns the disassembly
Parameters value (int) Address to look up
Returns String containing the symbol name for the address, disassembly for a gadget (if theres
one at that address), or an empty string.
45
Example
>>> print [Link]().strip(\n)
nop
>>> print [Link]()
/* push /bin///sh\x00 */
push 0x68
push 0x732f2f2f
push 0x6e69622f
...
2.14.1 Submodules
[Link].amd64 Shellcode for AMD64
[Link].amd64
46
Parameters
dest (str) The destination register.
src (str) Either the input register, or an immediate value.
stack_allowed (bool) Can the stack be used?
[Link]()
A single-byte nop instruction.
[Link](value)
Pushes a value onto the stack without using null bytes or newline characters.
Parameters value (int,str) The value or register to push
[Link](string, append_null=True)
Pushes a string onto the stack without using null bytes or newline characters.
Example
>>> print [Link]().rstrip()
/* push \x00 */
push 1
dec byte ptr [rsp]
>>> print [Link](a).rstrip()
/* push a\x00 */
push 0x61
>>> print [Link](aa).rstrip()
/* push aa\x00 */
push 0x...
xor dword ptr [rsp], 0x...
>>> print [Link](aaa).rstrip()
/* push aaa\x00 */
push 0x...
xor dword ptr [rsp], 0x...
>>> print [Link](aaaa).rstrip()
/* push aaaa\x00 */
push 0x61616161
>>> print [Link](aaa\xc3).rstrip()
/* push aaa\xc3\x00 */
push 0x...
xor dword ptr [rsp], 0x...
>>> print [Link](aaa\xc3, append_null = False).rstrip()
/* push aaa\xc3 */
push 0x...
>>> print [Link](\xc3).rstrip()
/* push \xc3\x00 */
push 0x...
xor dword ptr [rsp], 0x...
>>> print [Link](\xc3, append_null = False).rstrip()
/* push \xc3 */
push 0x...c3
>>> with [Link]():
...
[Link] = amd64
47
...
print enhex(asm([Link]("/bin/sh")))
48b801010101010101015048b82e63686f2e72690148310424
>>> with [Link]():
...
[Link] = amd64
...
print enhex(asm([Link]("")))
6a01fe0c24
>>> with [Link]():
...
[Link] = amd64
...
print enhex(asm([Link]("\x00", False)))
6a01fe0c24
Parameters
string (str) The string to push.
append_null (bool) Whether to append a single NULL-byte before pushing.
[Link](return_value=None)
A single-byte RET instruction.
Parameters return_value Value to return
[Link]()
A trap instruction.
[Link]
48
>>>
>>>
>>>
>>>
49
Examples
>>> with [Link](arch=arm):
...
print enhex(asm([Link]()))
...
print enhex(asm([Link](0)))
...
print enhex(asm([Link](0xdeadbeef)))
1eff2fe1
000020e01eff2fe1
ef0e0be3ad0e4de31eff2fe1
[Link].to_thumb()
Go from ARM to THUMB mode.
[Link]
50
Parameters
dest (str) The destination register.
src (str) Either the input register, or an immediate value.
stack_allowed (bool) Can the stack be used?
[Link]()
A single-byte nop instruction.
[Link](value)
Pushes a value onto the stack without using null bytes or newline characters.
Parameters value (int,str) The value or register to push
[Link](string, append_null=True)
Pushes a string onto the stack without using null bytes or newline characters.
Example
>>> print [Link]().rstrip()
/* push \x00 */
push 1
51
Parameters
string (str) The string to push.
append_null (bool) Whether to append a single NULL-byte before pushing.
[Link](return_value=None)
A single-byte RET instruction.
Parameters return_value Value to return
[Link](cookie = 0x7afceb58)
Returns an an egghunter, which searches from esp and upwards for a cookie. However to save bytes,
it only looks at a single 4-byte alignment. Use the function stackhunter_helper to generate a suitable
52
[Link]()
A trap instruction.
[Link]
53
[Link].i386_to_amd64()
Returns code to switch from i386 to amd64 mode.
[Link].mprotect_all(clear_ebx=True, fix_null=False)
Calls mprotect(page, 4096, PROT_READ | PROT_WRITE | PROT_EXEC) for every page.
It takes around 0.3 seconds on my box, but your milage may vary.
Parameters
clear_ebx (bool) If this is set to False, then the shellcode will assume that ebx has already
been zeroed.
fix_null (bool) If this is set to True, then the NULL-page will also be mprotected at the
cost of slightly larger shellcode
[Link](gid=egid)
Args: [gid (imm/reg) = egid] Sets the real and effective group id.
[Link](uid=euid)
Args: [uid (imm/reg) = euid] Sets the real and effective user id.
[Link]()
Execute /bin/sh
[Link](sock, size)
Recives a fixed sized payload into a mmaped buffer Useful in conjuncion with findpeer.
[Link](syscall=None, arg0=None, arg1=None, arg2=None,
arg3=None, arg4=None)
Args: [syscall_number, *args] Does a syscall
Example
>>> print [Link](SYS_execve, 1, esp, 2, 0).rstrip()
/* call execve(1, esp, 2, 0) */
push 0x1
pop ebx
mov ecx, esp
push 0x2
pop edx
xor esi, esi
push 0xb
pop eax
int 0x80
>>> print [Link](SYS_execve, 2, 1, 0, 20).rstrip()
/* call execve(2, 1, 0, 20) */
push 0x2
pop ebx
push 0x1
pop ecx
push 0x14
pop esi
push 0xb
pop eax
cdq /* Set edx to 0, eax is known to be positive */
int 0x80
>>> print [Link]().rstrip()
/* call syscall() */
54
int 0x80
>>> print [Link](eax, ebx, ecx).rstrip()
/* call syscall(eax, ebx, ecx) */
/* moving ebx into ebx, but this is a no-op */
/* moving ecx into ecx, but this is a no-op */
/* moving eax into eax, but this is a no-op */
int 0x80
>>> print [Link](ebp, None, None, 1).rstrip()
/* call syscall(ebp, ?, ?, 1) */
push 0x1
pop edx
mov eax, ebp
int 0x80
[Link]
55
print en-
[Link](return_value=None)
A single-byte RET instruction.
Parameters return_value Value to return
[Link]
countdown(timeout=[Link])
Scoped timeout setter. Sets the timeout within the scope, and restores it when leaving the scope.
When accessing timeout within the scope, it will be calculated against the time when the scope was
entered, in a countdown fashion.
If None is specified for timeout, then the current timeout is used is made. This allows None to be
specified as a default argument with less complexity.
default = [Link]
Value indicating that the timeout should not be changed
forever = None
Value indicating that a timeout should not ever occur
local(timeout)
Scoped timeout setter. Sets the timeout within the scope, and restores it when leaving the scope.
2.16. [Link] Timeout handling
57
maximum = 1048576.0
Maximum value for a timeout. Used to get around platform issues with very large timeouts.
OSX does not permit setting socket timeouts to 2**22. Assume that if we receive a timeout of 2**21 or
greater, that the value is effectively infinite.
timeout
Timeout for obj operations. By default, uses [Link].
timeout_change()
Callback for subclasses to hook a timeout change.
2.17.1 Sockets
class [Link](host,
port,
fam=any,
typ=tcp,
timeout=[Link], ssl=False, sock=None)
Bases: [Link]
Creates a TCP or UDP-connection to a remote host. It supports both IPv4 and IPv6.
The returned object supports all the methods from [Link] and [Link].
Parameters
host (str) The host to connect to.
port (int) The port to connect to.
fam The string any,
[Link]().
ipv4
or
ipv6
or
an
integer
to
pass
to
58
...
PwnlibException: Could not connect to [Link] on port 1
>>> import socket
>>> s = [Link]()
>>> [Link](([Link], 80))
>>> [Link](GET / + \r\n*2)
9
>>> r = [Link](s)
>>> [Link](4)
HTTP
Traceback (most recent call last):
...
PwnlibException: Could not connect to [Link] on port 1
classmethod fromsocket(socket)
Helper method to wrap a standard python [Link] with the tube APIs.
Parameters socket Instance of [Link]
Returns Instance of [Link].
class [Link](port=0, bindaddr=[Link], fam=any,
out=[Link])
Bases: [Link]
typ=tcp,
time-
Creates an TCP or UDP-socket to receive data on. It supports both IPv4 and IPv6.
The returned object supports all the methods from [Link] and [Link].
Parameters
port (int) The port to connect to.
bindaddr (str) The address to bind to.
fam The string any,
[Link]().
ipv4
or
ipv6
or
an
integer
to
pass
to
2.17.2 Processes
class [Link](args, shell=False, executable=None, cwd=None, env=None,
timeout=[Link])
Bases: [Link]
Implements a tube which talks to a process on stdin/stdout/stderr.
59
Examples
>>> context.log_level=error
>>> p = process(which(python2))
>>> [Link]("print Hello world")
>>> [Link]("print Wow, such data");
>>> == [Link](timeout=0.01)
True
>>> [Link](send)
>>> [Link]
True
>>> [Link](send)
False
>>> [Link]()
Hello world\n
>>> [Link](,)
Wow,
>>> [Link](.*data)
such data
>>> [Link]()
\n
>>> [Link]()
Traceback (most recent call last):
...
EOFError
Traceback (most recent call last):
...
EOFError
2.17.3 SSH
class [Link](user,
host,
port=22,
password=None,
key=None,
file=None,
proxy_command=None,
proxy_sock=None,
out=[Link])
keytime-
__call__(attr)
Permits function-style access to run commands over SSH
Examples
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
print repr(s(echo hello))
hello
60
__getattr__(attr)
Permits member access to run commands over SSH
Examples
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
print [Link](hello)
...
print [Link]()
...
print [Link]([huh,yay,args])
hello
demouser
huh yay args
__getitem__(attr)
Permits indexed access to run commands over SSH
Examples
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
print s[echo hello]
hello
close()
Close the connection.
connect_remote(host, port, timeout = [Link]) ssh_connecter
Connects to a host through an SSH connection. This is equivalent to using the -L flag on ssh.
Returns a [Link].ssh_connecter object.
Examples
>>> from pwn import *
>>> l = listen()
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
a = s.connect_remote(localhost, [Link])
...
b = l.wait_for_connection()
...
[Link](Hello)
...
print repr([Link]())
Hello\n
connected()
Returns True if we are connected.
61
Example
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
print [Link]()
...
[Link]()
...
print [Link]()
True
False
download_data(remote)
Downloads a file from the remote server and returns it as a string.
Parameters remote (str) The remote filename to download.
Examples
>>> with file(/tmp/bar,w+) as f:
...
[Link](Hello, world)
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
print s.download_data(/tmp/bar)
Hello, world
download_dir(local, remote=None)
Recursively uploads a directory onto the remote server
Parameters
local Local directory
remote Remote directory
download_file(remote, local=None)
Downloads a file from the remote server.
The file is cached in /tmp/pwntools-ssh-cache using a hash of the file, so calling the function twice has
little overhead.
Parameters
remote (str) The remote filename to download
local (str) The local filename to save it to. Default is to infer it from the remote filename.
interactive(shell=None)
Create an interactive session.
This is a simple wrapper for creating a new [Link].ssh_channel object and calling
[Link].ssh_channel.interactive() on it.
libs(remote, directory=None)
Downloads the libraries referred to by a file.
This is done by running ldd on the remote server, parsing the output and downloading the relevant files.
The directory argument specified where to download the files. This defaults to ./$HOSTNAME where
$HOSTNAME is the hostname of the remote server.
62
set_working_directory(wd=None)
Sets the working directory in which future commands will be run (via [Link]) and to which files will be
uploaded/downloaded from if no path is provided
Parameters wd (string) Working directory. Default is to auto-generate a directory based on
the result of running mktemp -d on the remote machine.
63
Examples
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
cwd = s.set_working_directory()
...
print == [Link]()
...
print [Link]() == cwd
True
True
upload_data(data, remote)
Uploads some data into a file on the remote server.
Parameters
data (str) The data to upload.
remote (str) The filename to upload it to.
Examoles:
>>> with ssh(host=localhost,
...
user=demouser,
...
password=demopass) as s:
...
s.upload_data(Hello, world, /tmp/foo)
...
print file(/tmp/foo).read()
Hello, world
upload_dir(local, remote=None)
Recursively uploads a directory onto the remote server
Parameters
local Local directory
remote Remote directory
64
upload_file(filename, remote=None)
Uploads a file to the remote server. Returns the remote filename.
Args: filename(str): The local filename to download remote(str): The remote filename to save it to. Default
is to infer it from the local filename.
class [Link].ssh_channel
Bases: [Link]
interactive(prompt = [Link].bold_red($) + )
If not in TTY-mode, this does exactly the same as meth:[Link], otherwise it
does mostly the same.
An SSH connection in TTY-mode will typically supply its own prompt, thus the prompt argument is
ignored in this case. We also have a few SSH-specific hacks that will ideally be removed once the
[Link] is more mature.
kill()
Kills the process.
poll() int
Poll the exit code of the process. Will return None, if the process has not yet finished and the exit code
otherwise.
class [Link].ssh_connecter
Bases: [Link]
class [Link].ssh_listener
Bases: [Link]
65
Examples
__ne__(other)
Shorthand for connecting tubes to eachother.
The following are equivalent
a >> b >> a
a <> b
clean(timeout = 0.05)
Removes all the buffered data from a tube by calling [Link]() with a
low timeout until it fails.
If timeout is zero, only cached data will be cleared.
Note: If timeout is set to zero, the underlying network is not actually polled; only the internal buffer is
cleared.
Returns All data received
66
Examples
>>> t = tube()
>>> [Link](clean me up)
>>> [Link](0)
clean me up
>>> len([Link])
0
clean_and_log(timeout = 0.05)
Works exactly as [Link](),
[Link]().
>>>
...
>>>
>>>
>>>
>>>
>>>
...
close()
Closes the tube.
connect_both(other)
Connects the both ends of this tube object with another tube object.
connect_input(other)
Connects the input of this tube to the output of another tube object.
Examples
>>> def p(x): print x
>>> def recvone(n, data=[data]):
...
while data: return [Link]()
...
raise EOFError
>>> a = tube()
>>> b = tube()
>>> a.recv_raw = recvone
>>> b.send_raw = p
>>> a.connected_raw = lambda d: True
>>> b.connected_raw = lambda d: True
>>> [Link]
= lambda d: True
>>> [Link]
= lambda d: True
>>> import time
>>> _=(b.connect_input(a), [Link](0.1))
data
67
connect_output(other)
Connects the output of this tube to the input of another tube object.
Examples
>>> def p(x): print x
>>> def recvone(n, data=[data]):
...
while data: return [Link]()
...
raise EOFError
>>> a = tube()
>>> b = tube()
>>> a.recv_raw = recvone
>>> b.send_raw = p
>>> a.connected_raw = lambda d: True
>>> b.connected_raw = lambda d: True
>>> [Link]
= lambda d: True
>>> [Link]
= lambda d: True
>>> _=(a.connect_output(b), [Link](0.1))
data
connected_raw(direction)
connected(direction = any) -> bool
Should not be called directly. Returns True iff the tube is connected in the given direction.
fileno() int
Returns the file number used for reading.
interactive(prompt = [Link].bold_red($) + )
Does simultaneous reading and writing to the tube. In principle this just connects the tube to standard in
and standard out, but in practice this is much more usable, since we are using [Link] to print a
floating prompt.
68
recvall() str
Receives data until EOF is reached.
recvline(keepends = True) str
Receive a single line from the tube.
A line is any sequence of bytes terminated by the byte sequence set in newline, which defaults to
\n.
If the request is not satisfied before timeout seconds pass, all data is buffered and an empty string ()
is returned.
Parameters
keepends (bool) Keep the line ending (True).
timeout (int) Timeout
Returns All bytes received over the tube until the first newline \n is received. Optionally
retains the ending.
Examples
>>> t = tube()
>>> t.recv_raw = lambda n: Foo\nBar\r\nBaz\n
>>> [Link]()
Foo\n
>>> [Link]()
69
Bar\r\n
>>> [Link](keepends = False)
Baz
>>> [Link] = \r\n
>>> [Link](keepends = False)
Foo\nBar
Parameters pred (callable) Function to call. Returns the line for which this function returns
True.
Examples
>>> t = tube()
>>> t.recv_raw = lambda n:
>>> t.recvline_pred(lambda
Bar
>>> t.recvline_pred(lambda
Bar\n
>>> t.recvline_pred(lambda
"Foo\nBar\nBaz\n"
line: line == "Bar\n")
line: line == "Bar\n", keepends=True)
line: line == Nope!, timeout=0.1)
then
If the request is not satisfied before timeout seconds pass, all data is buffered and an empty string ()
is returned.
recvline_startswith(delims, keepends = False, timeout = default) str
Keep recieving lines until one is found that starts with one of delims. Returns the last line recieved.
If the request is not satisfied before timeout seconds pass, all data is buffered and an empty string ()
is returned.
Parameters
delims (str,tuple) List of strings to search for, or string of single characters
keepends (bool) Return lines with newlines if True
timeout (int) Timeout, in seconds
Returns The first line received which starts with a delimiter in delims.
Examples
>>> t = tube()
>>> t.recv_raw = lambda n: "Hello\nWorld\nXylophone\n"
>>> t.recvline_startswith(tuple(WXYZ))
World
>>> t.recvline_startswith(tuple(WXYZ), True)
Xylophone\n
>>> t.recvline_startswith(Wo)
World
71
Parameters
numlines (int) Maximum number of lines to receive
keepends (bool) Keep newlines at the end of each line (False).
timeout (int) Maximum timeout
Raises [Link]: The connection closed before the request could be satisfied
Returns A string containing bytes received from the socket, or if a timeout occurred while
waiting.
Examples
>>> t = tube()
>>> t.recv_raw = lambda n: \n
>>> [Link](3)
[, , ]
>>> t.recv_raw = lambda n: Foo\nBar\nBaz\n
>>> [Link](3)
[Foo, Bar, Baz]
>>> [Link](3, True)
[Foo\n, Bar\n, Baz\n]
72
If the request is not satisfied before timeout seconds pass, all data is buffered and an empty string ()
is returned.
Parameters
pred (callable) Function to call, with the currently-accumulated data.
timeout (int) Timeout for the operation
Raises [Link]: The connection is closed
Returns A string containing bytes received from the socket, or if a timeout occurred while
waiting.
recvregex(regex, exact = False, timeout = default) str
Wrapper around recvpred(), which will return when a regex matches the string in the buffer.
By default [Link]() is used,
[Link]() will be used instead.
then
If the request is not satisfied before timeout seconds pass, all data is buffered and an empty string ()
is returned.
recvrepeat()
Receives data until a timeout or EOF is reached.
Examples
>>> data = [
... d,
... , # simulate timeout
... c,
... b,
... a,
... ]
>>> def delayrecv(n, data=data):
...
return [Link]()
>>> t = tube()
>>> t.recv_raw = delayrecv
>>> [Link](0.2)
abc
>>> [Link]()
d
73
Examples
>>> t = tube()
>>> t.recv_raw = lambda n: "Hello World!"
>>> [Link]( )
Hello
>>> _=[Link](0)
>>> # Matches on o in Hello
>>> [Link](tuple( Wor))
Hello
>>> _=[Link](0)
>>> # Matches expressly full string
>>> [Link]( Wor)
Hello Wor
>>> _=[Link](0)
>>> # Matches on full string, drops match
>>> [Link]( Wor, drop=True)
Hello
>>> # Try with regex special characters
>>> t = tube()
>>> t.recv_raw = lambda n: "Hello|World"
>>> [Link](|, drop=True)
Hello
send(data)
Sends data.
If log level DEBUG is enabled, also prints out the data received.
If it is not possible to send anymore because of a closed connection, it raises [Link]
Examples
>>> def p(x): print repr(x)
>>> t = tube()
>>> t.send_raw = p
>>> [Link](hello)
hello
74
shutdown(direction = send)
Closes the tube for futher reading or writing depending on direction.
Parameters direction (str) Which direction to close; in, read or recv closes the tube in
the ingoing direction, out, write or send closes it in the outgoing direction.
Returns None
Examples
>>> def p(x): print x
>>> t = tube()
>>> t.shutdown_raw = p
>>> _=map([Link], (in, read, recv, out, write, send))
recv
recv
recv
send
send
send
>>> [Link](bad_value)
Traceback (most recent call last):
...
KeyError: "direction must be in [in, out, read, recv, send, write]"
Traceback (most recent call last):
...
KeyError: "direction must be in [in, out, read, recv, send, write]"
shutdown_raw(direction)
Should not be called directly. Closes the tube for further reading or writing.
spawn_process(*args, **kwargs)
Spawns a new process having this tube as stdin, stdout and stderr.
Takes the same arguments as [Link].
75
timeout_change()
Informs the raw layer of the tube that the timeout has changed.
Should not be called directly.
Inherited from Timeout.
unrecv(data)
Puts the specified data back at the beginning of the receive buffer.
Examples
>>> t = tube()
>>> t.recv_raw = lambda n: hello
>>> [Link]()
hello
>>> [Link]()
hello
>>> [Link](world)
>>> [Link]()
world
>>> [Link]()
hello
wait_for_close()
Waits until the tube is closed.
76
[Link]() str
Get a random user agent string.
Returns A random user agent string selected from getall().
77
[Link].find_crc_function(data, checksum)
Finds all known CRC functions that hashes a piece of data into a specific checksum. It does this by trying all
known CRC functions one after the other.
Parameters data (str) Data for which the checksum is known.
Example
>>> find_crc_function(test, 46197)
[<function crc_crc_16_dnp at ...>]
[Link](data) int
Calculates the arc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8005
width = 16
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print arc(123456789)
47933
[Link].crc_10(data) int
Calculates the crc_10 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x233
width = 10
init = 0x0
refin = False
refout = False
xorout = 0x0
78
[Link].crc_10_cdma2000(data) int
Calculates the crc_10_cdma2000 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x3d9
width = 10
init = 0x3ff
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_10_cdma2000(123456789)
563
[Link].crc_11(data) int
Calculates the crc_11 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x385
width = 11
init = 0x1a
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_11(123456789)
1443
79
[Link].crc_12_3gpp(data) int
Calculates the crc_12_3gpp checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x80f
width = 12
init = 0x0
refin = False
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_12_3gpp(123456789)
3503
[Link].crc_12_cdma2000(data) int
Calculates the crc_12_cdma2000 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0xf13
width = 12
init = 0xfff
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_12_cdma2000(123456789)
3405
[Link].crc_12_dect(data) int
Calculates the crc_12_dect checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x80f
width = 12
init = 0x0
refin = False
80
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_12_dect(123456789)
3931
[Link].crc_13_bbc(data) int
Calculates the crc_13_bbc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1cf5
width = 13
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_13_bbc(123456789)
1274
[Link].crc_14_darc(data) int
Calculates the crc_14_darc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x805
width = 14
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
81
Example
>>> print crc_14_darc(123456789)
2093
[Link].crc_15(data) int
Calculates the crc_15 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4599
width = 15
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_15(123456789)
1438
[Link].crc_15_mpt1327(data) int
Calculates the crc_15_mpt1327 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x6815
width = 15
init = 0x0
refin = False
refout = False
xorout = 0x1
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_15_mpt1327(123456789)
9574
[Link].crc_16_aug_ccitt(data) int
Calculates the crc_16_aug_ccitt checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
82
width = 16
init = 0x1d0f
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_aug_ccitt(123456789)
58828
[Link].crc_16_buypass(data) int
Calculates the crc_16_buypass checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8005
width = 16
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_buypass(123456789)
65256
[Link].crc_16_ccitt_false(data) int
Calculates the crc_16_ccitt_false checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0xffff
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
83
Example
>>> print crc_16_ccitt_false(123456789)
10673
[Link].crc_16_cdma2000(data) int
Calculates the crc_16_cdma2000 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0xc867
width = 16
init = 0xffff
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_cdma2000(123456789)
19462
[Link].crc_16_dds_110(data) int
Calculates the crc_16_dds_110 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8005
width = 16
init = 0x800d
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_dds_110(123456789)
40655
[Link].crc_16_dect_r(data) int
Calculates the crc_16_dect_r checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x589
84
width = 16
init = 0x0
refin = False
refout = False
xorout = 0x1
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_dect_r(123456789)
126
[Link].crc_16_dect_x(data) int
Calculates the crc_16_dect_x checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x589
width = 16
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_dect_x(123456789)
127
[Link].crc_16_dnp(data) int
Calculates the crc_16_dnp checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x3d65
width = 16
init = 0x0
refin = True
refout = True
xorout = 0xffff
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
85
Example
>>> print crc_16_dnp(123456789)
60034
[Link].crc_16_en_13757(data) int
Calculates the crc_16_en_13757 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x3d65
width = 16
init = 0x0
refin = False
refout = False
xorout = 0xffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_en_13757(123456789)
49847
[Link].crc_16_genibus(data) int
Calculates the crc_16_genibus checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0xffff
refin = False
refout = False
xorout = 0xffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_genibus(123456789)
54862
[Link].crc_16_maxim(data) int
Calculates the crc_16_maxim checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8005
86
width = 16
init = 0x0
refin = True
refout = True
xorout = 0xffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_maxim(123456789)
17602
[Link].crc_16_mcrf4xx(data) int
Calculates the crc_16_mcrf4xx checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0xffff
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_mcrf4xx(123456789)
28561
[Link].crc_16_riello(data) int
Calculates the crc_16_riello checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0xb2aa
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
87
Example
>>> print crc_16_riello(123456789)
25552
[Link].crc_16_t10_dif(data) int
Calculates the crc_16_t10_dif checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8bb7
width = 16
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_t10_dif(123456789)
53467
[Link].crc_16_teledisk(data) int
Calculates the crc_16_teledisk checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0xa097
width = 16
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_teledisk(123456789)
4019
[Link].crc_16_tms37157(data) int
Calculates the crc_16_tms37157 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
88
width = 16
init = 0x89ec
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_tms37157(123456789)
9905
[Link].crc_16_usb(data) int
Calculates the crc_16_usb checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8005
width = 16
init = 0xffff
refin = True
refout = True
xorout = 0xffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_16_usb(123456789)
46280
[Link].crc_24(data) int
Calculates the crc_24 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x864cfb
width = 24
init = 0xb704ce
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
89
Example
>>> print crc_24(123456789)
2215682
[Link].crc_24_flexray_a(data) int
Calculates the crc_24_flexray_a checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x5d6dcb
width = 24
init = 0xfedcba
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_24_flexray_a(123456789)
7961021
[Link].crc_24_flexray_b(data) int
Calculates the crc_24_flexray_b checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x5d6dcb
width = 24
init = 0xabcdef
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_24_flexray_b(123456789)
2040760
[Link].crc_31_philips(data) int
Calculates the crc_31_philips checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4c11db7
90
width = 31
init = 0x7fffffff
refin = False
refout = False
xorout = 0x7fffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_31_philips(123456789)
216654956
[Link].crc_32(data) int
Calculates the crc_32 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4c11db7
width = 32
init = 0xffffffff
refin = True
refout = True
xorout = 0xffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_32(123456789)
3421780262
[Link].crc_32_bzip2(data) int
Calculates the crc_32_bzip2 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4c11db7
width = 32
init = 0xffffffff
refin = False
refout = False
xorout = 0xffffffff
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
91
Example
>>> print crc_32_bzip2(123456789)
4236843288
[Link].crc_32_mpeg_2(data) int
Calculates the crc_32_mpeg_2 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4c11db7
width = 32
init = 0xffffffff
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_32_mpeg_2(123456789)
58124007
[Link].crc_32_posix(data) int
Calculates the crc_32_posix checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4c11db7
width = 32
init = 0x0
refin = False
refout = False
xorout = 0xffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_32_posix(123456789)
1985902208
[Link].crc_32c(data) int
Calculates the crc_32c checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1edc6f41
92
width = 32
init = 0xffffffff
refin = True
refout = True
xorout = 0xffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_32c(123456789)
3808858755
[Link].crc_32d(data) int
Calculates the crc_32d checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0xa833982b
width = 32
init = 0xffffffff
refin = True
refout = True
xorout = 0xffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_32d(123456789)
2268157302
[Link].crc_32q(data) int
Calculates the crc_32q checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x814141ab
width = 32
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
93
Example
>>> print crc_32q(123456789)
806403967
[Link].crc_3_rohc(data) int
Calculates the crc_3_rohc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x3
width = 3
init = 0x7
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_3_rohc(123456789)
6
[Link].crc_40_gsm(data) int
Calculates the crc_40_gsm checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4820009
width = 40
init = 0x0
refin = False
refout = False
xorout = 0xffffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_40_gsm(123456789)
910907393606
[Link].crc_4_itu(data) int
Calculates the crc_4_itu checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x3
94
width = 4
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_4_itu(123456789)
7
[Link].crc_5_epc(data) int
Calculates the crc_5_epc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x9
width = 5
init = 0x9
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_5_epc(123456789)
0
[Link].crc_5_itu(data) int
Calculates the crc_5_itu checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x15
width = 5
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
95
Example
>>> print crc_5_itu(123456789)
7
[Link].crc_5_usb(data) int
Calculates the crc_5_usb checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x5
width = 5
init = 0x1f
refin = True
refout = True
xorout = 0x1f
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_5_usb(123456789)
25
[Link].crc_64(data) int
Calculates the crc_64 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x42f0e1eba9ea3693
width = 64
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_64(123456789)
7800480153909949255
[Link].crc_64_we(data) int
Calculates the crc_64_we checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x42f0e1eba9ea3693
96
width = 64
init = 0xffffffffffffffff
refin = False
refout = False
xorout = 0xffffffffffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_64_we(123456789)
7128171145767219210
[Link].crc_64_xz(data) int
Calculates the crc_64_xz checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x42f0e1eba9ea3693
width = 64
init = 0xffffffffffffffff
refin = True
refout = True
xorout = 0xffffffffffffffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_64_xz(123456789)
11051210869376104954
[Link].crc_6_cdma2000_a(data) int
Calculates the crc_6_cdma2000_a checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x27
width = 6
init = 0x3f
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
97
Example
>>> print crc_6_cdma2000_a(123456789)
13
[Link].crc_6_cdma2000_b(data) int
Calculates the crc_6_cdma2000_b checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x7
width = 6
init = 0x3f
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_6_cdma2000_b(123456789)
59
[Link].crc_6_darc(data) int
Calculates the crc_6_darc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x19
width = 6
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_6_darc(123456789)
38
[Link].crc_6_itu(data) int
Calculates the crc_6_itu checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x3
98
width = 6
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_6_itu(123456789)
6
[Link].crc_7(data) int
Calculates the crc_7 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x9
width = 7
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_7(123456789)
117
[Link].crc_7_rohc(data) int
Calculates the crc_7_rohc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4f
width = 7
init = 0x7f
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
99
Example
>>> print crc_7_rohc(123456789)
83
[Link].crc_8(data) int
Calculates the crc_8 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x7
width = 8
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8(123456789)
244
[Link].crc_82_darc(data) int
Calculates the crc_82_darc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x308c0111011401440411
width = 82
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_82_darc(123456789)
749237524598872659187218
[Link].crc_8_cdma2000(data) int
Calculates the crc_8_cdma2000 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x9b
100
width = 8
init = 0xff
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_cdma2000(123456789)
218
[Link].crc_8_darc(data) int
Calculates the crc_8_darc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x39
width = 8
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_darc(123456789)
21
[Link].crc_8_dvb_s2(data) int
Calculates the crc_8_dvb_s2 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0xd5
width = 8
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
101
Example
>>> print crc_8_dvb_s2(123456789)
188
[Link].crc_8_ebu(data) int
Calculates the crc_8_ebu checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1d
width = 8
init = 0xff
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_ebu(123456789)
151
[Link].crc_8_i_code(data) int
Calculates the crc_8_i_code checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1d
width = 8
init = 0xfd
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_i_code(123456789)
126
[Link].crc_8_itu(data) int
Calculates the crc_8_itu checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x7
102
width = 8
init = 0x0
refin = False
refout = False
xorout = 0x55
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_itu(123456789)
161
[Link].crc_8_maxim(data) int
Calculates the crc_8_maxim checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x31
width = 8
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_maxim(123456789)
161
[Link].crc_8_rohc(data) int
Calculates the crc_8_rohc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x7
width = 8
init = 0xff
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
103
Example
>>> print crc_8_rohc(123456789)
208
[Link].crc_8_wcdma(data) int
Calculates the crc_8_wcdma checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x9b
width = 8
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_8_wcdma(123456789)
37
[Link].crc_a(data) int
Calculates the crc_a checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0xc6c6
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print crc_a(123456789)
48901
[Link](data) int
Calculates the jamcrc checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x4c11db7
104
width = 32
init = 0xffffffff
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print jamcrc(123456789)
873187033
[Link](data) int
Calculates the kermit checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0x0
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print kermit(123456789)
8585
[Link](data) int
Calculates the modbus checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x8005
width = 16
init = 0xffff
refin = True
refout = True
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
2.20. [Link] Calculating CRC-sums
105
Example
>>> print modbus(123456789)
19255
[Link].x_25(data) int
Calculates the x_25 checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
width = 16
init = 0xffff
refin = True
refout = True
xorout = 0xffff
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print x_25(123456789)
36974
[Link](data) int
Calculates the xfer checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0xaf
width = 32
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print xfer(123456789)
3171672888
[Link](data) int
Calculates the xmodem checksum.
This is simply the generic_crc() with these frozen arguments:
polynom = 0x1021
106
width = 16
init = 0x0
refin = False
refout = False
xorout = 0x0
See also: [Link]
Parameters data (str) The data to checksum.
Example
>>> print xmodem(123456789)
12739
107
Examples
>>> cyclic_find(cyclic(1000)[514:518])
514
[Link].b64e(s) str
Base64 encodes a string
Example
>>> b64e("test")
dGVzdA==
108
[Link](s) str
Reverses the bits in every byte of a given string.
Example
>>> bitswap("1234")
\x8cL\xcc,
[Link].bitswap_int(n) int
Reverses the bits of a numbers and returns the result as a new number.
Parameters
n (int) The number to swap.
width (int) The width of the integer
Examples
>>> hex(bitswap_int(0x1234,
0x2c
>>> hex(bitswap_int(0x1234,
0x2c48
>>> hex(bitswap_int(0x1234,
0x2c4800
>>> hex(bitswap_int(0x1234,
0x589000
8))
16))
24))
25))
[Link](x) str
Hex-encodes a string.
Example
>>> enhex("test")
74657374
109
[Link](n, k, word_size=None)
Returns a rotation by k of n.
When n is a number, then means ((n << k) | (n >> (word_size - k))) truncated to word_size
bits.
When n is a list, tuple or string, this is n[k % len(n):]
+ n[:k % len(n)].
Parameters
n The value to rotate.
k (int) The rotation amount. Can be a positive or negative number.
110
[Link](n, k, word_size=None)
A simple wrapper around rol(), which negates the values of k.
[Link](s, endian = big) str
Converts an iterable of bits into a string.
Parameters
s Iterable of bits
endian (str) The string little or big, which specifies the bits endianness.
Returns A string of the decoded bits.
Example
>>> unbits([1])
\x80
>>> unbits([1], endian = little)
\x01
>>> unbits(bits(hello), endian = little)
\x16\xa666\xf6
[Link](s) str
Hex-decodes a string.
Example
>>> unhex("74657374")
test
111
...
ValueError: Invalid input to urldecode
>>> urldecode("%qq", ignore_invalid = True)
%qq
Traceback (most recent call last):
...
ValueError: Invalid input to urldecode
[Link](s) str
URL-encodes a string.
Example
>>> urlencode("test")
%74%65%73%74
112
[Link].md5file(x)
Calculates the md5 sum of a file
[Link].md5filehex(x)
Calculates the md5 sum of a file; returns hex-encoded
[Link].md5sum(x)
Calculates the md5 sum of a string
[Link].md5sumhex(x)
Calculates the md5 sum of a string; returns hex-encoded
[Link].sha1file(x)
Calculates the sha1 sum of a file
[Link].sha1filehex(x)
Calculates the sha1 sum of a file; returns hex-encoded
[Link].sha1sum(x)
Calculates the sha1 sum of a string
[Link].sha1sumhex(x)
Calculates the sha1 sum of a string; returns hex-encoded
[Link].sha224file(x)
Calculates the sha224 sum of a file
[Link].sha224filehex(x)
Calculates the sha224 sum of a file; returns hex-encoded
[Link].sha224sum(x)
Calculates the sha224 sum of a string
[Link].sha224sumhex(x)
Calculates the sha224 sum of a string; returns hex-encoded
[Link].sha256file(x)
Calculates the sha256 sum of a file
[Link].sha256filehex(x)
Calculates the sha256 sum of a file; returns hex-encoded
[Link].sha256sum(x)
Calculates the sha256 sum of a string
[Link].sha256sumhex(x)
Calculates the sha256 sum of a string; returns hex-encoded
[Link].sha384file(x)
Calculates the sha384 sum of a file
[Link].sha384filehex(x)
Calculates the sha384 sum of a file; returns hex-encoded
[Link].sha384sum(x)
Calculates the sha384 sum of a string
[Link].sha384sumhex(x)
Calculates the sha384 sum of a string; returns hex-encoded
[Link].sha512file(x)
Calculates the sha512 sum of a file
113
[Link].sha512filehex(x)
Calculates the sha512 sum of a file; returns hex-encoded
[Link].sha512sum(x)
Calculates the sha512 sum of a string
[Link].sha512sumhex(x)
Calculates the sha512 sum of a string; returns hex-encoded
2.24 [Link]
itertools
Extension
of
standard
module
[Link](func)
A decorator chaining the results of func. Useful for generators.
Parameters func (function) The function being decorated.
Returns A generator function whoose elements are the concatenation of the return values from
func(*args, **kwargs).
Example
>>> @chained
... def g():
...
for x in count():
...
yield (x, -x)
114
[Link](n, iterator)
Advance the iterator n steps ahead. If n is :const:None, consume everything.
Parameters
n (int) Number of elements to consume.
iterator (iterator) An iterator.
Returns None.
Examples
>>>
>>>
>>>
5
>>>
>>>
>>>
[3,
i = count()
consume(5, i)
[Link]()
i = iter([1, 2, 3, 4, 5])
consume(2, i)
list(i)
4, 5]
[Link](x, y) int
Computes the dot product of x and y.
Parameters
x (iterable) An iterable.
x An iterable.
Returns The dot product of x and y, i.e.: x[0] * y[0] + x[1] * y[1] + ....
Example
115
[Link](xss) iterator
Flattens one level of nesting; when xss is an iterable of iterables, returns an iterator whoose elements is the
concatenation of the elements of xss.
Parameters xss An iterable of iterables.
Returns An iterator whoose elements are the concatenation of the iterables in xss.
Examples
>>> list(flatten([[1, 2], [3, 4]]))
[1, 2, 3, 4]
>>> take(6, flatten([[43, 42], [41, 40], count()]))
[43, 42, 41, 40, 0, 1]
[Link].iter_except(func, exception)
Calls func repeatedly until an exception is raised. Works like the build-in iter() but uses an exception instead
of a sentinel to signal the end.
Parameters
func The function to call.
exception (exception) The exception that signals the end. Other exceptions will not be
caught.
Returns An iterator whoose elements are the results of calling func() until an exception matching
exception is raised.
116
Examples
>>> s = {1, 2, 3}
>>> i = iter_except([Link], KeyError)
>>> [Link]()
1
>>> [Link]()
2
>>> [Link]()
3
>>> [Link]()
Traceback (most recent call last):
...
StopIteration
Traceback (most recent call last):
...
StopIteration
[Link](alphabet) iterator
The words with symbols in alphabet, in lexicographic order (determined by the order of alphabet).
Parameters alphabet The alphabet to draw symbols from.
Returns An iterator of the words with symbols in alphabet, in lexicographic order.
Example
>>> take(8, imap(lambda x: .join(x), lexicographic(01)))
[, 0, 1, 00, 01, 10, 11, 000]
i = count()
lookahead(4, i)
[Link]()
i = count()
nth(4, i)
[Link]()
lookahead(4, i)
117
[Link](iterable) iterator
Parameters iterable An iterable.
Returns An iterator whoose elements are pairs of neighbouring elements of iterable.
118
Examples
>>> list(pairwise([1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]
>>> i = starmap([Link], pairwise(count()))
>>> take(5, i)
[1, 3, 5, 7, 9]
[Link].random_combination(iterable, r) tuple
Parameters
iterable An iterable.
r (int) Size of the combination.
Returns A random element from [Link](iterable, r = r).
Examples
119
>>> random_combination(range(2), 2)
(0, 1)
>>> random_combination(range(10), r = 2) in combinations(range(10), r = 2)
True
[Link].random_combination_with_replacement(iterable, r)
random_combination(iterable, r) -> tuple
Parameters
iterable An iterable.
r (int) Size of the combination.
Returns A random element from itertools.combinations_with_replacement(iterable,
r = r).
Examples
>>> cs = {(0, 0), (0, 1), (1, 1)}
>>> random_combination_with_replacement(range(2), 2) in cs
True
>>> i = combinations_with_replacement(range(10), r = 2)
>>> random_combination_with_replacement(range(10), r = 2) in i
True
[Link].random_permutation(iterable, r=None)
random_product(iterable, r = None) -> tuple
Parameters
iterable An iterable.
r (int) Size of the permutation. If None select all elements in iterable.
Returns A random element from [Link](iterable, r = r).
Examples
>>> random_permutation(range(2)) in {(0, 1), (1, 0)}
True
>>> random_permutation(range(10), r = 2) in permutations(range(10), r = 2)
True
120
[Link](*iterables)
Take elements from iterables in a round-robin fashion.
Returns An iterator whoose elements are taken from iterables in a round-robin fashion.
Examples
>>> .join(roundrobin(ABC, D, EF))
ADEBFC
>>> .join(take(10, roundrobin(ABC, DE, repeat(x))))
ADxBExCxxx
121
take(2, range(10))
1]
i = count()
take(2, i)
1]
take(2, i)
3]
take(9001, [1, 2, 3])
2, 3]
122
>>> .join(unique_everseen(AAAABBBCCDAABBB))
ABCD
>>> .join(unique_everseen(ABBCcAD, [Link]))
ABCD
[Link].unique_justseen(iterable, key=None)
unique_everseen(iterable, key = None) -> iterator
Get unique elements, preserving order. Remember only the elements just seen. If key is not None then for each
element elm in iterable the element that will be rememberes is key(elm). Otherwise elm is remembered.
Parameters
iterable An iterable.
key A function to map over each element in iterable before remembering it. Setting to
None is equivalent to the identity function.
Returns An iterator of the unique elements in iterable.
Examples
>>> .join(unique_justseen(AAAABBBCCDAABBB))
ABCDAB
>>> .join(unique_justseen(ABBCcAD, [Link]))
ABCAD
[Link]()
Alias for [Link]().
[Link]()
Alias for [Link]()
123
[Link].combinations_with_replacement()
Alias for itertools.combinations_with_replacement()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link].izip_longest()
Alias for itertools.izip_longest()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
[Link]()
Alias for [Link]()
124
Example
>>> concat([[1, 2], [3]])
[1, 2, 3]
[Link].concat_all(*args) list
Concats all the arguments together.
Example
>>> concat_all(0, [1, (2, 3)], [([[4, 5, 6]])])
[0, 1, 2, 3, 4, 5, 6]
[Link](l, e) l
Generate all indices of needle in haystack, using the Knuth-Morris-Pratt algorithm.
Example
>>>
>>>
3
>>>
4
>>>
6
foo = findall([1,2,3,4,4,3,4,2,1], 4)
[Link]()
[Link]()
[Link]()
[Link](s) list
Turns a string into a list of the corresponding ascii values.
2.25. [Link] Operations on lists
125
Example
>>> ordlist("hello")
[104, 101, 108, 108, 111]
[Link](cs) str
Takes a list of ascii values and returns the corresponding string.
Example
>>> unordlist([104, 101, 108, 108, 111])
hello
[Link].align_down(alignment, x) int
Rounds x down to nearest multiple of the alignment.
Example
>>> [align_down(5, n) for n in range(15)]
[0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10]
[Link].binary_ip(host) str
Resolve host and return IP as four byte string.
126
Example
>>> binary_ip("[Link]")
\x7f\x00\x00\x01
[Link].mkdir_p(path)
Emulates the behavior of mkdir -p.
[Link].parse_ldd_output(output)
Parses the output from a run of ldd on a binary. Returns a dictionary of {path: address} for each library
required by the specified binary.
Parameters output (str) The output to parse
Example
>>> sorted(parse_ldd_output(
...
[Link].1 => (0x00007fffbf5fe000)
...
[Link].5 => /lib/x86_64-linux-gnu/[Link].5 (0x00007fe28117f000)
...
[Link].2 => /lib/x86_64-linux-gnu/[Link].2 (0x00007fe280f7b000)
...
[Link].6 => /lib/x86_64-linux-gnu/[Link].6 (0x00007fe280bb4000)
...
/lib64/[Link].2 (0x00007fe2813dd000)
... ).keys())
[/lib/x86_64-linux-gnu/[Link].6, /lib/x86_64-linux-gnu/[Link].2, /lib/x86_64-linux-gnu/l
[Link](path) str
Open file, return content.
Examples
>>> read(pwnlib/util/[Link]).split(\n)[0]
import socket, re, os, stat, errno, string, base64, logging
[Link].register_sizes(regs, in_sizes)
Create dictionaries over register sizes and relations
Given a list of lists of overlapping register names (e.g. [eax,ax,al,ah]) and a list of input sizes, it returns
the following:
all_regs : list of all valid registers
sizes[reg] : the size of reg in bits
bigger[reg] : list of overlapping registers bigger than reg
smaller[reg]: list of overlapping registers smaller than reg
Used in i386/AMD64 shellcode, e.g. the mov-shellcode.
Example
>>>
...
...
...
...
127
128
Example
>>> size(451)
451B
>>> size(1000)
1000B
>>> size(1024)
1.00KB
>>> size(1024, si = True)
1.02KB
>>> [size(1024 ** n) for n in range(7)]
[1B, 1.00KB, 1.00MB, 1.00GB, 1.00TB, 1.00PB, 1024.00PB]
129
Default False.
Returns A dictionary mapping each of the hosts interfaces to a list of its IPv4 addresses.
[Link].interfaces6(all = False) dict
As interfaces() but only includes IPv6 addresses and the lists in the dictionary only contains the addresses
not the family.
Parameters
all (bool) Whether to include interfaces with not associated address.
Default False.
Returns A dictionary mapping each of the hosts interfaces to a list of its IPv6 addresses.
130
131
132
big, True)
little, True)
big, False)
big, True)
call last):
133
...
ValueError: pack(): number does not fit within word_size
>>> pack(0x814243, 25, big, True)
\x00\x81BC
>>> pack(-1, all, little, True)
\xff
>>> pack(-256, all, big, True)
\xff\x00
>>> pack(0x0102030405, all, little, True)
\x05\x04\x03\x02\x01
Traceback (most recent call last):
...
ValueError: pack(): number does not fit within word_size
134
If word_size is not a multiple of 8, then the bits used for padding are discarded.
Parameters
number (int) String to convert
word_size (int) Word size of the converted integer or the string all.
endianness (str) Endianness of the converted integer (little/big)
sign (str) Signedness of the converted integer (False/True)
kwargs Anything that can be passed to [Link]
Returns The unpacked number.
Examples
>>> hex(unpack(\xaa\x55, 16,
0x55aa
>>> hex(unpack(\xaa\x55, 16,
0xaa55
>>> hex(unpack(\xaa\x55, 16,
-0x55ab
>>> hex(unpack(\xaa\x55, 15,
0x2a55
>>> hex(unpack(\xff\x02\x03,
0x302ff
>>> hex(unpack(\xff\x02\x03,
-0xfdfd
little, False))
big, False))
big, True))
big, True))
all, little, True))
all, big, True))
135
sign=None,
Splits data into groups of word_size//8 bytes and calls unpack() on each group. Returns a list of the
results.
word_size must be a multiple of 8 or the string all. In the latter case a singleton list will always be returned.
Parameters
number (int) String to convert
word_size (int) Word size of the converted integers or the string all.
endianness (str) Endianness of the converted integer (little/big)
sign (str) Signedness of the converted integer (False/True)
kwargs Anything that can be passed to [Link]
Returns The unpacked numbers.
Examples
>>> map(hex, unpack_many(\xaa\x55\xcc\x33, 16, little, False))
[0x55aa, 0x33cc]
>>> map(hex, unpack_many(\xaa\x55\xcc\x33, 16, big, False))
[0xaa55, 0xcc33]
>>> map(hex, unpack_many(\xaa\x55\xcc\x33, 16, big, True))
[-0x55ab, -0x33cd]
>>> map(hex, unpack_many(\xff\x02\x03, all, little, True))
[0x302ff]
>>> map(hex, unpack_many(\xff\x02\x03, all, big, True))
[-0xfdfd]
[Link](pid) dict
Parameters pid (int) PID of the process.
Returns Dictionary mapping the PID of each child of pid to its descendants.
[Link](pid) str
Parameters pid (int) PID of the process.
Returns The path of the binary of the process. I.e. what /proc/<pid>/exe points to.
[Link](pid) str
Parameters pid (int) PID of the process.
Returns Name of process as listed in /proc/<pid>/status.
Example
>>> name([Link]()) == [Link]([Link][0])
True
[Link](pid) int
Parameters pid (int) PID of the process.
Returns Parent PID as listed in /proc/<pid>/status under PPid, or 0 if there is not parent.
[Link].pid_by_name(name) int list
Parameters name (str) Name of program.
Returns List of PIDs matching name sorted by lifetime, youngest to oldest.
Example
>>> [Link]() in pid_by_name(name([Link]()))
True
137
Returns A list of the values in /proc/<pid>/stat, with the exception that ( and ) has been
removed from around the process name.
[Link](pid) str
Parameters pid (int) PID of the process.
Returns State of the process as listed in /proc/<pid>/status. See proc(5) for details.
Example
>>> state([Link]())
R (running)
[Link](pid) dict
Get the status of a process.
Parameters pid (int) PID of the process.
Returns The contents of /proc/<pid>/status as a dictionary.
[Link](pid) int
Parameters pid (int) PID of the process.
Returns PID of the process tracing pid, or None if no pid is not being traced.
Example
>>> tracer([Link]()) is None
True
[Link].wait_for_debugger(pid) None
Sleeps until the process with PID pid is being traced.
Parameters pid (int) PID of the process.
Returns None
138
[Link](expression) value
Safe Python expression evaluation
Evaluates a string that contains an expression that only uses Python constants. This can be used to e.g. evaluate
a numerical expression from an untrusted source.
>>> expr("1+2")
3
>>> expr("[1,2]*2")
[1, 2, 1, 2]
>>> expr("__import__(sys).modules")
Traceback (most recent call last):
...
ValueError: opcode LOAD_NAME not allowed
Traceback (most recent call last):
...
ValueError: opcode LOAD_NAME not allowed
139
140
CHAPTER 3
genindex
modindex
search
141
142
p
pwn, 3
pwnlib, 3
[Link], 17
[Link], 19
[Link], 20
[Link], 20
[Link], 29
[Link], 31
[Link], 35
[Link], 35
[Link], 37
[Link], 38
[Link], 43
[Link], 44
[Link], 45
[Link].amd64, 46
[Link], 48
[Link], 49
[Link], 50
[Link], 50
[Link].i386, 50
[Link], 55
[Link], 53
[Link], 55
[Link], 56
[Link], 56
[Link], 56
[Link], 58
[Link], 59
[Link], 59
[Link], 58
[Link], 59
[Link], 60
[Link], 65
[Link], 76
[Link], 76
[Link], 77
[Link], 107
[Link], 108
[Link], 112
[Link], 114
[Link], 124
[Link], 126
[Link], 129
[Link], 130
[Link], 136
[Link], 138
[Link], 139
143
144
Index
Symbols
color <color>
phd command line option, 15
-?, show
shellcraft command line option, 15
-a <alphabet>, alphabet <alphabet>
cyclic command line option, 13
-c <<opt>>, context <<opt>>
asm command line option, 12
constgrep command line option, 13
disasm command line option, 13
-c <count>, count <count>
phd command line option, 14
-e <<constant name>>, exact <<constant name>>
constgrep command line option, 13
-f <<format>>, format <<format>>
shellcraft command line option, 15
-f <format>, format <format>
asm command line option, 12
-h, help
asm command line option, 12
constgrep command line option, 13
cyclic command line option, 13
disasm command line option, 13
elfdiff command line option, 14
elfpatch command line option, 14
hex command line option, 14
phd command line option, 14
shellcraft command line option, 15
unhex command line option, 15
-i, case-insensitive
constgrep command line option, 13
-l <<lookup value>>, -o <<lookup value>>, offset
<<lookup value>>, lookup <<lookup value>>
cyclic command line option, 13
-l <highlight>, highlight <highlight>
phd command line option, 14
-m, mask-mode
constgrep command line option, 13
-n <length>, length <length>
A
a
elfdiff command line option, 14
acceptloop_ipv4()
(in
module
[Link]), 55
acceptloop_ipv4()
(in
module
[Link]), 53
address ([Link] attribute), 32
align() (in module [Link]), 126
align_down() (in module [Link]), 126
ancestors() (in module [Link]), 136
arc() (in module [Link]), 78
arch ([Link] attribute), 23
architectures ([Link] attribute), 24
asm command line option
-c <<opt>>, context <<opt>>, 12
-f <format>, format <format>, 12
-h, help, 12
145
Index
Index
D
d() ([Link] method), 41
data
hex command line option, 14
de_bruijn() (in module [Link]), 108
debug() (in module [Link]), 35
debug() ([Link] method), 38
default ([Link] attribute), 57
defaults ([Link] attribute), 25
descendants() (in module [Link]), 136
disasm command line option
-c <<opt>>, context <<opt>>, 13
-h, help, 13
hex, 13
disasm() (in module [Link]), 18
disasm() ([Link] method), 32
dotproduct() (in module [Link]), 115
download_data() ([Link] method), 62
download_dir() ([Link] method), 62
download_file() ([Link] method), 62
dropwhile() (in module [Link]), 124
dump() ([Link] method), 45
dup() (in module [Link]), 48
dup() (in module [Link]), 53
dup() (in module [Link]), 56
dupsh() (in module [Link]), 48
dupsh() (in module [Link]), 53
dupsh() (in module [Link]), 56
dwarf ([Link] attribute), 32
dynamic ([Link] attribute), 31
DynELF (class in [Link]), 30
E
echo() (in module [Link]), 48
echo() (in module [Link]), 53
egghunter() (in module [Link]), 50
elf
elfpatch command line option, 14
ELF (class in [Link]), 31
elfclass ([Link] attribute), 31
elfclass ([Link] attribute), 32
elfdiff command line option
-h, help, 14
a, 14
b, 14
elfpatch command line option
-h, help, 14
bytes, 14
elf, 14
offset, 14
elftype ([Link] attribute), 32
endian ([Link] attribute), 25
endianness ([Link] attribute), 26
endiannesses ([Link] attribute), 26
147
i386_to_amd64()
(in
module
[Link]), 55
i386_to_amd64()
(in
module
[Link]), 53
ifilter() (in module [Link]), 124
ifilterfalse() (in module [Link]), 124
imap() (in module [Link]), 124
F
indented() ([Link] method), 38
failure() ([Link] method), 38
infloop() (in module [Link].amd64), 46
field() ([Link] method), 41
infloop() (in module [Link]), 49
file
infloop() (in module [Link].i386), 50
phd command line option, 14
infloop() (in module [Link]), 55
fileno() ([Link] method), 68
info() ([Link] method), 38
find_base() ([Link] static method), 31
info_once() ([Link] method), 38
find_crc_function() (in module [Link]), 78
init() (in module [Link]), 56
find_module_addresses() (in module [Link]), 36
interactive() ([Link] method), 62
findall() (in module [Link]), 125
interactive() ([Link].ssh_channel method), 65
findpeer() (in module [Link]), 53
interactive() ([Link] method), 68
findpeer() (in module [Link]), 56
interfaces() (in module [Link]), 129
findpeersh() (in module [Link]), 53 interfaces4() (in module [Link]), 129
findpeersh() (in module [Link]), interfaces6() (in module [Link]), 130
56
islice() (in module [Link]), 124
findpeerstager() (in module [Link]), isprint() (in module [Link]), 110
53
iter_except() (in module [Link]), 116
flat() (in module [Link]), 130
izip() (in module [Link]), 124
flatten() (in module [Link]), 116
izip_longest() (in module [Link]), 124
forever ([Link] attribute), 57
fromsocket() ([Link] class method), J
59
jamcrc() (in module [Link]), 104
H
hex
disasm command line option, 13
unhex command line option, 15
hex command line option
-h, help, 14
data, 14
hexdump_iter() (in module [Link]), 109
hexii() (in module [Link]), 110
148
L
label() (in module [Link]), 50
lexicographic() (in module [Link]), 117
libs() ([Link] method), 62
line
asm command line option, 12
link_map ([Link] attribute), 31
listen (class in [Link]), 59
listen() (in module [Link]), 56
listen_remote() ([Link] method), 62
load() (in module [Link]), 31
local() ([Link] method), 26
local() ([Link] method), 57
log_level ([Link] attribute), 27
Logger (class in [Link]), 37
lookahead() (in module [Link]), 117
lookup() ([Link] method), 31
Index
Index
149
[Link] (module), 59
[Link] (module), 60
[Link] (module), 65
[Link] (module), 76
[Link] (module), 76
[Link] (module), 77
[Link] (module), 107
[Link] (module), 108
[Link] (module), 112
[Link] (module), 114
[Link] (module), 124
[Link] (module), 126
[Link] (module), 129
[Link] (module), 130
[Link] (module), 136
[Link] (module), 138
[Link] (module), 139
PwnlibException, 35
150
Index
T
tabulate() (in module [Link]), 121
take() (in module [Link]), 122
takewhile() (in module [Link]), 124
tee() (in module [Link]), 124
Index
U
u16() (in module [Link]), 134
u32() (in module [Link]), 134
u64() (in module [Link]), 134
u8() (in module [Link]), 135
unbits() (in module [Link]), 111
unhex command line option
-h, help, 15
hex, 15
unhex() (in module [Link]), 111
unique_everseen() (in module [Link]), 122
unique_justseen() (in module [Link]), 123
unique_window() (in module [Link]), 123
unordlist() (in module [Link]), 126
unpack() (in module [Link]), 135
unpack_many() (in module [Link]), 135
unrecv() ([Link] method), 76
unregister() (in module [Link]), 20
unregister() (in module [Link]), 20
unresolve() ([Link] method), 45
update() ([Link] method), 28
upload_data() ([Link] method), 64
upload_dir() ([Link] method), 64
upload_file() ([Link] method), 64
urldecode() (in module [Link]), 111
urlencode() (in module [Link]), 112
V
vaddr_to_offset() ([Link] method), 34
W
w() ([Link] method), 43
wait_for_close() ([Link] method), 76
wait_for_connection()
([Link]
method), 59
wait_for_debugger() (in module [Link]), 138
warn() ([Link] method), 38
warn_once() ([Link] method), 38
wget() (in module [Link]), 139
which() (in module [Link]), 129
151
X
x_25() (in module [Link]), 106
xfer() (in module [Link]), 106
xmodem() (in module [Link]), 106
xor() (in module [Link]), 112
xor_pair() (in module [Link]), 112
152
Index