Python For Sec Introduction
Python For Sec Introduction
The Python language has many advantages when it comes to scripting. The power of
python can be felt when you start working with and try new things with it. It has modules
which can be used to create scripts to automate stuff, play with files and folders, Image
processing, controlling keyboard and mouse, web scraping, regex parsing etc.
For those of you who are familiar with Kali Linux, many scripts are written in Python.
There are many freeware tools which are there in the market which can get the job
done, the why script it with Python? The answer to the question is simple. For those
who have written the tools have a superset of requirements, they want to cover all the
scenarios and add customisations to the tools. This ends up in tools getting
complicated and cumbersome. Moreover, every time we do not have the feasibility to
use tools and hence scripting comes handy. We can script tasks as per our need and
requirement set.
For security professionals, Python can be used for but not limited to:
• Penetration testing
• Information gathering
• Scripting tools
• Automating stuff
• Forensics
I will be discussing a few examples where Python can be used along with the code
and comments. I have tried to heavily comment the code so that it becomes easy to
understand and digest. The approach which I have taken is to break the requirement
into small steps and generate a flow control for how the code should go.
NOTE: I assume that the user is having basic knowledge of Python-like syntax, data
types, flow control, loops, functions, sockets, etc. since the article will not be
discussing the basics and will be drifting away from the conventional “hello world”
approach.
#Output
‘harpreet\\py_scripts’
#Code
Import os
os.getcwd()
#Output
‘C:\\Users\\harpreetsingh\\Desktop’
# Steps involved:
• Open
• read /write
• Close the file
# Output
Module name: Webbrowser
This module is used to open link in the browser. We will be using the open function of
this module to open links in the browser in the below examples. It can take two optional
parameters ‘–n’ to open URL in a new window or ‘-t’ to open URL in new tab.
It will open the URL in the browser. In no time it will fire up the browser and open the
link.
Open cmd
Python
>>>import webbrowser
>>>help(webbrowser)
# Output
Module name: Sys
With this module, command line arguments can be passed to the Python scripts
# Output
For further reading about sys module
Open cmd
Python
>>>import sys
>>>help(sys)
# Output
Open cmd
Python
>>>import urllib2
>>>help(urllib2)
# Output
Module Name: Socket
This module is used when we need to mix Python with networking. It can be used to
create socket connections (TCP/UDP), binding the sockets to the host and port,
closing the connection, promiscuous mode configurations and much more.
Open cmd
Python
>>>import socket
>>>help(socket)
# Output
Ctypes:
It is a means of using C (low-level language) code within Python scripts. It will be used
to decode the IP header in one of the below examples.
Data type – CTYPES Date type – Python
C_bool <Boolean> Bool
C_char One character
C_byte <charcater> Int/long
C_ubyte <unsigned character> Int/long
C_shot <short> Int/long
C_ushort <unsigned short> Int/long
C_int <integer> Int/long
C_uint Int/long
C_long Int/long
C_ulong Int/long
C_float Float
For further reading about ctypes module
Open cmd
Python
>>>import ctypes
>>>help(ctypes)
# Output
IP packet architecture
• Version: IP version used – 4 for IPv4 and 6 for IPv6.
• IHL – IP Header Length: No of 32-bit words forming the header
• Identification: a 16-bit number which is used to identify a packet uniquely.
• Flags: Used to control fragment permissions for that packet.
• TTL (Time to live): No of hops for which the packet may be routed. This number
will get decremented by one each time the packet is routed through hops. This is used
to avoid routing loops.
• Protocol: This field helps us to identify the type of packet
• 1 ICMP
• 6 TCP
• 17 UDP
• Header Checksum: Used for error detection which might have been introduced
during transit.
• Source address: Source address from where the packet has originated
• Destination address: Address for which the packet is destined for.
The below-discussed code will take two inputs – URL/IP address and the directory
list which you would like to test. It will test for the existence of the directories and will
open the URI in the browser if it exists.
Code Flow
Code and comments
/jmx-console
/images
/audio
/php-my-admin
/tag/sniffers/
Save the file dir.txt to a location and copy the location address (Replace the ‘\’ in the
address with ‘\\’). Replace the address in the first line of the first for loop with this
address.
Copy and paste the below code in an IDE and save it(dirb.py)
Command to run the code: python dirb.py (URL)
Example: python dirb.py sectools.org
“””
# Import required packages
import os,webbrowser,sys,urllib.request,urllib.error,urllib.parse
# Print the input of the user on the screen
print(“The URL/IP entered is “)
print(str(sys.argv[1]))
print (“\n”)
url=str(sys.argv[1])
files=[‘dir.txt’]
url_list=[]
for f in files:
hellow=open(os.path.join(‘C:\\Users\\harpreetsingh\\Desktop\\py_scripts’, f))
directories = hellow.readlines()
# iterate through the directory list and create a list with directories appended to the
IP/URL and save it
for i in directories:
i=i.strip()
url_list.append(‘http://’+url+i)
# Iterate through the items from the newly created list and check the response code
# Incase the response code is 200, open the link in the browser
for url in url_list:
print(url)
try:
connection = urllib.request.urlopen(url)
print(connection.getcode())
connection.close()
if connection.getcode() == 200:
webbrowser.open(url)
except urllib.error.HTTPError as e:
print(e.getcode())
# Output
# Response codes on the output screen
Part2
This is the second article on “Python for Security Professionals, ”
In this part we are going to discuss the below using python:
• Port scanning
• Parsing text files for regex
• Creating a reverse TCP shell
Followed by a scenario discussion and code enhancement. So, let’s start.
When we have a network, checking every host manually for collecting information
will not be a viable option, in such cases, network scans can make this task easy
and fast.
What is regex?
>>help(re) #code
>>Import subprocess
>>help(subprocess)
Let’s code stuff
The code discussed below will scan a host and port range to check for the status of
the port. The post can then be separately analyzed for the services running and
vulnerabilities. We will be performing a TCP connect scan (Actively connecting to a
port) for doing this.
NOTE:
TCP connect scan can be easily detected and blocked since we are trying to create a
connection. Limit the no of ports to be scanned to avoid detection.
Code and comments
# The user can enter garbage value or fatfinger the hostname while typing, check if
this gets resolved for a valid IP address
try:
IP=socket.gethostbyname(host)
except:
print(“%s –> Oops! Entered host cannot be resolved to an IP address” %host)
exit()
# The ports entered by the user needs to be converted to an integer for feeding this
to the range function else this will give an error.
int_first_port=int(first_port)
int_last_port=int(last_port)
# Just print the information just in case the user wants to save the result to a text file
print(“\n”)
print(“The host address to be scanned is: %s” %host)
print(“The IP address to be scanned is: %s” %IP)
print(“The port range to be scanned is: %d to %d” %(int_first_port,int_last_port))
print(“================== SCANNING ===================”)
# Loop through the port range and check if we are able to create a successful
connection
for port in range(int_first_port,int_last_port+1):
try:
connect.connect((IP,port))
print(“%s Port open” %port)
connect.close()
• Take multiple hostnames from a text file and perform the scan. The user can start the
scan and perform some other work or take a break for coffee.
• Introduce timestamps: It will be useful if the timestamp of when the scan started and
ended. This can also be used to calculate the duration of the scan, and this can be used
as a parameter to speed up our tool as well.
• Introduce threading: You will realize that the scan is fast when we have a small range
of ports, but this becomes slow when the range is large. Make the code threaded so
that the speed increase since multiple threads will be running in parallel. (HINT:
Threading module in python will be of help)
Regex
# The use is asked to enter the path of the file from which the raw data is to be
searched for regex.
file_path=input(“Enter the file path which contains the raw data”)
buffer = “Read buffer:\n”
# The below line is just to make sure that the data is getting stored in the buffer, can
be commented as well
print(“===================== BELOW DATA IS PRESENT IN THE FILE
========================”)
print(buffer)
print(“===================== PARSED OUTPUT
BELOW========================”)
# Compile function from re module can be used to provide the regex pattern we are
searching for.
# Below regex pattern is for searching the phone number; this can be changed as
per your need.
a = re.compile(‘\d{3}-\d{8}’)
# Findall function from the re module can be used to find the pattern in the data. The
data which has been found will be stored in the form of a list and can be printed
using a loop.
find = re.findall(a,buffer)
for i in find:
print(i)
Data present in the text file is below:
The code can be of help when we have raw logs from various security tools. Let us
consider the case that your organization has been flooded with spam emails and the
only thing you have now id the dump of logs from the mail server. Will you be reading
that to parse the critical data (may be source IP address of the attacker) or will you
prefer to spend some time on the raw logs and identify the regex of what you are
looking for? The choice is yours, but in the interest of time, scripting will be helpful.