Koding Books

Professional, free coding tutorials

White hat hacking using Python

While waiting for a flight to Dubai, watching people use the terminal network to access the internet got me thinking about information security.

All those people use apps to access their bank accounts, send confidential information to their employers, shop on Amazon, etc. The security needs to be tight.

Before I go any further, the information contained within this article is for intellectual research, furthering Python skills and general security knowledge.

Please do not use the information in this article to access network computers you are not authorised to use. If you do, it’s on you.

Introduction

Python is a popular programming language for developing penetration testing tools due to its ease of use and versatility. Penetration testing, or pen testing, is testing a computer system, network, or web application to identify vulnerabilities that an attacker could exploit. Penetration testing tools developed in Python can automate the process of identifying and exploiting vulnerabilities, making it easier for security professionals to test the security of their systems.

White hat hacking, also known as ethical hacking, uses hacking techniques to identify and fix security vulnerabilities in computer systems, networks, and web applications. White hat hackers are typically security professionals hired by organizations to test the security of their systems and identify potential vulnerabilities that malicious hackers could exploit. White hat hacking is legal and ethical and is an important part of maintaining the security of computer systems and networks.

Existing Tools

My A-level computing teacher always said, don’t reinvent the wheel; use what’s already out there.

There are several popular Python libraries for penetration testing. Here are a few:

  1. Scapy: a powerful packet manipulation library that allows you to create, send, and capture network packets.
  2. Metasploit Framework: a popular open-source framework for developing and executing exploits against remote targets.
  3. Nmap: a network exploration and security auditing tool that can discover hosts and services on a computer network.
  4. Requests: a library for making HTTP requests in Python, which can be useful for testing web applications.
  5. Paramiko: a library for implementing SSH protocols in Python, which can be useful for testing the security of SSH servers.
  6. PyCrypto: a collection of cryptographic algorithms and protocols which can be useful for testing the security of encryption and decryption algorithms.

These are just a few examples; many more libraries are available for Python penetration testing, depending on your specific needs.

Tools

Many Python penetration testing tools can be developed using the libraries I mentioned earlier. Here are a few examples:

  1. Network scanner: using the Scapy library, you can develop a tool that scans a network for hosts and services and identifies potential vulnerabilities.
  2. Exploit development: using the Metasploit Framework, you can develop exploits for known vulnerabilities in software and test them against remote targets.
  3. Web application scanner: using the Requests library, you can develop a tool that scans a web application for vulnerabilities such as SQL injection and cross-site scripting (XSS).
  4. SSH brute-forcer: using the Paramiko library, you can develop a tool that attempts to brute-force SSH login credentials on a remote server.
  5. Encryption cracker: using the PyCrypto library, you can develop a tool that attempts to crack encrypted data using various cryptographic algorithms.

These are just a few examples; many more Python penetration testing tools can be developed using these libraries, depending on your specific needs.

Best Practices

Some best practices for developing Python penetration testing tools:

  1. Use version control: Use a version control system like Git to keep track of changes to your code and collaborate with others.
  2. Follow coding standards: Follow coding standards like PEP 8 to ensure your code is readable and maintainable.
  3. Use error handling: Use error handling to catch and handle exceptions in your code, and provide meaningful error messages to users.
  4. Document your code: Document your code using comments and docstrings to make it easier for others to understand and use your code.
  5. Test your code: Write unit tests to ensure your code is working as expected, and use integration tests to test the interaction between different components of your tool.
  6. Use encryption: Use encryption to protect sensitive data like login credentials and other confidential information.
  7. Keep your tools up-to-date: Keep your tools up-to-date with the latest security patches and updates to ensure they are effective against the latest threats.

Web penetration tools

There are many Python web application penetration testing tools available. Here are a few examples:

  1. OWASP ZAP: a popular open-source web application security scanner that can be used to identify vulnerabilities in web applications.
  2. Burp Suite: a popular commercial web application security scanner that can be used to identify vulnerabilities in web applications.
  3. sqlmap: a popular open-source tool for detecting and exploiting SQL injection vulnerabilities in web applications.
  4. Wfuzz: a tool for brute-forcing web applications, which can be used to test the security of login forms and other input fields.
  5. DirBuster: a tool for brute-forcing directories and files on web servers, which can be used to identify hidden files and directories.
  6. XSSer: a tool for detecting and exploiting cross-site scripting (XSS) vulnerabilities in web applications.

Example | A Network Scanner

Folder structure

app/
---test_network_scanner.py
---network_scanner.py
---requirements.txt

test_network_scanner.py

#!/usr/bin/env python

from scapy.all import *
import pytest

def scan(ip_address):
    # Create an ARP request packet
    arp = ARP(pdst=ip_address)

    # Create an Ethernet frame
    eth = Ether(dst="ff:ff:ff:ff:ff:ff")

    # Combine the ARP request packet and Ethernet frame
    packet = eth/arp

    # Send the packet and capture the response
    result = srp(packet, timeout=3, verbose=0)[0]

    # Print the results
    print("IP\t\tMAC Address\n-----------------------------------------")

    for sent, received in result:
        print(received.psrc + "\t" + received.hwsrc)

def test_scan():
    # Test scanning a single host
    scan("192.168.1.1")

    # Test scanning a range of hosts
    scan("192.168.1.1/24")

    # Test scanning an invalid IP address
    with pytest.raises(Exception):
        scan("invalid_ip_address")

if __name__ == "__main__":
    pytest.main([__file__])

network_scanner.py

#!/usr/bin/env python

from scapy.all import *

def scan(ip_address):
    # Create an ARP request packet
    arp = ARP(pdst=ip_address)

    # Create an Ethernet frame
    eth = Ether(dst="ff:ff:ff:ff:ff:ff")

    # Combine the ARP request packet and Ethernet frame
    packet = eth/arp

    # Send the packet and capture the response
    result = srp(packet, timeout=3, verbose=0)[0]

    # Print the results
    print("IP\t\tMAC Address\n-----------------------------------------")

    for sent, received in result:
        print(received.psrc + "\t" + received.hwsrc)

def main():
    # Get the IP address to scan from the user
    ip_address = input("Enter an IP address to scan: ")

    # Call the scan function with the user-provided IP address
    scan(ip_address)

if __name__ == "__main__":
    main()

requirements.txt

scapy
pytest

pip install -r requirements.txt

Run the command above to install the requirements before running the program with the following command

python network_scanner.py

Wallah, we have built a network scanner together.

The last byte…

  • Python is a popular programming language for developing penetration testing tools due to its ease of use and versatility.
  • Popular Python libraries for penetration testing include Scapy, Metasploit Framework, Nmap, Requests, Paramiko, and PyCrypto.
  • Python penetration testing tools can be used to automate the process of identifying and exploiting vulnerabilities in computer systems, networks, and web applications.
  • Best practices for developing Python penetration testing tools include using version control, following coding standards, error handling, documenting your code, testing your code, using encryption, and keeping your tools up-to-date.
  • Python web application penetration testing tools include OWASP ZAP, Burp Suite, sqlmap, Wfuzz, DirBuster, and XSSer.
  • We provided an example of a Python program that uses the Scapy library to scan a network for hosts with a specified IP address, and showed how to write unit tests for the program using the pytest framework.
  • We also showed how to use a requirements.txt file to install the dependencies for the program, and how to run the program from the command line.

With great power comes great responsibility.

Ali Kayani

https://www.linkedin.com/in/ali-kayani-silvercoder007/

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

How E-Trading is Revolutionising the Fixed Income Market

CMAKE – A practical introduction

Dual Thrust Trading Algorithm

Combining Mean Reversion And Momentum In The FOREX Market.