Python for
CyberSecurity
< Automate as much as you can >
{
}
...
Table of contents
01
02
03
Introduciton to Python
Python Fundmentals
WorkShop
Whoa!
< Ahmed Elkhressy – Security Consultant
aelkhressy@mcsholding.com >
}
Intro to Python
01 {
} ..
..
History of Pyhton
Python was created by Guido van
Rossum in the late 1980s and released
in 1991, t was designed to be a simple,
readable, and versatile programming
language
} ..
Key Feature of Python
1) Open-source and free.
2) Cross-platform (Windows, macOS, Linux, Raspberry Pi)
3) Extensive standard library.
} ..
Python Vs C
} ..
Aspect Python C
Type High-level, interpreted
programming language.
Low-level, procedural
programming language.
Syntax Simple and easy to read, with
minimal boilerplate.
Strict and verbose, requiring
detailed code.
Execution Interpreted language (executed
via an interpreter).
Compiled language (requires a
compiler like GCC).
Speed Slow Faster
Application of Python
● Web Development: Django, Flask
● Data Science:Pandas, NumPy, Matplotlib
● Cybersecurity: Scripting, automation, and
penetration testing
*
Let’s Start…
Print(“Hello World”)
VSCode/CMD/terminal
{
}
Python Fundmentals
02 {
} ..
..
Variables
In Python you don’t need to declare variables,
but you can specify data type to variable via
casting
X = 10
Y = “khressy”
Print(X)
Print(y)
Sample
...
Data Types
x = 10
x = 10.5
x = "Hello World"
Integer
Float
str
...
Data Types
x = [“BMW", “Audi", “Ford"]
x = (“BMW", “Audi", “Ford")
x = range(6)
List
tuple
range
...
Operators: Arithmetic
+ Addition
- subtraction
* Multiplcation
/ Division
% Modulus
** Exponentiation
// Floor Division
...
Operators: Logical
And &&
Or ||
Not !
...
List
Collection of item can be edited (order and changeable)
x = [“BMW", “Audi", “Ford"]
print(x)
Print(x[1])
...
x = [“BMW", “Audi", “Ford"]
X[0] = “Honda”
x = [“BMW", “Audi", “Ford"]
x.append(“Honda”)
x.remove(“Audi”)
tuple
Collection of immutable items can be (order and unchangeable)
x = (“BMW", “Audi", “Ford“)
print(x)
Print(x[1])
...
x = [“BMW", “Audi", “Ford"]
y = list(x)
y.remove(“BMW”)
x = tuple(y)
Conditions: if statement
...
If condition
If condition
else
If condition
Elif condition
else
If a > B
Print(“a is bigger”)
Loops: while
...
while condition
while condition
break
while condition
continue
i = 1
while i < 6:
print(i)
i += 1
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Loops: for
...
condition
for x in range(10):
Print x
Input
...
how to take input from user
name = input(“name”)
Print(name)
Practical exercise 01
Write a python program that Write a program to check if
a number is even or odd.
Input: 15
Expected output: odd
Practical exercise 02
Write a python to Count the Number of Vowels in a
String
Input: Hello, how are you?
Expected output: 7
Practical exercise 03
Write a python to manage a shopping list Allow the user
to:
- add items to the list.
- remove an item by its name.
- Displays all items in the list.
- Clears the list using
Practical exercise 04
Write a python program that check if a number is prime.
Input: 23
Expected output: prime
Practical exercise 05
Write a python program print triangle
Input: 5
Expected output:
*
**
***
****
*****
Error Handling: try except
...
try:
print(x)
except:
print("Variable x is not defined")
Function
...
Def my_function():
Print(“hello world”)
Myfucntion()
Library
...
There are a huge number of libraries
Such as
os
re
Socket
math
Library: how to call
...
Import os  call
From math import sqrt  call fucn
From os import *  similar to 1
Import numpy as np  alias
create file
...
import os
file_path = r”path”
with open(file_path, 'w') as file:
file.write("This is a new file created using the os module and Python.")
create file
...
import os
directory_path = r"D:XpandCSpyhtonnew_directory"
os.mkdir(directory_path)
Practical exercise 06
Write a python program that create a file, directory
Practical exercise 07
Write a python program that can open facebook from
your browser
Workshop
03
< Lets fun… >
{
} ..
..
Password Checker
Using function to Write a Python program check if the
password is strong or not,
Strong password contain:
1) At least 8 character
2) At least one lowercase char
3) At least one uppercase char
4) At least one digit
5) At least one special char
Library: re
Scanner
Write a Python program that scans IPs addresses and
identifies open ports if the IP is online.
Socket
1- how communication happen over network?
2- what we need to start communication?
IP scanner
How to create IP scanner?
'ping {ip} -n 1 -w 1 > nul 2>&1’
Condition: response != 0  ofline
Port scanner
How to create port scanner?!!
Port scanner
1) Make function to scan one port
2) Make function to scan
Scan: scanme.nmap.org
Condition: response == 0  port is open
Malware
Write a Python program to encrypt a file.
Library: cryptography
Command: pip install cryptography

_python___language______programming.pptx