So you want to do Ethical Hacking using Python? This is a great simple intro that discusses general overview and some high level details.
The ability to code, understand programs, and craft a quick script is invaluable. The best way to make custom tools, automate parts of the hacking process, and have capabilities that can’t always be downloaded is by using scripts. In this article we will discuss ethical hacking using python as the primary scripting language of choice.
What is Ethical Hacking?
When you hear hacking the first thing that comes to mind is someone trying to steal your data. Well, hacking has it’s origins in the MIT Model Railroad Club. Hackers were originally hacking high-tech train sets in the 1960’s. They migrated from train sets to computers. Their curiosity and drive ended up sparking a huge wave of hackers and enthusiasts we see in our modern world.
Hacking can create better products than the original it came from, the best example is the UNIX operating system that was hacked and created by Dennis Ritchie and Keith Thompson. The abuse of power, hacking without permission, and committing illegal acts with hacking has given hacking a bad name. Modern black hats , the unethical hackers, are caught and sentenced to punishment.
On the other hand, for every bad guy there will be a good guy to stop them. White hats are the good guys in our good guy versus bad guy equation. Ethical hacking is now a modern skill set like programming, IT, or any technical ability. For instance, ethical hackers are employed by companies to help find and fix security flaws before the bad guys can exploit them for money or personal gain.
What is Python?
Python is an interpreted, object-oriented, general-purpose scripting language. It’s very powerful and has an immense amount of libraries and capabilities available to be. It has uses from Artificial Intelligence to Web Scraping and Hacking somewhere in between. You can use Python to build a website, build a Neural Network, Scrape websites, collect data, analyze data sets, and so much more. Python is also easy to maintain and has a large community behind it.
Furthermore, major companies like Google, Facebook, Netflix, Reddit, and Dropbox use python. It’s a great language to program anything from a quick script to a large full featured application. Python can easily process text, images, data, and can easily spin up a web server. Python is something you will encounter in countless devices and systems you are working with.
Why use Python for Ethical Hacking?
Python has so many uses because of the countless powerful libraries that the community helps create and maintain for it. If you can think of a library it probably exists in python. As well, Python is also very beginner friendly, easy to read, and brings a lot of power in a quick and easy to script package. If you ever want to do something cool besides ethical hacking then learning python enables you to switch gears much easier than many languages.
Moreover, there are many Python libraries that make scripting hacking tools easy and quick. The following are some uses and their respective libraries:
- There are great libraries for Network based tools in Python just to name a few: Pulsar, Twisted, Napalm, and Asyncio.
- The following libraries are nice for Packet manipulation: Scapy and IMPACKET.
- There are also libraries for making your own port mapping tools such as Python-Nmap which is essentially NMAP as a library form and sockets which is a way to access the networking logic.
- There are multiple cryptography libraries for working cryptography tools.
- You can make Web scrapping and data gathering tools with libraries like BeautifulSoup, Requests, and Scrapy.
Building tools and using these libraries mentioned above are just the beginning when using the massive collection of Python libraries.
Simple Example: Port Scanner
So let’s jump into a simple example of a tool you may want to script up. There are great versions of tools like this but let’s say all you have on the machine you’re working on is python.
Here is the scenario:
You’ve succesfully managed to logon to a machine but need to see what other machines may have a port open so you can send a hacking payload. You want to be able to scan a host connection and check if different ports are open. You need to build a quick port scanner because installing a tool may trigger their IDS/IPS or cause an Administrator to start looking at the machine logs to see why a normal user wants to install a port scanning tool.
Here is the code:
import socket scanningSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) targetMachine = input('Select target to scan: ') lowerRange = input('Select lower port range: ') upperRange = input('Select upper port range: ') def portScan(port): try: conn = scanningSocket.connect((targetMachine, port)) return True except: return False for currentPort in range(int(lowerRange), int(upperRange)): print('Trying port',currentPort) if portScan(currentPort): print('[*] TCP Port',currentPort,'open')
So let’s break down this script so you can understand what each part does.
Port Scanner Code Breakdown
We first need to import the library that will allow our program to access the low level socket logic to make connections.
Now that we have the library imported we need to make a local variable that contains a socket object that will be making the connection. For this purpose we want to make an IPV4 TCP connection.
scanningSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
NOTE: AF_INET specifies IPV4 and SOCK_STREAM specifies setup a stream connection which means a TCP connection
Now that we have a socket object to make connection we need to know the target and port ranges to scan.
targetMachine = input('Select target to scan: ') lowerRange = input('Select lower port range: ') upperRange = input('Select upper port range: ')
We now need to define a function that we can call for each port. A function will help us reduce duplicate code as much as possible. We already know the target machine so we just need to know what port the function should be trying to connect to.
We now want to try to connect to the port on the target machine. If it fails there will be an exception throws. If it succeeds then we will have a valid connection.
try: conn = scanningSocket.connect((targetMachine, port)) return True except: return False
We have now defined a
portScan() function we can use to call while we scan our host. We need to iterate over every port from our previously specified lower and upper range.
for currentPort in range(int(lowerRange), int(upperRange)):
Lastly, we need the logic to print out some logs, and check if the connection succeeded or not.
print('Trying port',currentPort) if portScan(currentPort): print('[*] TCP Port',currentPort,'open')
Ethical Hacking Using Python: Conclusion
In conclusion, Ethical Hacking is a very valuable and interesting skill set. Using python to augment that skill set makes any Ethical Hacker a much more valuable and potent Hacker. Welcome to the Cyber Security and Hacking world. Always remember to hack with permission and stay on the good side!