codec
– Data transformation¶
This module contains functions that allow you to manipulate, encode or decode strings and byte sequences.
-
pwnypack.codec.
xor
(key, data)[source]¶ Perform cyclical exclusive or operations on
data
.The
key
can be a an integer (0 <= key < 256) or a byte sequence. If the key is smaller than the provideddata
, thekey
will be repeated.Parameters: - key (int or bytes) – The key to xor
data
with. - data (bytes) – The data to perform the xor operation on.
Returns: The result of the exclusive or operation.
Return type: bytes
Examples
>>> from pwny import * >>> xor(5, b'ABCD') b'DGFA' >>> xor(5, b'DGFA') b'ABCD' >>> xor(b'pwny', b'ABCDEFGHIJKLMNOPQRSTUVWXYZ') b'15-=51)19=%5=9!)!%=-%!9!)-' >>> xor(b'pwny', b'15-=51)19=%5=9!)!%=-%!9!)-') b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- key (int or bytes) – The key to xor
-
pwnypack.codec.
find_xor_mask
(data, alphabet=None, max_depth=3, min_depth=0, iv=None)[source]¶ Produce a series of bytestrings that when XORed together end up being equal to
data
and only contain characters from the givingalphabet
. The initial state (or previous state) can be given asiv
.Parameters: - data (bytes) – The data to recreate as a series of XOR operations.
- alphabet (bytes) – The bytestring containing the allowed characters
for the XOR values. If
None
, all characters except NUL bytes, carriage returns and newlines will be allowed. - max_depth (int) – The maximum depth to look for a solution.
- min_depth (int) – The minimum depth to look for a solution.
- iv (bytes) – Initialization vector. If
None
, it will be assumed the operation starts at an all zero string.
Returns: A list of bytestrings that, when XOR’ed with
iv
(or just eachother ifiv` is not providede) will be the same as ``data
.Examples
Produce a series of strings that when XORed together will result in the string ‘pwnypack’ using only ASCII characters in the range 65 to 96:
>>> from pwny import * >>> find_xor_mask('pwnypack', alphabet=''.join(chr(c) for c in range(65, 97))) [b'````````', b'AAAAABAA', b'QVOXQCBJ'] >>> xor(xor(b'````````', b'AAAAABAA'), b'QVOXQCBJ') 'pwnypack'
-
pwnypack.codec.
rot13
(d)¶ Rotate all characters in the alphabets A-Z and a-z by 13 positions in the alphabet. This is a
caesar()
shift of 13 along the fixed alphabetsA-Z
anda-z
.Parameters: d (str) – The string to the apply the cipher to. Returns: The string with the rot13 cipher applied. Return type: str Examples
>>> rot13('whax') 'junk' >>> rot13('junk') 'whax'
-
pwnypack.codec.
caesar
(shift, data, shift_ranges=('az', 'AZ'))[source]¶ Apply a caesar cipher to a string.
The caesar cipher is a substition cipher where each letter in the given alphabet is replaced by a letter some fixed number down the alphabet.
If
shift
is1
, A will become B, B will become C, etc…You can define the alphabets that will be shift by specifying one or more shift ranges. The characters will than be shifted within the given ranges.
Parameters: - shift (int) – The shift to apply.
- data (str) – The string to apply the cipher to.
- shift_ranges (list of str) – Which alphabets to shift.
Returns: The string with the caesar cipher applied.
Return type: str
Examples
>>> caesar(16, 'Pwnypack') 'Fmdofqsa' >>> caesar(-16, 'Fmdofqsa') 'Pwnypack' >>> caesar(16, 'PWNYpack', shift_ranges=('AZ',)) 'FMDOpack' >>> caesar(16, 'PWNYpack', shift_ranges=('Az',)) '`g^iFqsA'
-
pwnypack.codec.
enhex
(d, separator='')[source]¶ Convert bytes to their hexadecimal representation, optionally joined by a given separator.
Parameters: - d (bytes) – The data to convert to hexadecimal representation.
- separator (str) – The separator to insert between hexadecimal tuples.
Returns: The hexadecimal representation of
d
.Return type: str
Examples
>>> from pwny import * >>> enhex(b'pwnypack') '70776e797061636b' >>> enhex(b'pwnypack', separator=' ') '70 77 6e 79 70 61 63 6b'
-
pwnypack.codec.
dehex
(d)¶ Convert a hexadecimal representation of a byte sequence to bytes. All non-hexadecimal characters will be removed from the input.
Parameters: d (str) – The string of hexadecimal tuples. Returns: The byte sequence represented by d
.Return type: bytes Examples
>>> from pwny import * >>> dehex('70776e797061636b') b'pwnypack' >>> dhex('70 77 6e 79 70 61 63 6b') b'pwnypack'
-
pwnypack.codec.
enb64
(d)¶ Convert bytes to their base64 representation.
Parameters: d (bytes) – The data to convert to its base64 representation. Returns: The base64 representation of d
.Return type: str Example
>>> from pwny import * >>> enb64(b'pwnypack') 'cHdueXBhY2s='
-
pwnypack.codec.
deb64
(d)¶ Convert a base64 representation back to its original bytes.
Parameters: d (str) – The base64 representation to decode. Returns: The bytes represented by d
.Return type: bytes Example
>>> from pwny import * >>> deb64('cHdueXBhY2s=') b'pwnypack'
-
pwnypack.codec.
enurlform
(q)[source]¶ Convert a dictionary to a URL encoded query string.
Parameters: q (dict) – The query to encode. Returns: The urlencoded representation of q
.Return type: str Example
>>> from pwny import * >>> enurlform({'foo': 'bar', 'baz': ['quux', 'corge']}) 'foo=bar&baz=quux&baz=corge'
-
pwnypack.codec.
deurlform
(d)[source]¶ Convert a URL encoded query string to a dictionary.
Parameters: d (str) – The URL encoded query string. Returns: A dictionary containing each key and all its values as a list. Return type: dict Example
>>> from pwny import * >>> deurlform('foo=bar&baz=quux&baz=corge') {'foo': ['bar'], 'baz': ['quux', 'corge']}
-
pwnypack.codec.
enurlquote
(v, plus=False)[source]¶ Percent encode a string for use in an URL.
Parameters: - v (str) – The value to percent encode.
- plus (bool) – Use a plus symbol for spaces, otherwise use %20.
Returns: The percent encoded string.
Return type: str
Example
>>> from pwny import * >>> enurlquote('Foo Bar/Baz', True) 'Foo+Bar/Baz
-
pwnypack.codec.
deurlquote
(d, plus=False)[source]¶ Decode a percent encoded string.
Parameters: - d (str) – The percent encoded value to decode.
- plus (bool) – Parse a plus symbol as a space.
Returns: The decoded version of the percent encoded of
d
.Return type: str
Example
>>> from pwny import * >>> deurlquote('Foo+Bar/Baz') 'Foo Bar/Baz'
-
pwnypack.codec.
frequency
(v)¶ Perform a frequency analysis on a byte sequence or string.
Parameters: d (bytes or str) – The sequence to analyse. Returns: A dictionary of unique elements in d
and how often the occur.Return type: dict Example
>>> frequency('pwnypack') {'a': 1, 'c': 1, 'k': 1, 'n': 1, 'p': 2, 'w': 1, 'y': 1}