The goal of this tutorial is to provide a comprehensive understanding of penetration testing for application security. By the end of this tutorial, you will gain knowledge on how to identify and exploit vulnerabilities in a web application.
Basic understanding of web application architectures and protocols is beneficial. Experience with a programming language like Python or JavaScript is helpful but not mandatory.
Penetration testing, also known as 'pentesting' or 'ethical hacking', is a process meant to probe and exploit vulnerabilities in a system, such as a web application, to assess its security.
Planning and Reconnaissance: This involves defining the scope and goals of the test and gathering intelligence (like network and domain names, mail servers) to better understand how the target system works and its potential vulnerabilities.
Scanning: This step involves using applications like Nessus, Nmap, or Wireshark to understand how the target application will respond to various intrusion attempts.
Gaining Access: This step involves web application attacks like Cross-Site Scripting (XSS), SQL Injection, and Backdoor attacks to uncover a system's vulnerabilities. The goal is to exploit these vulnerabilities to extract valuable data.
Maintaining Access: The goal of this step is to see if the vulnerability can be used to achieve a persistent presence in the exploited system—long enough for a bad actor to gain in-depth access.
Analysis: The results are then compiled into a report detailing:
What vulnerabilities were discovered
Note: The code examples here use Python, a common language for penetration testing due to its easy-to-read syntax and wide range of libraries.
import socket
ip = '127.0.0.1' # replace with your target IP
ports = [21, 22, 80, 443] # common ports for FTP, SSH, HTTP, HTTPS
for port in ports:
try:
service = socket.getservbyport(port)
print(f"Port: {port} runs {service}")
except:
print(f"Port: {port} is not recognized")
This script tries to identify the services running on the specified ports of the target IP.
requests
library to test for a basic SQL Injection vulnerabilityimport requests
url = 'http://target-url.com/login' # replace with your target URL
data = {'username': 'admin', 'password': "' OR '1'='1"}
response = requests.post(url, data=data)
if 'logged in' in response.text:
print('SQL Injection vulnerability detected')
else:
print('No SQL Injection vulnerability detected')
This script injects a common SQL Injection payload into the password field. If the server processes it and logs in, it's vulnerable to SQL Injection.
Through this tutorial, you learned about penetration testing, its steps, and practiced examples of conducting a basic port scan and testing for SQL Injection vulnerability. The next step is to continue learning about and practicing different types of application attacks.
Write a Python script to perform a full port scan (ports 1 to 65535) on your local machine. (Hint: Use a loop)
Try to exploit an XSS vulnerability on a test site like Google's XSS Game.
Write a Python script to check if a website is vulnerable to a directory traversal attack.
Remember, ethical hacking means you have permission to attack the system.
Happy learning and remember to hack ethically!