# AI Penetration Tester (Code Behind It)

> Source: <https://gist.github.com/Prestidigitation-Alias-Mirage/4d556ca0c8755c0b8551ff74c1a30303>
> Published: 2026-07-25 01:00:35+00:00

| python | |
| # ai_pen_tester.py | |
| import torch | |
| import torch.nn as nn | |
| import scapy.all as scapy | |
| import requests | |
| import nmap | |
| # Define the AI model architecture | |
| class PenTestModel(nn.Module): | |
| def __init__(self): | |
| super(PenTestModel, self).__init__() | |
| self.fc1 = nn.Linear(128, 128) # input layer (128) -> hidden layer (128) | |
| self.fc2 = nn.Linear(128, 128) # hidden layer (128) -> hidden layer (128) | |
| self.fc3 = nn.Linear(128, 2) # hidden layer (128) -> output layer (2) | |
| def forward(self, x): | |
| x = torch.relu(self.fc1(x)) | |
| x = torch.relu(self.fc2(x)) | |
| x = self.fc3(x) | |
| return x | |
| # Define the penetration testing functions | |
| class PenTester: | |
| def __init__(self, model): | |
| self.model = model | |
| def scan_network(self, ip_range): | |
| # Use scapy to perform a basic network scan | |
| arp_request = scapy.ARP(pdst=ip_range) | |
| broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff") | |
| arp_request_broadcast = broadcast/arp_request | |
| answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0] | |
| # Create a list of IP addresses to scan | |
| ip_addresses = [answered_list[i][1].psrc for i in range(len(answered_list))] | |
| return ip_addresses | |
| def scan_web_app(self, ip_address): | |
| # Use requests to perform a basic web application scan | |
| url = f"http://{ip_address}" | |
| response = requests.get(url) | |
| # Check for common web application vulnerabilities | |
| if response.status_code == 200: | |
| # Check for SQL injection vulnerabilities | |
| sql_injection_url = f"{url}/?id=1' OR '1' = '1" | |
| sql_injection_response = requests.get(sql_injection_url) | |
| if sql_injection_response.status_code == 200: | |
| return True | |
| # Check for cross-site scripting (XSS) vulnerabilities | |
| xss_url = f"{url}/?name=<script>alert('XSS')</script>" | |
| xss_response = requests.get(xss_url) | |
| if xss_response.status_code == 200: | |
| return True | |
| return False | |
| def run_pen_test(self, ip_range): | |
| # Scan the network and identify potential targets | |
| ip_addresses = self.scan_network(ip_range) | |
| # Use the AI model to predict which targets are most likely to be vulnerable | |
| predictions = [] | |
| for ip_address in ip_addresses: | |
| # Create a feature vector for the target | |
| features = [1, 0, 1, 0] # placeholder features | |
| features = torch.tensor(features, dtype=torch.float32) | |
| # Run the feature vector through the AI model | |
| output = self.model(features) | |
| prediction = torch.argmax(output) | |
| # Add the prediction to the list | |
| predictions.append((ip_address, prediction)) | |
| # Sort the predictions by confidence | |
| predictions.sort(key=lambda x: x[1], reverse=True) | |
| # Run the penetration test on the top N targets | |
| for ip_address, _ in predictions[:5]: | |
| if self.scan_web_app(ip_address): | |
| print(f"Vulnerability found on {ip_address}!") | |
| # Create an instance of the AI model and the penetration tester | |
| model = PenTestModel() | |
| pen_tester = PenTester(model) | |
| # Run the penetration test | |
| pen_tester.run_pen_test("192.168.1.0/24") |
