GPT_Vuln-analyzer
This is a Proof Of Concept application that demostrates how AI can be used to generate accurate results for vulnerability analysis and also allows further utilization of the already super useful ChatGPT made using openai-api, python-nmap, dnsresolver python modules and also use customtkinter and tkinter for the GUI version of the code. This project also has a CLI and a GUI interface, It is capable of doing network vulnerability analysis, DNS enumeration and also subdomain enumeration.
Requirements
- Python 3.10
- All the packages mentioned in the requirements.txt file
- OpenAI api
- IPGeolocation API
Usage Package
Import packages
cd package && pip3/pip install .
Simple import any of the 3 packages and then add define the variables accordingly
from GVA import scanner
from GVA import dns_recon
from GVA import subdomain
from GVA import geo
from GVA import gui
from dotenv import load_dotenv()
load_dotenv()
openai_key = os.getenv('OPENAI_API_KEY')
geoIP_key = os.getenv('GEOIP_API_KEY')
sub_domain_list = ['admin', 'whateveryouwant']
# scanner(target: str, profile: int, api_key: str)
# dns_recon(target: str, api_key: str)
# domain(target: str, domain_list: List[str)
# geo(api_key: str, target: str)
print(scanner.scanner('127.0.0.1', 1, openai_key))
print(dns_recon.dns_recon('127.0.0.1', openai_key))
print(subdomain.domain('127.0.0.1', sub_domain_list))
print(geo.geo(geoIP_key, '127.0.0.1'))
gui.application()
Usage CLI
- First Change the "APIKEY__" part of the code with OpenAI api key and the IPGeolocation API key in the
.env
file
GEOIP_API_KEY = ''
OPENAI_API_KEY = ''
- second install the packages
pip3 install -r requirements.txt
or
pip install -r requirements.txt
- run the code python3 gpt_vuln.py
# Regular Help Menu
python gpt_vuln.py --help
# Rich Help Menu
python gpt_vuln.py --r help
# Specify target with the attack
python gpt_vuln.py --target <IP> --attack dns/nmap
# Specify target and profile for nmap
python gpt_vuln.py --target <IP> --attack nmap --profile <1-5>
(Default:1)
# Specify target for DNS no profile needed
python gpt_vuln.py --target <IP or HOSTNAME> --attack dns
# Specify target for Subdomain Enumeration no profile needed
python gpt_vuln.py --target <HOSTNAME> --attack sub
# Specify target for geolocation lookup
python gpt_vuln.py --target <IP> --attack geo
Supported in both windows and linux
Understanding the code
Profiles:
Parameter | Return data | Description | Nmap Command |
---|---|---|---|
p1 |
json |
Effective Scan | -Pn -sV -T4 -O -F |
p2 |
json |
Simple Scan | -Pn -T4 -A -v |
p3 |
json |
Low Power Scan | -Pn -sS -sU -T4 -A -v |
p4 |
json |
Partial Intense Scan | -Pn -p- -T4 -A -v |
p5 |
json |
Complete Intense Scan | -Pn -sS -sU -T4 -A -PE -PP -PY -g 53 --script=vuln |
The profile is the type of scan that will be executed by the nmap subprocess. The Ip or target will be provided via argparse. At first the custom nmap scan is run which has all the curcial arguments for the scan to continue. Next, the scan data is extracted from the huge pile of data driven by nmap. the "scan" object has a list of sub-data under "tcp" each labled according to the ports opened. once the data is extracted the data is sent to openai API davenci model via a prompt. the prompt specifically asks for a JSON output and the data also to be used in a certain manner.
The entire structure of request that has to be sent to the openai API is designed in the completion section of the Program.
def scanner(ip: Optional[str], profile: int, key: str) -> str:
if key is not None:
pass
else:
raise ValueError("KeyNotFound: Key Not Provided")
# Handle the None case
profile_argument = ""
# The port profiles or scan types user can choose
if profile == 1:
profile_argument = '-Pn -sV -T4 -O -F'
elif profile == 2:
profile_argument = '-Pn -T4 -A -v'
elif profile == 3:
profile_argument = '-Pn -sS -sU -T4 -A -v'
elif profile == 4:
profile_argument = '-Pn -p- -T4 -A -v'
elif profile == 5:
profile_argument = '-Pn -sS -sU -T4 -A -PE -PP -PS80,443 -PA3389 -PU40125 -PY -g 53 --script=vuln'
else:
raise ValueError(f"Invalid Argument: {profile}")
# The scanner with GPT Implemented
nm.scan('{}'.format(ip), arguments='{}'.format(profile_argument))
json_data = nm.analyse_nmap_xml_scan()
analyze = json_data["scan"]
try:
response = PortAI(key, analyze)
except KeyboardInterrupt:
print("Bye")
quit()
return str(response)
Regex
We use Regex to extract only the important information from the custom prompt provided this reduces the total amount of unwanted data
def extract_data(json_string):
# Define the regular expression patterns for individual values
critical_score_pattern = r'"critical score": \["(.*?)"\]'
os_information_pattern = r'"os information": \["(.*?)"\]'
open_ports_pattern = r'"open ports": \["(.*?)"\]'
open_services_pattern = r'"open services": \["(.*?)"\]'
vulnerable_service_pattern = r'"vulnerable service": \["(.*?)"\]'
found_cve_pattern = r'"found cve": \["(.*?)"\]'
# Initialize variables for extracted data
critical_score = None
os_information = None
open_ports = None
open_services = None
vulnerable_service = None
found_cve = None
# Extract individual values using patterns
match = re.search(critical_score_pattern, json_string)
if match:
critical_score = match.group(1)
match = re.search(os_information_pattern, json_string)
if match:
os_information = match.group(1)
match = re.search(open_ports_pattern, json_string)
if match:
open_ports = match.group(1)
match = re.search(open_services_pattern, json_string)
if match:
open_services = match.group(1)
match = re.search(vulnerable_service_pattern, json_string)
if match:
vulnerable_service = match.group(1)
match = re.search(found_cve_pattern, json_string)
if match:
found_cve = match.group(1)
# Create a dictionary to store the extracted data
data = {
"critical score": critical_score,
"os information": os_information,
"open ports": open_ports,
"open services": open_services,
"vulnerable service": vulnerable_service,
"found cve": found_cve
}
# Convert the dictionary to JSON format
json_output = json.dumps(data)
return json_output
def AI(key: str, data: Any) -> str:
openai.api_key = key
try:
prompt = f"""
Do a NMAP scan analysis on the provided NMAP scan information
The NMAP output must return in a JSON format accorging to the provided
output format. The data must be accurate in regards towards a pentest report.
The data must follow the following rules:
1) The NMAP scans must be done from a pentester point of view
2) The final output must be minimal according to the format given.
3) The final output must be kept to a minimal.
4) If a value not found in the scan just mention an empty string.
5) Analyze everything even the smallest of data.
The output format:
{{
"critical score": [""],
"os information": [""],
"open ports": [""],
"open services": [""],
"vulnerable service": [""],
"found cve": [""]
}}
NMAP Data to be analyzed: {data}
"""
# A structure for the request
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
)
response = completion.choices[0].text
return extract_data(str(response))
except KeyboardInterrupt:
print("Bye")
quit()
The AI code defines an output format and commands the AI to follow a few pre dertermined rules to increase accuracy.
The regex extraction code does the extraction and further the main function arranges them into tables.
Output
nmap output:
ββββββββββββββββββββββ³ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Elements β Results β
β‘βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β critical score β High β
β os information β Microsoft Windows 11 21H2 β
β open ports β 80, 22, 445, 902, 912 β
β open services β http, ssh, microsoft-ds, vmware-auth, vmware-auth β
β vulnerable service β OpenSSH β
β found cve β CVE-2023-28531 β
ββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
DNS Output: target is jainuniversity.ac.in
ββββββββββββ³ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Elements β Results β
β‘βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β A β 172.67.147.95", "104.21.41.132 β
β AAA β β
β NS β mia.ns.cloudflare.com.","paul.ns.cloudflare.com. β
β MX β 30 aspmx5.googlemail.com.","30 aspmx4.googlemail.com.","20 alt2.aspmx.l.google.com.","30 β
β β aspmx3.googlemail.com.","30 aspmx2.googlemail.com.","20 alt1.aspmx.l.google.com.","10 aspmx.l.google.com. β
β PTR β β
β SOA β mia.ns.cloudflare.com. dns.cloudflare.com. 2309618668 10000 2400 604800 3600 β
β TXT β atlassian-sending-domain-verification=5b358ce4-5ad3-404d-b4b4-005bf933603b","include:_spf.atlassian.net β
ββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
GEO Location output:
βββββββββββββββββββββββββββββββ³ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Identifiers β Data β
β‘ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β ip β βββββββββββββ β
β continent_code β AS β
β continent_name β Asia β
β country_code2 β IN β
β country_code3 β IND β
β country_name β India β
β country_capital β New Delhi β
β state_prov β Haryana β
β state_code β IN-HR β
β district β β
β city β Gurugram β
β zipcode β 122003 β
β latitude β 28.44324 β
β longitude β 77.05501 β
β is_eu β False β
β calling_code β +91 β
β country_tld β .in β
β languages β en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,β¦ β
β country_flag β https://ipgeolocation.io/static/flags/in_64.png β
β geoname_id β 9148991 β
β isp β Bharti Airtel Limited β
β connection_type β β
β organization β Bharti Airtel Limited β
β currency.code β INR β
β currency.name β Indian Rupee β
β currency.symbol β βΉ β
β time_zone.name β Asia/Kolkata β
β time_zone.offset β 5.5 β
β time_zone.current_time β 2023-07-11 17:08:35.057+0530 β
β time_zone.current_time_unix β 1689075515.057 β
β time_zone.is_dst β False β
β time_zone.dst_savings β 0 β
βββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Usage GUI
The GUI uses customtkinter for the running of the code. The interface is straight forward the only thing required to remember is:
- When using dns attack dont specify the profile
python GVA_gui.py
main window
output_DNS
output_nmap
oytput_geo
Advantage
- Can be used in developing a more advanced systems completly made of the API and scanner combination
- Has the capability to analize DNS information and reslove Mustiple records it a more better format.
- Can increase the effectiveness of the final system
- Can also perform subdomain enumeration
- Highly productive when working with models such as GPT3