Python is an interpreted, open source programming language that is simple, powerful, and preinstalled on many systems. It has less syntax than other languages and a plethora of penetration testing tools have already been created in Python. Python code is translated and executed by an interpreter one statement at a time, allowing it to be run from the command prompt, through command prompt files, or in an integrated development environment. The language uses whitespace and comments to make code more readable. It can perform basic operations like printing, taking user input, performing conditionals and loops, defining reusable functions, and importing additional modules.
2. Why Python?
Simple
Powerful
Usually preinstalled
Less Syntax
Open Source
Plethora of PenTestTools already created
3. Interpreted?
• Python is interpreted, which means that python code is translated
and executed by an interpreter one statement at a time.
• This means you can run commands/code on the command
prompt…
• Through a command
prompt with a text file…
• Using an IDE
5. Good programming proclamations
Using comments to denote programs or sections of code
◦ A comment beginning with # is called a single-line comment because it
terminates at the end of the current line.
◦ You also may use a multi-line comment—possibly containing many
lines—begins with ’’’ and ends with ’’’.
You use blank lines, space characters and tab characters (i.e., “tabs”)
to make programs easier to read.
◦ Together, these characters are known as white space.
◦ White-space characters are USUALLY ignored by the interpreter.
◦ Python has its own rules with white space
6. Printing a Line of Text
• Code: print(“ “) Python 3
• Code: print “ “ Python 2
• Anything placed inside the quotes will be printed on the screen upon execution
of the print command.
8. Obtaining input
How to input values from the user
› Code: variable = input(“Prompt “) Python 3
› Code: variable = raw_input(“Prompt”) Python 2
› variable – stores the input from the user.
› input – function to extract user input from the command prompt.
› “Prompt” – a string to inform the user of the type of value to enter.
Ex. Inputting a numerical value
testGrade1 = input(”Enter test grade 1: “)
testGrade *= 100
print(”Test Grade: “, testGrade, “%”)
Good Programming:Organize outputs, good prompts
9. DataType Conversion
• Can control how variables are interpreted within the
program:
• Evaluation
• Code: eval(string)
• Ex. eval(“51” + “52”)
• eval( str1 ** str2)
• test = eval(input(“Enter a num”))
10. Conditional Operators
• Operator Meaning
• == equal to
• < less than
• <= less than or equals to
• > greater than
• >= greater than or equal to
• != not equal to
• Common Mistake:Confusing = with ==
11. Decision Structures
• Code:
Creating a decision structure:
if (expression):
statement
• To execute more than one statement in a block they must be indented equally:
if (score > 90):
grade = 'A'
print(“Good Job!n”)
print(“Not part of the conditions execution”)
• Good Programming: Commenting conditional blocks
• Common Mistakes: Forgetting equal indent, forgetting :, and forgetting space after if
• Good Practices: Use a tab not a space (harder to line up and troubleshoot)
Space Indent
12. Nested Conditionals
if (condition):
Statement
elif (condition):
Statement
elif (condition):
Statement
Statement
else:
Statement
• Common Errors: Not Lining up Else with its preceding If.
• Question:Why is a trailing else good programming?
13. Nested if/else if Example
Question: What zodiac sign is it for the current year?
14. Logical Operators
Code:
• Using logical operators:
• if (condition or condition)
Statement
• if (condition and condition)
Statement
15. While Loops
• AWhile Loop is a loop that executes 0 or more times before
terminating.
• Pre-conditional loop
Code:
Creating aWhile Loop:
while (condition statement):
statement1
statement2
DebuggingTechniques:
Setup a counter to keep track of the number of times a loop runs
Set up a counter to count if an event is occurring or the number of
times it occurs
Output values each time a loop executes
16. Example
num = eval(input(“Enter a number less than 10:”))
while (num >= 10):
print(“Invalid Entry!”)
num = eval(input(“Enter a number less than 10:”))
17. For Loops
• For loops are a pre-test loop
• In order to utilize a for loop you need 3 things:
1. Needs to initialize a counter
2. Must test the counter variable (less than)
3. It must update the counter variable
• Code:
for initialization in range(start, stop, increment):
statement1
statement2
18. Example
for i in range(0, 5, 1):
print(“Hello”)
Step 1: Perform the initialization
expression
Step 2: Evaluate the test expressions
Step 3: Execute the body of the
loop
Step 4: Perform the
update
Assign 0 to i
i < 5
Update iPrint “Hello”
True
False
19. Function Definition
• Definition includes:
• return value: the value the function returns to the part of the
program that called it
• name: name of the function. Function names follow same rules
as variables
• parameter list: variables containing
values passed to the function
• body: statements that perform
the function’s task
20. Calling a Function
• Functions just like variables need to be called in order
to be invoked
• Code:
Calling a Function:
functionName (Parameters)
22. Modules
• Some special functions have not been innately included into the interpreter to
speed up the loading process, these packages are known as modules.
• Code: import moduleName
• Ex.
• import math
• import random
• Import os
• OS module
• os.getcwd()
• os.chdir(path)
Notes de l'éditeur
Read Introduction from Violent Python pg.1 -2
Perl is complex and has too many idiosyncrasies even though many prebuilt tools are designed in perl so modification is huge
Java and C are system languages and need to be comipled
Python is a general purpose programming language. That means you can use Python to write code for any programming tasks.
Google search engine
Mission critical projects in NASA,
Processing financial transactions at New York Stock Exchange.
Data Analytics
Common elements in programming languages:
Key Words
Programmer-Defined Identifiers
Operators
Punctuation
Syntax
Loop Directories???
Additionally, new functionality is added to the language all the time as it is created.