Python Analyst Reference
Python Analyst Reference
Python Analyst
Reference
Version 20190124
Python Analyst Reference
Version 20190124
Contents
Credits and Formatting Notes .................................................................................................................. 2
Python Variable Types ............................................................................................................................. 2
Mathematical and Logic Operators .......................................................................................................... 3
Python 2/3 Compatibility Imports ............................................................................................................ 5
Format Strings ......................................................................................................................................... 5
String .format() Method .......................................................................................................................... 6
Built-In format() function......................................................................................................................... 6
Python ’s byte strings vs. Python UTF-8 strings ................................................................................... 7
String Methods ........................................................................................................................................ 8
Slicing Strings .......................................................................................................................................... 9
codecs Module ........................................................................................................................................ 9
Lists ....................................................................................................................................................... 10
List Comprehension ............................................................................................................................... 11
Lambda functions .................................................................................................................................. 12
for and while Loops ............................................................................................................................... 13
Tuples ................................................................................................................................................... 13
Dictionaries ........................................................................................................................................... 14
Python Debugger................................................................................................................................... 16
Ternary Operator .................................................................................................................................. 16
File Operations ...................................................................................................................................... 17
The os Module for File Operations ......................................................................................................... 18
Python’s gzip and zlib Modules .............................................................................................................. 18
Regular Expressions............................................................................................................................... 19
Sets ....................................................................................................................................................... 21
Scapy..................................................................................................................................................... 22
struct Module........................................................................................................................................ 23
PIL Module ............................................................................................................................................ 24
sqlite3 Module ...................................................................................................................................... 25
python-registry Module......................................................................................................................... 26
Generators ............................................................................................................................................ 27
requests Module ................................................................................................................................... 27
socket Module....................................................................................................................................... 28
try/except/else/finally Blocks ................................................................................................................ 29
subprocess Module ............................................................................................................................... 30
select Module........................................................................................................................................ 30
Page 1 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Throughout this document, examples will be provided using a Python interactive shell. These examples
will begin with the Python prompt >>> and the output will follow on the next line or in some cases to the
right on the same line. Code examples are listed in italics. Items indicating the type of information
expected in a command are listed between <> such as print(<thing to print>).
Page 2 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
In Python 2, if either dividend or divisor are floats, the result is a float, but if both are integers, the result
will be an integer. To produce an integer result, Python 2 does a floor operation. In Python 3, even if
both divisor and dividend are integers, the division result will be a float. To import Python 3 division into
Python 2 use from __future__ import division
Floats are an approximation, so you need to specify the precision when doing comparisons: Examples:
False
Since the precision is not specified in the example above, Python attempts to carry the precision out to
many decimal places, which at some point become non-zero since floats are an approximation. This
makes the comparison not yield the expected result. Use format or round to define the precision:
True
True
True
Page 3 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Logical Operators
== Equal
!= Not Equal
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
and And
or Or
^ XOR
False and True are keywords and should not be quoted like strings. False, None, 0 and Empty values are
False. Everything else is True.
>>> bool("False") is True since it is a string with a non-empty value, not the unquoted keyword False.
Python evaluates only enough of an expression to return a value with the same Boolean value as the
whole expression. This is called shortcut processing, and behaves as follows:
OR expression, return 1st item if it is True, else return 2nd item (you can prove an OR in one item)
AND expression, return 1st item if it is False, else return 2nd item (you can disprove an AND in one item)
Page 4 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
from __future__ import print_function (In Python 2, print was a built-in keyword, but in Python 3 it is a
function. Since the keyword is not supported in Python 3, always use the function which must be
imported into Python 2)
try:
input = raw_input
except:
pass
Python 2 has two functions that can accept input, input() and raw_input(). raw_input() always returns a
string and is the safer one to use. input() in Python 2 evaluates the input before returning it, which can
allow string injection attacks. In Python 3, the input() function is actually the same as raw_input() in
Python 2 and Python 3 does not have the dangerous input() function at all.
Format Strings
(“Some String”) (variable1, variable2)
String Meaning
%d Integer decimal
%10d Integer decimal, 10 digits wide
%010d Integer decimal, 10 digits wide, leading zeros
%x Hex in lowercase
%X Hex in uppercase
%f Floating-point decimal (around six characters
after the decimal by default)
%6.2f Floating-point decimal, 6 wide (in total) with 2
after decimal. Note that the decimal point counts
as one of the 6 characters, so it will be ###.## for
a total of 6 characters, 2 after the decimal point.
The result is rounded, not truncated.
%s String
%% Escapes the percent sign to print a single %
Examples:
newstring (“I’m d sure”) (100) produces “I’m 100 sure” and assigns it to newstring
Page 5 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Alignment options:
Length
Can be a single number or include a decimal point when a float is used to show the number of
places after the decimal point (rounded, not truncated).
Example print(“There are {0: .2f} percent”.format( .3 )) produces There are 44.37 percent
Type Options
Examples:
'aaaa22 16'
Example:
>>> format(10.3,"0>8.2f")
'00010.30'
Page 6 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Examples:
In Python 3:
>>> b'ABC'.decode()
'ABC'
>>> "ABC".encode()
b'ABC'
also note:
>>> b'ABC'.encode()
In Python 2.7
>>> "test".encode()
'test'
>>> "test".decode()
u'test'
>>> "test"
'test'
Page 7 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
String Methods
If x “pyWars rocks!”
Note that Title Case capitalizes the first letter of each word, regardless of length or significance.
Remember, strings are immutable, so these methods create a new string rather than modifying the
original.
Note that the substring and main string must both be encoded in the same way (bytes or UTF-8).
Python 3 example:
>>> a.replace('t','*')
But note that if the substring type is bytes when the Python 3 string is UTF-8:
>>> a.replace(b'e','*')
Page 8 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Slicing Strings
String[start (beginning at zero):end (up to but not including):step]
If x “Python rocks”
Expression Result
x[0] P
x[2] t
x[0:3] or x[:3] Pyt
x[0:-1] or x[:-1] Python rock
x[3:] hon rocks
x[0::2] or x[::2] Pto ok
x[::-1] Skcor nohtyP
x[-1]+x[4]+x[7]*2+x[1] sorry
x[:6][::-1] or x[5::-1] nohtyP
codecs Module
To use the codecs module, you must first: import codecs
Common Codecs
Codec Description
bz2 Bzip2 encoding/decoding, requires bytes-like object
rot13 Rotates letters 13 ASCII places
base64 Base64 encodes/decodes bytes-like object (not UTF-8 strings)
zip Creates a compressed Zip version, requires bytes-like object
hex Produces a byte string of hex characters from a bytes-like object
utf-16 2-byte (16-bit) Unicode
Examples if x “Python rocks” and using Python 2 (note: Python 3 strings are not bytes-like objects):
>>> codecs.encode(x,"rot13")
'Clguba ebpxf'
>>> codecs.encode(x,"utf-16")
b'\xff\xfeP\x00y\x00t\x00h\x00o\x00n\x00 \x00r\x00o\x00c\x00k\x00s\x00'
>>> codecs.encode(x,"base64")
'UHl0aG9uIHJvY2tz\n'
Page 9 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Lists
List Method Description
.append(value) Add an object to end of the list
.insert(position, value) Insert the value at the given position, other items will shift right, position
is a positive or negative number. The change is made to the list, so the
function just returns None.
.remove(value) Removes the first matching item by its value. The change is made to the
list, so the function just returns None.
.sort(key …, reverse …) Sort the elements of the list by changing the actual list’s order. Can
provide a key function to use for the sort. Both key and reverse are
optional (so .sort(reverse=True) would sort the list backwards according
to the default sort key (which is ASCII values). The change is made to the
list, so the function just returns None.
.count(value) Count number of occurrences of an item in the list. Entire list entry must
match the provided value (does not look for substrings within items).
.index(value) Look up where a value is in the list
.reverse() Reorders the list, in reverse order. Changes the list itself, does not just
display it backwards. The change is made to the list, so the function just
returns None.
In addition to the list methods above, there are other useful functions that work on lists:
Function Description
del list[index] Delete an item by index number (del is a keyword, not a list method)
Sorted([]) Function that will display the list items in a sorted order but not change
the list itself. Accepts an optional key=function() to produce an element
on which to sort, e.g. sorted(customer_list, key=lowercase) You can
optionally pass reverse=True as an argument to reverse the sort.
‘a’ in list Looks for any items that match ‘a’ in the list and return True if present or
false if not. Value being searched must match the whole list item, does
not search substrings.
sum([]) Provides to sum of a list that contains only numbers (int or float).
Traceback if contains strings, tuples, other lists, etc.
zip([],[]) Creates a new list of tuples from two or more lists. The first item from
each list are placed in a tuple at in dex 0, the second items from each list
are placed in a tuple at index 1, and so on. Stops once one list is
exhausted.
map(function,[]) Run the specified function on each item in a list (or any iterable). Note
that you use the name of the function without the parentheses. If two
lists are provided, the specified function becomes a custom zipper. The
result of map is a map object. You can use list(map(function,[])) to have
it return a list with the results.
enumerate([]) Returns an enumerate object of tuples consisting of each list’s items
index and value. An example of use is for index,value in
enumerate(some_liist) where each index would correspond to its
associated value as the list is walked. To create a list of tuples with index
and value use list(enumerate(some_list))
Page 10 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
The default action when you copy a list is that it creates a pointer to the list rather than recreates a new
list. So if a list called list1 exists, list2 = list1 would make list2 a pointer to the same list, and changes to
list1 would also result in changes to list2.
To copy the list items into a new list, use list2 =list(list1)
To copy a list of lists, use the deepcopy() function from the copy module:
c = a + b would set c to a new list [1, 2, 3, 4] and changes to a or b would not affect c.
and c[1:3] would be [2, 3] (same rules apply as with strings, with negative numbers and stepping)
List Comprehension
Newlist = [<expression> for <iterator> in <list> <filter>]
Example:
Note that in Python 2, the variables declared inside a list comprehension do not get their own scope, so
if there is a local, global or built-in variable with the same name it will overwrite its values. In Python 3,
the variable declared in a list comprehension gets its own scope.
Python 2 example:
>>> x = 3
>>> newlist
[0, 1, 2, 3]
>>> x
If there is not a variable with that name already declared, it declares it with a persistent scope.
>>> newlist
Page 11 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
[0, 1, 2, 3]
>>> var
Python 3 example:
But with Python , the declared variable’s scope is just the list comprehension:
>>> newlist
[0, 1, 2, 3]
>>> var
Lambda functions
A small function, often used as a key or with map().
Syntax is
Examples:
Page 12 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
for x in range(100):
for x in range(<start>, <stop>, <step>): (starts at start, stop is up to but not including the number
provided, step is positive or negative as with slicing)
while loops
while loops can go on forever, whereas for loops have a defined end.
while loops can have an else statement, that occurs only once when the test condition evaluates
to False. If a break ends the loops, the else is not performed.
Tuples
Tuples are immutable
Can be declared with parentheses around the list, or just as comma separate values without
parentheses.
Can access individual elements by their index, e.g. tuple[2]
Page 13 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Dictionaries
Dictionary method Description
.get(key, <value if not found>) You provide the key, and it returns the value. Optionally, you can
also provide a value that will be returned if the key specified is not
found. You can also request a value by the key as if it were a list, i.e.
dict[key] but if the key does not exist, this method causes a
Traceback. Get() on the other hand just returns None if the key does
not exist and no optional value to return is provided.
.copy() Like lists, assigning a dictionary to a variable creates a link to the
dictionary, not a new dictionary. To create a copy, you can cast the
dictionary to a dict() like with lists, e.g. b = dict(a) or you can call the
.copy() method, e.g. b = a.copy()
.keys() Returns a list (or view in Python 3) of the keys (ordered based on the
memory location of the key prior to Python 3.6 and in the order you
put them in in 3.6 and later)
.values() Returns a list (or view in Python 3) of the values (ordered based on
the memory location of the key prior to Python 3.6 and in the order
you put them in in 3.6 and later)
.items() Returns a list (or view in Python 3) of the tuples containing
(key,value). Items are ordered based on the memory location of the
key prior to Python 3.6 and in the order you put them in in 3.6 and
later.
In Python 3, .keys(), .values() and .items() do not return lists but instead return a View object pointing to
the dictionary. A view object is iterable but it cannot be sliced or use any list methods. A view is more
like a pointer in that if you assign a view to a variable, the elements of the dictionary will update as the
dictionary updates.
<key> in dict syntax will search through the keys of the dictionary for a key and return True if
present or False if not.
<value> in dict.values() will do the same for values.
for x in dict iterates through the keys (the same as for x in dict.keys() would)
There is no efficient way to look up a key based on the value, but looking up a value based on
key is very fast.
dict[<key>] = <value> syntax will add a new key, value pair (overwriting the old value at that key
if it previously existed)
Page 14 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Specialized Dictionaries
Special Dictionaries in collections Module Description
defaultdict(default_function) A dictionary that will create any key that you
query and set it to the value returned by the
default_function. You can access dict[key] safely
for any value since nonexistent keys
automatically call the default_function to have a
value set.
Counter Counter automatically counts the number of
times a key is set. It is a customized defaultdict,
similar to defaultdict(lambda:0) but it also adds
additional methods.
.most_common() counter method lists the keys
with the greatest count first (takes an optional
value to set the number of keys to display or else
it displays all in frequency order).
.update([]) method takes a list of keys and
increments the count for each
.subtract([]) method takes a list of keys and
decrements the count for each
Page 15 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Python Debugger
Three ways to start:
Ternary Operator
Provides a shortcut way to do a conditional assignment. Example:
x = 10 if y==5 else 11
Page 16 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
File Operations
Create a file object with the built-in open() function using:
OR
open() can optionally take an encoding= argument if a specific encoding is used in the file (or if you want
to read binary data as a string and encode it as Latin1 to avoid corruption). Example:
Where the path to the file is absolute or relative, and the mode is one of the following:
Method Description
.seek() Sets the file pointer position
.tell() Returns the file pointer’s current position
.read() Read the contents of a file as a string
.readlines() Read the contents of a file as a list of lines
.readline() Reads the next line of the file as a string
.write() Writes a string to a file
.writelines() Iterates over an object that produces strings and writes each to a file.
.close() Closes the file
Page 17 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
filehandle open(‘filename’,’r’)
for oneline in filehandle:
print(oneline, end ””)
filehandle.close()
>>> import os
>>> os.path.exists("/bin/bash")
True
Note that False will be returned if either the file does not exist or the process is running as a user that
does not have access to the file.
You can list the contents of a single directory (not recursively) with os.listdir(“path/to/dir”)
You can use os.walk(“starting path”) to recursively list files. Each iteration returns a tuple with
three elements:
o A string containing the current directory
o A list of the directories in that directory
o A list of the files in that directory
Example: for currentdir, list_of_dirs, list_of_files in os.walk(“/”): #Some code block
gzip.open() will open compressed files. In Python , it defaults to ‘rb’ mode but log files should be
opened in text mode (‘rt’). gzip objects support the .read(), .readlines(), .write(), .writelines(), .seek(),
and .tell() methods the same as the built-in open() function does. Example: the following will read the
first 40 characters of the gzipped log file:
Page 18 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Regular Expressions
Python’s re module implements regular expressions. It contains the following functions:
Function Description
match() Start at the beginning of data searching for pattern. Returns an object
that stores the data. The object returned supports the .group() method
to access capture group data. .group() returns the whole result,
regardless of defined capture groups. .group(1) returns just the first
capture group (numbering starts at 1). .group(“name”) will return a
capture group with explicit group name of “name”).
search() Match pattern anywhere in the data. Returns an object that stores the
data. The object returned supports the .group() method to access capture
group data. .group() returns the whole result, regardless of defined
capture groups. .group(1) returns just the first capture group (numbering
starts at 1). .group(“name”) will return a capture group with explicit
group name of “name”).
findall(’ expression’, Find all occurrences of the expression in the data. Returns a list containing
data,[optional_modifier]) each match as an item. The expression can be a regular string, a raw
string (denoted by an r before the string) or a byte string (denoted by a b
before the string). If desired, a third argument can be provided to modify
the behavior. Examples:
re.IGNORECASE will make the search case insensitive. Can alternatively
just add (?i) to the beginning of the regular expression string.
re.MULTILINE will make ^ and $ anchors apply to each new line character,
not just the first line. Can alternatively just add (?m) to the beginning of
the regular expression string.
Re.DOTALL will make . match newlines also, since normally the . does not
match newline character. This should always be used for searches within
binary data. Can alternatively just add (?s) to the beginning of the regular
expression string.
Character Meaning
. Wildcard for any character
? Previous character is optional
+ One of more of the previous character (greedy, match as much as
possible)
+? One or more of the previous character (stop as soon as the match is
made)
* Zero or more of the previous character (greedy, match as much as
possible)
*? Zero or more of the previous character (stop as soon as the match is
made)
{x] Match exactly x copies of the previous character
{x:y} Match between x and y copies of the previous character
Page 19 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Character Meaning
\w Any text character (a-z, A-Z, 0-9, and _)
\W Opposite of \w (only non-text characters)
\d Matches digits (0-9)
\D Opposite of \d
\s Matches any white-space character (space, tab, newlines)
\S Opposite of \s
[set of chars] Define your own set of characters to match against one character. If
the character is in the set you define, it matches.
[^set of char] Caret in first position negates the set, so matches anything except
was is listed.
\b Border of a word character, the transition of \w to a \W or vice
versa.
^ Match must be at the start of the string
$ Match must be at the end of the string
\ Escpapes special characters (\. means to search for a literal period)
| A logical OR, Example a|b matches on an a or b
(<expression>) Enclosing an expression in parentheses makes it a capture group.
Only items inside a capture group are returned, but the rest of the
expression must also match (it simply is not returned in the result).
Capture groups are numbered beginning a 1, not zero.
(?P<groupname><expression>) Creates a capture group with an explicit name, rather than the
automatic numbering. Not that in this case, the first set of brackets
is a literal syntactic requirement, example:
>>> a = re.search('(?P<test1>a..)de',"abcdefgabc")
>>> a.group('test1')
'abc'
(?:<expression>) If ?: is placed within a parenthetical group, it indicates that the
grouping is for ordering only and is not a capture group. Example: to
capture string dates from 0 to 1, use ‘(?:0[1-9]|[1-2][0-9]|3[0-1])’
(?i) Make expression case insensitive.
(?s) Make . match newlines also, since normally the . does not match
newline character (always use for searching within binary data)
(?m) Make ^ and $ anchors apply to each new line character, not just the
first line (use for Multiline searches)
(?P<groupname>) Makes a back reference to the capture group named groupname
\<number> Makes a back reference to the capture group numbered <number>
Since regular expressions are also python strings, the \ character is interpreted both by the
Python string and by the regular expression, since it has spelling meaning for both. To indicate
that a string is “raw” and should not process the string with the Python string engine by putting
r at the beginning of the string. You can also do a b in front of the string to make it a byte string.
Finally, in Python 3 only, you can do rb at the beginning of the string to denote a raw, byte
string.
Page 20 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Sets
Sets are like lists, but each element is unique. Sets contain immutable objects, so they cannot contain
lists or dictionaries. They are denoted with {} like dictionaries, so {1:2} is a dictionary and {1,2} is a set.
Operator Behavior
^ Symmetric_differnce
& Intersection
| Union
- Difference
Like lists and dictionaries, assigning a set to a variable creates a link to the original set, not a new set. To
copy a set, use the set() function:
b = set(a)
Page 21 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Scapy
To import scapy, use
Scapy Functions
Function Description
rdpcap(filename) Reads a pcap file into a scapy.plist.PacketList data structure
wrpcap(filename, packetlist) Writes a PacketList to a file
sniff() Used to capture packets and return a PacketList object
sniff(offline ”file.pcap”) Reads a pcap and returns a PaketList object
sniff(prn=function_name) prn= argument specifies a callback function that is called for
each packet returned by sniff. An example is lfilter, which
provides the ability to filter packets based on specified criteria
(sniff has a filter ”BPF” argument as well but it has many OS
dependencies that are often not met).
Method Description
.sessions() Follows TCP streams, produces a dictionary with a key of “protocol
srcip:srcport dstip:dstport” and a value that is a scapy.plist.PacketList
with all the associated packets in it.
PacketList objects are lists of packets. Packet objects have the .haslayer(<layer>) method, which returns
True if that layer is present in that packet. Layer names are case sensitive and include Ether, IP, TCP,
UDP, DNS, and Raw. Layer names are passed not in quotes, e.g. packet_one.haslayer(TCP)
Each layer has fields and the fields are addressed with a dot notation, for example
packet_one[TCP].sport for the Source Port in the TCP layer. You can view the fields in any layer with
ls(<layer>). If a field name is unique, you can skip stating the layer and access just the field name, such
as packet_one.load instead of packet_one[Raw].load since the field “load” only exists at the Raw layer. If
the field name is not unique, scapy will return the first field it encounters with that name. Each packet
also has a .time attribute that records the epoch time when the packet was captured.
Page 22 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
struct Module
struct.unpack(<pattern_string>,<byte_data>) converts byte data into other types
the <pattern_string> describes how to pack or unpack the data, as shown in this chart:
Examples:
>>> struct.unpack("BB",b"\xff\x00")
(255, 0)
Page 23 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
PIL Module
Originally called PIL (Python Image Library) but now called pillow; however, the package name pil is still
used to avoid backward compatibility issues.
imagedata=image.open(“picture.jpg”)
imagedata.show()
To read a picture carved out of a data stream, use BytesIO in Python 3 or StreamIO in Python 2
Python 3 Example:
Image.open(BytesIO(img)).show()
Then use TAGS.get(<integer>) to look up an EXIF tag number and get its string meaning back. Can
specify a second, optional argument to TAGS.get() that provides a string to return if the key is not found
in the dictionary.
If you point a new variable to a variable holding an Image, it creates a pointer to the original, not a new
object (like lists). To create a new image, use:
copy = Image.Image.copy(original)
Page 24 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
PIL.Image methods
Method Description
Image.open(<filename>) Open a file and create an Image object (not a method of an
Image object, but used to instantiate an ImageObject)
.show() Use default viewe to display the image
.thumbnail((Width,Height),Method) Reduces the size of the image to a maximum size specified by
the tuple in the first argument, using the method specified in
the next argument. Methods include Image.NEAREST,
Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS and others.
Preserves the image aspect ratio. Modifies the original image,
does not produce a new copy.
.resize((Width,Height),Method) Enlarge or reduce the size of image to the size provided in the
tuple supplied as first argument, ignoring original aspect ratio.
Does not modify original image, but returns a copy of it.
.size (attribute, not a method) A tuple that provides the size of the image (width,height)
.crop((left,upper,right,lower)) Returns a cropped copy of the image. The argument is a tuple
defining the area to be cropped.
.rotate(degrees) Returns a copy of the image rotated the specified number of
degrees, does not alter original image.
.save() Saves the image to disk
_getexif() Returns a dictionary describing the metadata about the image.
The keys are integers designating the tag type as per the EXIF
standard. Values are the associated data for that tag.
sqlite3 Module
To import this module, use: import sqlite3
db sqlite3.connect(“filename”)
You can then make SQL queries to the database with the .execute() method as seen here:
.execute() method returns an iterable object that can be converted into a list to view all contents if
desired or iterated with a for loop.
Page 25 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
python-registry Module
pip install python-registry
Key Methods
reg_key reg_hive.open(“Microsoft\Windows\CurrentVersion”)
Format
REG_BINARY Eight values of 2-bytes each, representing year,
month, day (a number representing the day of
the week starting with Sunday), date, hr, min,
sec, microsecond.
REG_DWORD Linux timestamp integer recording the number of
seconds since Epoch.
Page 26 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
Generators
Placing a yield statement in a function makes it a generator, which pauses its execution, returns a value,
and awaits a .__next__() call to resume execution and return the value indicated by the next yield
statement.
requests Module
Import with: import requests
webdata requests.get(“http://www.sans.org”)
Or post requests:
formdata {‘username’:’admin’,’password’:’ninja’}
Both of these return a response object. Can access several different attributes of response objects:
Alternatively, you can create a session, which is like creating a browser that remembers setting, such as
User-Agent, and maintains state via cookies
browser = requests.session()
browser.headers attribute displays a dictionary with the various header options like Accept-Encoding,
User-Agent, etc. These can be changed as desired by simply changing this dictionary.
You can then call browser.get(<url>) and browser.post(<url>,<postdata>)) to make requests from the
customized browser object. The responses will still be request objects just as they were when using
requests.get(<url>) and requests.post(<url>,<postdata>)
You can also use browser.cookies attribute to view the request.cookies.RequestCookieJar object, which
is a special type of dictionary. Calling browser.cookies.keys() will provide a list of the cookies. You can
use browser.cookies[<cookie_name] to view the value of a cookie.
Page 27 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
socket Module
To import, use: import socket
>>> socket.gethostbyname("www.sans.org")
'45.60.35.34'
>>> socket.gethostbyaddr("8.8.8.8")
(the above result is a tuple with hostname, list of aliases, and a list of addresses)
Example:
After creating the socket object (TCP or UDP), if the object will be a server, bind it to a port with:
udp_socket.bind((“10.10.10.10”, 000))
(the argument is a tuple with a string for the IP and an integer for the port)
To send and receive data to a UDP socket, use .sendto() and .recvfrom() methods
https://www.twitter.com/threathunting_
Page 28 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
tcp_socket.bind((<ip>,<port>))
connection,remote = tcp_socket.accept()
.accept() will return a connection object and a tuple with the remote IP and port.
From that point, you can interact through the connection object with .send(), .recv() and .close()
try/except/else/finally Blocks
try:
#block to try
except <specific_error_name>:
#block for that error
except Exception as e:
#block that can include the name of the exception as the variable e
else:
#block to do if there is no exception
finally:
#block to do at the end whether there was an exception or not (usually for clean up)
while True:
try:
#something to try
except:
continue
else:
break
https://www.twitter.com/threathunting_
Page 29 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.
Python Analyst Reference
Version 20190124
subprocess Module
Can be used to start a new process, provide it input and capture the output:
Can use processhandle.wait() to cause your program to pause until the subprocess completes. It returns
an integer exit code to show the status once the process terminates. However, if the subprocess
generates a lot of output, the output buffer may fill and cause a hang.
Instead, you can use processhandle.communicate() which will read the subprocess.PIPE repeatedly until
the subprocess is finished executing. It then returns a tuple with two, separate byte strings. The first
contains all the stdout and the second contains all the stderr from the subprocess.
select Module
select.select([list_of_sockets], [list_of_sockets], [list_of_sockets])
The sockets in the list are each checked. The first list is checked to see if the sockets have data ready for
you to receive. The second list is checked to see if they are ready for you to send data. The third list
checks to see if they are in an error condition.
https://www.twitter.com/threathunting_
Page 30 of 30
Extracted from Mark Baggett’s Automating Information Security with Python course.