{"slug": "ai-penetration-tester-code-behind-it", "title": "AI Penetration Tester (Code Behind It)", "summary": "A developer shared code for an AI penetration tester that uses a neural network to prioritize network targets for vulnerability scanning. The tool combines Scapy for network discovery, requests for web app checks, and a PyTorch model to predict likely vulnerable hosts.", "body_md": "| python | |\n| # ai_pen_tester.py | |\n| import torch | |\n| import torch.nn as nn | |\n| import scapy.all as scapy | |\n| import requests | |\n| import nmap | |\n| # Define the AI model architecture | |\n| class PenTestModel(nn.Module): | |\n| def __init__(self): | |\n| super(PenTestModel, self).__init__() | |\n| self.fc1 = nn.Linear(128, 128) # input layer (128) -> hidden layer (128) | |\n| self.fc2 = nn.Linear(128, 128) # hidden layer (128) -> hidden layer (128) | |\n| self.fc3 = nn.Linear(128, 2) # hidden layer (128) -> output layer (2) | |\n| def forward(self, x): | |\n| x = torch.relu(self.fc1(x)) | |\n| x = torch.relu(self.fc2(x)) | |\n| x = self.fc3(x) | |\n| return x | |\n| # Define the penetration testing functions | |\n| class PenTester: | |\n| def __init__(self, model): | |\n| self.model = model | |\n| def scan_network(self, ip_range): | |\n| # Use scapy to perform a basic network scan | |\n| arp_request = scapy.ARP(pdst=ip_range) | |\n| broadcast = scapy.Ether(dst=\"ff:ff:ff:ff:ff:ff\") | |\n| arp_request_broadcast = broadcast/arp_request | |\n| answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0] | |\n| # Create a list of IP addresses to scan | |\n| ip_addresses = [answered_list[i][1].psrc for i in range(len(answered_list))] | |\n| return ip_addresses | |\n| def scan_web_app(self, ip_address): | |\n| # Use requests to perform a basic web application scan | |\n| url = f\"http://{ip_address}\" | |\n| response = requests.get(url) | |\n| # Check for common web application vulnerabilities | |\n| if response.status_code == 200: | |\n| # Check for SQL injection vulnerabilities | |\n| sql_injection_url = f\"{url}/?id=1' OR '1' = '1\" | |\n| sql_injection_response = requests.get(sql_injection_url) | |\n| if sql_injection_response.status_code == 200: | |\n| return True | |\n| # Check for cross-site scripting (XSS) vulnerabilities | |\n| xss_url = f\"{url}/?name=<script>alert('XSS')</script>\" | |\n| xss_response = requests.get(xss_url) | |\n| if xss_response.status_code == 200: | |\n| return True | |\n| return False | |\n| def run_pen_test(self, ip_range): | |\n| # Scan the network and identify potential targets | |\n| ip_addresses = self.scan_network(ip_range) | |\n| # Use the AI model to predict which targets are most likely to be vulnerable | |\n| predictions = [] | |\n| for ip_address in ip_addresses: | |\n| # Create a feature vector for the target | |\n| features = [1, 0, 1, 0] # placeholder features | |\n| features = torch.tensor(features, dtype=torch.float32) | |\n| # Run the feature vector through the AI model | |\n| output = self.model(features) | |\n| prediction = torch.argmax(output) | |\n| # Add the prediction to the list | |\n| predictions.append((ip_address, prediction)) | |\n| # Sort the predictions by confidence | |\n| predictions.sort(key=lambda x: x[1], reverse=True) | |\n| # Run the penetration test on the top N targets | |\n| for ip_address, _ in predictions[:5]: | |\n| if self.scan_web_app(ip_address): | |\n| print(f\"Vulnerability found on {ip_address}!\") | |\n| # Create an instance of the AI model and the penetration tester | |\n| model = PenTestModel() | |\n| pen_tester = PenTester(model) | |\n| # Run the penetration test | |\n| pen_tester.run_pen_test(\"192.168.1.0/24\") |", "url": "https://wpnews.pro/news/ai-penetration-tester-code-behind-it", "canonical_source": "https://gist.github.com/Prestidigitation-Alias-Mirage/4d556ca0c8755c0b8551ff74c1a30303", "published_at": "2026-07-25 01:00:35+00:00", "updated_at": "2026-07-25 01:27:43.292797+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "ai-tools"], "entities": ["PyTorch", "Scapy", "Nmap"], "alternates": {"html": "https://wpnews.pro/news/ai-penetration-tester-code-behind-it", "markdown": "https://wpnews.pro/news/ai-penetration-tester-code-behind-it.md", "text": "https://wpnews.pro/news/ai-penetration-tester-code-behind-it.txt", "jsonld": "https://wpnews.pro/news/ai-penetration-tester-code-behind-it.jsonld"}}