Python programming
It is really useful for a pentester to know about python and be able to script something. Being able to automate things can definitely help us go quicker in our daily practice. Of course we do not need to be a developer but it is very helpful to be able to read code.
notes from my practice, TCM Academy and DeepLearning.AI
shebang
For python our script need to have this shebang in the first line
#!/bin/python3this will allow us to launch the script using./ myscript.pyinstead ofpython3 myscript.py
Strings
Print a string
#!/bin/python3
print("Hello world!") # double quotes
print('Hello world!') # single quotes
print("""This string runs
multiple lines""") # triple quotes for multiple lines
print("This string is "+"awesome") # concatenationUsing f-Strings
f-Strings is useful to mix strings with computations or data
# Will print The temperature 75F in degrees celsius is 23.88888888888889C
print(f"The temperature 75F in degrees celsius is {(75 - 32) * 5 / 9}C")
# It works also with multiligne strings
print(f"""
Most countries use the metric system for recipe measurement,
but American bakers use a different system. For example, they use
fluid ounces to measure liquids instead of milliliters (ml).
So you need to convert recipe units to your local measuring system!
For example, 8 fluid ounces of milk is {8 * 29.5735} ml.
And 100ml of water is {100 / 29.5735} fluid ounces.
""")
# This will print
'''
Most countries use the metric system for recipe measurement,
but American bakers use a different system. For example, they use
fluid ounces to measure liquids instead of milliliters (ml).
So you need to convert recipe units to your local measuring system!
For example, 8 fluid ounces of milk is 236.588 ml.
And 100ml of water is 3.381405650328842 fluid ounces.
'''
# It also works on variables
my_name = "gabrielle"
print(f"Hello {my_name}!")
# this will print Hello gabrielle!
# And we can mix var and computation as follow
fav_num = 8
print(f"Your favorite number plus 10 is {fav_num+10}")
# this will print Your favorite number plus 10 is 18type()
In Python, you can check the type of any data that you are using. To check the data type, you can use the type() function.
Math
The order of operations in Python is the same as in arithmetic. First, you compute parentheses, then exponents, then you multiply and divide (from left to right), and finally, you add and subtract (from left to right).
Variables and Methods
Functions
Boolean Expressions
Relational and Boolean operators
Conditional Statements
Lists (mutable)
Declare a list
Tuples (immutable)
It is immutable meaning we can not use pop or append on them once it is declared it can not be modified.
Looping
For loops
Start to finish of an iterate
While loops
Execute as long as true
Importing modules
Advanced strings
Dictionaries
Key value pairs A Dictionnary is a data structure that helps you store key-value pairs. The main difference with list, is that dictionaries assign a key to each values instead of an index.
Sys
Reading and writing files
Sockets
We use socket to connect to an open port and an IP addess
To test our script we can launch netcat
nc -nlvp 7777
Building a port scanner
We can use socket to build a port scanner
This script is just and exercise (done in the PEH TCM course) so it definitely could be improved:
Handle other type of errors
...
Virtual Environments
We might sometimes need to use a version for a script and another version for another Create and isolate python virtual environments not dependant With virtualenv we can have multiple version of a package installed and usable in a system at the same time pip install virtualenv We can then create a folder and launch our virtual env in it:
We then have to activate it:
We can see that our env is launched
![]()
By default our virtual env does not contains our previously installed python packages or any modules We can check which virtualenv is being called with which python3:

On our host (outside of our virtualenv) the same command gives this:

And in our virtual environment we can install anything we need We can have multiple virtual environments running at the same time. One we finished with the virtualenv we can type deactivate
Using functions from a local file
We can create a file called helper_functions.py
We can then import our file in our other python script by using import.
Built-in packages
The math package
The statistics package
The random package
The pandas package
import pandas as pd creates a shortcut that you can use to avoid typing pandas.
Package matplotlib
Package Beautiful soup
Beautiful soup is a python package to interpret HTML webpages inside python programs
Use APIs
Example with a weather API
Resources
Last updated
