230 Programming in Python
230 Programming in Python
230 Programming in Python
S e c t i o n 0 2 | M o d u l e 0 3
© Caendra Inc. 2019
All Rights Reserved
Table of Contents
You can find all the Python code samples used on the Resources drop-down menu of this
module.
What is Python?
python -V
Why Interactive?
Using the interactive shell, we can see errors while we write our
code. In the below program we are trying to print a variable that
does not exist (f), and of course, the interpreter returns an error.
IMPORTANT NOTE!
Python differs from many other programming languages
because it uses whitespace and indentation to determine block
structures. In other words, Python specifies that several
statements are part of a single group by indenting them.
Curly Brackets
delimit the block Indentation delimits the
block
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.22
3.1 What is Python
Outside while
Within while
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.24
3.2
x = 10
y = “Hello”
x = 10
Here we see perfectly legal Python
y = “Hello”
code that creates a variable named ‘x’
and assigns the value 10 to that variable. The second
statement creates a new variable y and assigns the string
“Hello”.
We do not need to
declare the type of the
variable.
Input / Output
Let’s now look at how we can get user input and work with
it.
We know how to print output, but how can we get input from the
user?
Where:
• user_input is the variable that will contain the user value
• Message is the text that will be displayed to the user right before
his input
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.39
3.3 Input / Output
The above code gets the user name and surname, and then
it prints out a welcome message.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.40
3.3 Input / Output
Control Flow
IMPORTANT NOTE!
Operator
< Less than
<= Less than or equal
== Equal
The following table > Greater than
summarizes the comparison >= Greater than or equal
if expression:
statement
else:
statement
if expression:
statement The else clause is optional. If
else: expression evaluates to true, the
statement
statement or block that forms
the target of if is executed; otherwise, the statement or
block that is the target of else will be executed.
executed.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.50
3.4 Control Flow
IMPORTANT NOTE!
Another loop statement is the for loop. Its general form is:
In other words, the body of the for loop will be executed for
each element in the sequence.
Note: In this example the list function is used to print all the elements within the range. We will see it later on.
Lists
simple_list = [1,2,3,4,5]
list = [1,2,“els”,4,5,‘something’,[0,9]]
IMPORTANT NOTE!
simple_list = [“first”,2,“els”,4]
In almost every 0 1 2 3
programming
language, indices Index Element value
The remove method is quite different from the others. It does not
work with indices; instead, it looks for a given value within the list,
and if this exists, it removes the element. Note that only the first
instance of that value is removed.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.71
3.5 Lists
Dictionaries
Where the element on the left of the of the colon is the key,
and the element on the right is its associated value. As
much as lists, dictionaries can store objects of any type and
values are not implicitly ordered.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.75
3.6 Dictionaries
Create a dictionary
Unlike lists, if the key does not exist, a new key:value pair is added at the beginning of the
dictionary
Create a dictionary
Functions
Where:
• def indicates a function definition
• function_name is the identifier of the function
• parameters is a comma-separated list of variables
• function_statements is the body of the function
• return exits a function and gives the execution back to the caller
Python functions body must be indented in order to delimit the start and the end of the function itself.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.82
3.7 Functions
Call function
my_sum and
store the value
returned in x
Global x.
Global x
Global
scope
The previous example is also useful to explain the variable scope. As you can see,
two variables x are used, but they have different values depending on their scope.
The first x is local to the function and can be used only within my_sum. Each change
made to this variable has no effect outside the function. The second x is global and
can be used in the entire program (within the single file).
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.86
3.7 Functions
function_switch[user](x) is then
function_switch[1](5)
function_switch[1] is a then
a(5) is called
Modules
Let’s see then how to create a new module and how we can
use it.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.94
3.8 Modules
First, we need to create a new file and insert our code into
it. Let’s suppose we want to create a function that returns
the double of a number. Once we have our code, save the
file into the Python directory and name it “my_double.py”.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.95
3.8 Modules
Now we can run a new shell and import our module. To do it, let’s type the keyword
import followed by the name of our file (my_double). Once we import the module, if
no errors or warnings are raised, we can use objects defined in it by typing the
module name and the object name separated by a dot (my_double.some_variable).
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.96
3.8 Modules
Scripting for
Pentesters
SERVER
CLIENT
Solution!
Please continue only if you have solved the exercise.
If you want to see what our Python code does while you
program, please consider running Wireshark in the
background. It can be a great way to ensure everything is
working properly.
Sockets: http://docs.Python.org/3/library/socket.html
OS: http://docs.Python.org/3.3/library/os.html
Platform: http://docs.Python.org/3/library/platform.html
Solution!
Please continue only if you have solved the exercise.
On the left, is the portion of the client code that starts the connection to
the server backdoor. On the right, are the operations that we can send
to the server.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.123
3.9.3. Backdoor
The next program we are going to see will make use of the
module HTTP.client. For more information, here is the link
to the documentation:
http://docs.Python.org/3/library/http.client.html
Solution!
Please continue only if you have solved the exercise.
Solution!
Please continue only if you have solved the exercise.
Python-assisted
exploitation
Try to write your own
python tools in order to
speed up target
exploration.
*Labs are only available in Full or Elite Editions of the course. To upgrade, click HERE. To access, go to the course in
your members area and click the labs drop-down in the appropriate module line or to the virtual labs tabs on the
left navigation.
PTSv4: Section 2, Module 3 - Caendra Inc. © 2019 | p.136
References
Python
http://www.Python.org/getit
*Labs are only available in Full or Elite Editions of the course. To upgrade, click HERE. To access, go to the course in your members area and
click the labs drop-down in the appropriate module line or to the virtual labs tabs on the left navigation.