forked from yashab-cyber/HackGpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackgpt.py
More file actions
executable file
·1111 lines (910 loc) · 42 KB
/
hackgpt.py
File metadata and controls
executable file
·1111 lines (910 loc) · 42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
HackGPT - Enterprise AI-Powered Penetration Testing Platform
Author: HackGPT Team
Version: 2.0.0 (Production-Ready)
Description: Enterprise-grade pentesting automation platform with advanced AI, microservices architecture,
and cloud-native capabilities for professional security assessments.
Features:
- Advanced AI Engine with ML pattern recognition
- Enterprise authentication and RBAC
- Real-time analytics and reporting
- Microservices architecture with Docker/Kubernetes support
- Performance optimization with caching and parallel processing
- Database persistence with PostgreSQL
- Compliance framework integration (OWASP, NIST, ISO27001, SOC2)
- Zero-day detection with behavioral analysis
"""
import os
import sys
import json
import time
import asyncio
import argparse
import logging
import configparser
from datetime import datetime, timedelta
from pathlib import Path
import threading
import queue
import hashlib
import uuid
from typing import Dict, List, Any, Optional, Union
# Load environment variables
from dotenv import load_dotenv
load_dotenv()
# Core imports
try:
import requests
import openai
import subprocess
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn
from rich.prompt import Prompt, Confirm
from rich.markdown import Markdown
from rich.live import Live
from rich.layout import Layout
import speech_recognition as sr
import pyttsx3
import pypandoc
import cvsslib
from flask import Flask, render_template, request, jsonify, session
from flask_cors import CORS
import redis
import psycopg2
import sqlalchemy
from celery import Celery
import docker
import kubernetes
import consul
import jwt
import bcrypt
from ldap3 import Server, Connection, ALL
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('Agg') # Non-interactive backend
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import DBSCAN
from sklearn.ensemble import IsolationForest
import websockets
import aiohttp
except ImportError as e:
print(f"Missing required package: {e}")
print("Please run: pip install -r requirements.txt")
sys.exit(1)
# Import our custom modules
try:
from database import get_db_manager, PentestSession, Vulnerability, User, AuditLog
from ai_engine import get_advanced_ai_engine
from security import EnterpriseAuth, ComplianceFrameworkMapper
from exploitation import AdvancedExploitationEngine, ZeroDayDetector
from reporting import DynamicReportGenerator, get_realtime_dashboard
from cloud import DockerManager, KubernetesManager, ServiceRegistry
from performance import get_cache_manager, get_parallel_processor
except ImportError as e:
print(f"Missing HackGPT modules: {e}")
print("Please ensure all modules are properly installed")
sys.exit(1)
# Initialize Rich Console
console = Console()
# Configuration
class Config:
"""Application configuration"""
def __init__(self, config_file: str = "config.ini"):
self.config = configparser.ConfigParser()
self.config_file = config_file
self.load_config()
# Environment variables override config file
self.DATABASE_URL = os.getenv("DATABASE_URL", self.config.get("database", "url", fallback="postgresql://hackgpt:hackgpt123@localhost:5432/hackgpt"))
self.REDIS_URL = os.getenv("REDIS_URL", self.config.get("cache", "redis_url", fallback="redis://localhost:6379/0"))
self.OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", self.config.get("ai", "openai_api_key", fallback=""))
self.SECRET_KEY = os.getenv("SECRET_KEY", self.config.get("security", "secret_key", fallback=str(uuid.uuid4())))
self.LDAP_SERVER = os.getenv("LDAP_SERVER", self.config.get("ldap", "server", fallback=""))
self.LDAP_BIND_DN = os.getenv("LDAP_BIND_DN", self.config.get("ldap", "bind_dn", fallback=""))
self.LDAP_BIND_PASSWORD = os.getenv("LDAP_BIND_PASSWORD", self.config.get("ldap", "bind_password", fallback=""))
# Application settings
self.DEBUG = self.config.getboolean("app", "debug", fallback=False)
self.LOG_LEVEL = self.config.get("app", "log_level", fallback="INFO")
self.MAX_WORKERS = self.config.getint("performance", "max_workers", fallback=10)
self.ENABLE_VOICE = self.config.getboolean("features", "enable_voice", fallback=True)
self.ENABLE_WEB_DASHBOARD = self.config.getboolean("features", "enable_web_dashboard", fallback=True)
self.ENABLE_REALTIME_DASHBOARD = self.config.getboolean("features", "enable_realtime_dashboard", fallback=True)
# Cloud settings
self.DOCKER_HOST = os.getenv("DOCKER_HOST", self.config.get("cloud", "docker_host", fallback="unix:///var/run/docker.sock"))
self.KUBERNETES_CONFIG = os.getenv("KUBECONFIG", self.config.get("cloud", "kubernetes_config", fallback=""))
self.SERVICE_REGISTRY_BACKEND = self.config.get("cloud", "service_registry_backend", fallback="memory")
def load_config(self):
"""Load configuration from file"""
if os.path.exists(self.config_file):
self.config.read(self.config_file)
else:
# Create default config
self.create_default_config()
def create_default_config(self):
"""Create default configuration file"""
self.config.add_section("app")
self.config.set("app", "debug", "false")
self.config.set("app", "log_level", "INFO")
self.config.add_section("database")
self.config.set("database", "url", "postgresql://hackgpt:hackgpt123@localhost:5432/hackgpt")
self.config.add_section("cache")
self.config.set("cache", "redis_url", "redis://localhost:6379/0")
self.config.add_section("ai")
self.config.set("ai", "openai_api_key", "")
self.config.set("ai", "local_model", "llama2:7b")
self.config.add_section("security")
self.config.set("security", "secret_key", str(uuid.uuid4()))
self.config.set("security", "jwt_algorithm", "HS256")
self.config.set("security", "jwt_expiry", "3600")
self.config.add_section("ldap")
self.config.set("ldap", "server", "")
self.config.set("ldap", "bind_dn", "")
self.config.set("ldap", "bind_password", "")
self.config.add_section("performance")
self.config.set("performance", "max_workers", "10")
self.config.set("performance", "cache_ttl", "3600")
self.config.add_section("features")
self.config.set("features", "enable_voice", "true")
self.config.set("features", "enable_web_dashboard", "true")
self.config.set("features", "enable_realtime_dashboard", "true")
self.config.add_section("cloud")
self.config.set("cloud", "docker_host", "unix:///var/run/docker.sock")
self.config.set("cloud", "kubernetes_config", "")
self.config.set("cloud", "service_registry_backend", "memory")
with open(self.config_file, 'w') as f:
self.config.write(f)
# Initialize configuration
config = Config()
# Setup logging
logging.basicConfig(
level=getattr(logging, config.LOG_LEVEL),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('logs/hackgpt.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger('hackgpt')
# ASCII Banner
BANNER = """
[bold red]
██╗ ██╗ █████╗ ██████╗██╗ ██╗ ██████╗ ██████╗ ████████╗
██║ ██║██╔══██╗██╔════╝██║ ██╔╝██╔════╝ ██╔══██╗╚══██╔══╝
███████║███████║██║ █████╔╝ ██║ ███╗██████╔╝ ██║
██╔══██║██╔══██║██║ ██╔═██╗ ██║ ██║██╔═══╝ ██║
██║ ██║██║ ██║╚██████╗██║ ██╗╚██████╔╝██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
[/bold red]
[bold cyan] Enterprise AI-Powered Penetration Testing Platform v2.0[/bold cyan]
[bold green] Production-Ready | Cloud-Native | AI-Enhanced[/bold green]
[dim] Advanced Security Assessment Platform[/dim]
"""
class AIEngine:
"""AI Engine for decision making and analysis"""
def __init__(self):
self.api_key = os.getenv('OPENAI_API_KEY')
self.local_mode = not bool(self.api_key)
self.console = Console()
if self.local_mode:
self.console.print("[yellow]No OpenAI API key found. Running in local mode.[/yellow]")
self.setup_local_llm()
else:
openai.api_key = self.api_key
def setup_local_llm(self):
"""Setup local LLM using ollama"""
try:
result = subprocess.run(['which', 'ollama'], capture_output=True, text=True)
if result.returncode != 0:
self.console.print("[yellow]Installing ollama for local AI...[/yellow]")
subprocess.run(['curl', '-fsSL', 'https://ollama.ai/install.sh', '|', 'sh'], shell=True)
# Pull a lightweight model
subprocess.run(['ollama', 'pull', 'llama2:7b'], check=True)
self.console.print("[green]Local LLM setup complete[/green]")
except Exception as e:
self.console.print(f"[red]Error setting up local LLM: {e}[/red]")
def analyze(self, context, data, phase="general"):
"""Analyze data using AI"""
prompt = self._create_prompt(context, data, phase)
if self.local_mode:
return self._query_local_llm(prompt)
else:
return self._query_openai(prompt)
def _create_prompt(self, context, data, phase):
"""Create appropriate prompt based on phase"""
base_prompt = f"""
You are HackGPT, an expert penetration testing AI assistant.
Context: {context}
Phase: {phase}
Data to analyze: {data}
Please provide:
1. Summary of findings
2. Risk assessment
3. Recommended next actions
4. Specific commands or techniques to try
Keep responses concise and actionable.
"""
return base_prompt
def _query_openai(self, prompt):
"""Query OpenAI API"""
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=1000,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
return f"AI Error: {str(e)}"
def _query_local_llm(self, prompt):
"""Query local LLM using ollama"""
try:
result = subprocess.run(
['ollama', 'run', 'llama2:7b', prompt],
capture_output=True, text=True, timeout=60
)
return result.stdout if result.returncode == 0 else f"Local AI Error: {result.stderr}"
except Exception as e:
return f"Local AI Error: {str(e)}"
class ToolManager:
"""Manages pentesting tools installation and execution"""
TOOL_COMMANDS = {
'nmap': 'sudo apt install -y nmap',
'masscan': 'sudo apt install -y masscan',
'nikto': 'sudo apt install -y nikto',
'gobuster': 'sudo apt install -y gobuster',
'sqlmap': 'sudo apt install -y sqlmap',
'hydra': 'sudo apt install -y hydra',
'theharvester': 'sudo apt install -y theharvester',
'enum4linux': 'sudo apt install -y enum4linux',
'whatweb': 'sudo apt install -y whatweb',
'wpscan': 'sudo apt install -y wpscan',
'dnsenum': 'sudo apt install -y dnsenum',
'whois': 'sudo apt install -y whois',
'searchsploit': 'sudo apt install -y exploitdb',
'metasploit-framework': 'sudo apt install -y metasploit-framework',
'netcat': 'sudo apt install -y netcat-traditional',
'curl': 'sudo apt install -y curl',
'wget': 'sudo apt install -y wget',
}
GITHUB_TOOLS = {
'linpeas': {
'url': 'https://github.com/carlospolop/PEASS-ng.git',
'path': '/opt/PEASS-ng',
'executable': '/opt/PEASS-ng/linPEAS/linpeas.sh'
},
'winpeas': {
'url': 'https://github.com/carlospolop/PEASS-ng.git',
'path': '/opt/PEASS-ng',
'executable': '/opt/PEASS-ng/winPEAS/winPEAS.exe'
}
}
def __init__(self):
self.console = Console()
self.installed_tools = set()
def check_tool(self, tool_name):
"""Check if tool is installed"""
result = subprocess.run(['which', tool_name], capture_output=True, text=True)
return result.returncode == 0
def install_tool(self, tool_name):
"""Install a specific tool"""
if tool_name in self.installed_tools:
return True
self.console.print(f"[yellow]Installing {tool_name}...[/yellow]")
try:
if tool_name in self.TOOL_COMMANDS:
cmd = self.TOOL_COMMANDS[tool_name]
result = subprocess.run(cmd.split(), check=True, capture_output=True, text=True)
self.installed_tools.add(tool_name)
self.console.print(f"[green]✓ {tool_name} installed successfully[/green]")
return True
elif tool_name in self.GITHUB_TOOLS:
tool_info = self.GITHUB_TOOLS[tool_name]
if not os.path.exists(tool_info['path']):
subprocess.run(['git', 'clone', tool_info['url'], tool_info['path']], check=True)
subprocess.run(['chmod', '+x', '-R', tool_info['path']], check=True)
self.installed_tools.add(tool_name)
self.console.print(f"[green]✓ {tool_name} installed successfully[/green]")
return True
except subprocess.CalledProcessError as e:
self.console.print(f"[red]✗ Failed to install {tool_name}: {e}[/red]")
return False
return False
def ensure_tools(self, tools):
"""Ensure all required tools are installed"""
missing_tools = []
for tool in tools:
if not self.check_tool(tool) and tool not in self.installed_tools:
missing_tools.append(tool)
if missing_tools:
self.console.print(f"[yellow]Missing tools: {', '.join(missing_tools)}[/yellow]")
for tool in missing_tools:
self.install_tool(tool)
return len(missing_tools) == 0
def run_command(self, command, timeout=300):
"""Execute a system command safely"""
try:
self.console.print(f"[cyan]Executing: {command}[/cyan]")
result = subprocess.run(
command.split() if isinstance(command, str) else command,
capture_output=True, text=True, timeout=timeout
)
return {
'success': result.returncode == 0,
'stdout': result.stdout,
'stderr': result.stderr,
'command': command
}
except subprocess.TimeoutExpired:
return {
'success': False,
'stdout': '',
'stderr': f'Command timed out after {timeout} seconds',
'command': command
}
except Exception as e:
return {
'success': False,
'stdout': '',
'stderr': str(e),
'command': command
}
class PentestingPhases:
"""Implementation of the 6 pentesting phases"""
def __init__(self, ai_engine, tool_manager, target, scope, auth_key):
self.ai = ai_engine
self.tools = tool_manager
self.target = target
self.scope = scope
self.auth_key = auth_key
self.console = Console()
self.results = {}
# Setup reports directory
self.report_dir = Path(f"/reports/{target}")
self.report_dir.mkdir(parents=True, exist_ok=True)
def phase1_reconnaissance(self):
"""Phase 1: Planning & Reconnaissance"""
self.console.print(Panel("[bold blue]Phase 1: Planning & Reconnaissance[/bold blue]"))
# Ensure required tools
recon_tools = ['theharvester', 'whois', 'dnsenum', 'nmap', 'masscan']
self.tools.ensure_tools(recon_tools)
results = {}
# Passive Reconnaissance
self.console.print("[yellow]Starting passive reconnaissance...[/yellow]")
# theHarvester
harvester_cmd = f"theharvester -d {self.target} -b all -f {self.report_dir}/harvester.json"
harvester_result = self.tools.run_command(harvester_cmd)
results['harvester'] = harvester_result
# WHOIS lookup
whois_cmd = f"whois {self.target}"
whois_result = self.tools.run_command(whois_cmd)
results['whois'] = whois_result
# DNS enumeration
dns_cmd = f"dnsenum {self.target}"
dns_result = self.tools.run_command(dns_cmd)
results['dns'] = dns_result
# Active Reconnaissance
self.console.print("[yellow]Starting active reconnaissance...[/yellow]")
# Nmap service detection
nmap_cmd = f"nmap -sV -Pn {self.target} -oN {self.report_dir}/nmap_services.txt"
nmap_result = self.tools.run_command(nmap_cmd)
results['nmap'] = nmap_result
# Masscan for fast port scanning
masscan_cmd = f"masscan -p1-65535 {self.target} --rate=1000"
masscan_result = self.tools.run_command(masscan_cmd)
results['masscan'] = masscan_result
# AI Analysis
combined_output = "\n".join([f"{k}: {v['stdout']}" for k, v in results.items()])
ai_analysis = self.ai.analyze(
f"Reconnaissance phase for target {self.target}",
combined_output,
"reconnaissance"
)
results['ai_analysis'] = ai_analysis
self.results['phase1'] = results
self.console.print(Panel(ai_analysis, title="[green]AI Analysis[/green]"))
# Save results
self._save_phase_results("phase1_reconnaissance", results)
return results
def phase2_scanning_enumeration(self):
"""Phase 2: Scanning & Enumeration"""
self.console.print(Panel("[bold blue]Phase 2: Scanning & Enumeration[/bold blue]"))
# Ensure required tools
scan_tools = ['nmap', 'nikto', 'gobuster', 'whatweb', 'enum4linux']
self.tools.ensure_tools(scan_tools)
results = {}
# Vulnerability scanning
self.console.print("[yellow]Starting vulnerability scanning...[/yellow]")
# Nmap vulnerability scripts
nmap_vuln_cmd = f"nmap --script vuln {self.target} -oN {self.report_dir}/nmap_vulns.txt"
nmap_vuln_result = self.tools.run_command(nmap_vuln_cmd)
results['nmap_vulns'] = nmap_vuln_result
# Web application scanning
self.console.print("[yellow]Starting web application scanning...[/yellow]")
# Nikto web vulnerability scanner
nikto_cmd = f"nikto -h {self.target} -output {self.report_dir}/nikto.txt"
nikto_result = self.tools.run_command(nikto_cmd)
results['nikto'] = nikto_result
# Directory brute forcing with gobuster
gobuster_cmd = f"gobuster dir -u http://{self.target} -w /usr/share/wordlists/dirb/common.txt -o {self.report_dir}/gobuster.txt"
gobuster_result = self.tools.run_command(gobuster_cmd)
results['gobuster'] = gobuster_result
# Technology stack detection
whatweb_cmd = f"whatweb {self.target}"
whatweb_result = self.tools.run_command(whatweb_cmd)
results['whatweb'] = whatweb_result
# SMB/NetBIOS enumeration
enum4linux_cmd = f"enum4linux {self.target}"
enum4linux_result = self.tools.run_command(enum4linux_cmd)
results['enum4linux'] = enum4linux_result
# AI Analysis
combined_output = "\n".join([f"{k}: {v['stdout']}" for k, v in results.items()])
ai_analysis = self.ai.analyze(
f"Scanning and enumeration phase for target {self.target}",
combined_output,
"scanning"
)
results['ai_analysis'] = ai_analysis
self.results['phase2'] = results
self.console.print(Panel(ai_analysis, title="[green]AI Analysis[/green]"))
# Save results
self._save_phase_results("phase2_scanning_enumeration", results)
return results
def phase3_exploitation(self, confirm=True):
"""Phase 3: Exploitation"""
self.console.print(Panel("[bold red]Phase 3: Exploitation[/bold red]"))
if confirm and not Confirm.ask("[red]This phase will attempt to exploit vulnerabilities. Continue?[/red]"):
self.console.print("[yellow]Exploitation phase skipped by user.[/yellow]")
return {}
# Ensure required tools
exploit_tools = ['searchsploit', 'sqlmap', 'hydra', 'metasploit-framework']
self.tools.ensure_tools(exploit_tools)
results = {}
# Search for exploits
self.console.print("[yellow]Searching for available exploits...[/yellow]")
# Use AI to identify potential vulnerabilities from previous phases
if 'phase2' in self.results:
vuln_data = str(self.results['phase2'])
exploit_suggestions = self.ai.analyze(
f"Suggest exploits for target {self.target}",
vuln_data,
"exploitation_planning"
)
results['exploit_suggestions'] = exploit_suggestions
self.console.print(Panel(exploit_suggestions, title="[yellow]Exploit Suggestions[/yellow]"))
# SQL injection testing
sqlmap_cmd = f"sqlmap -u http://{self.target} --batch --crawl=2"
sqlmap_result = self.tools.run_command(sqlmap_cmd)
results['sqlmap'] = sqlmap_result
# Brute force common services (with rate limiting)
hydra_cmd = f"hydra -L /usr/share/wordlists/metasploit/unix_users.txt -P /usr/share/wordlists/metasploit/unix_passwords.txt -t 4 {self.target} ssh"
hydra_result = self.tools.run_command(hydra_cmd)
results['hydra'] = hydra_result
# AI Analysis
combined_output = "\n".join([f"{k}: {str(v)}" for k, v in results.items()])
ai_analysis = self.ai.analyze(
f"Exploitation phase for target {self.target}",
combined_output,
"exploitation"
)
results['ai_analysis'] = ai_analysis
self.results['phase3'] = results
self.console.print(Panel(ai_analysis, title="[green]AI Analysis[/green]"))
# Save results
self._save_phase_results("phase3_exploitation", results)
return results
def phase4_post_exploitation(self):
"""Phase 4: Post-Exploitation"""
self.console.print(Panel("[bold blue]Phase 4: Post-Exploitation[/bold blue]"))
# This phase would only run if exploitation was successful
# For demo purposes, we'll show what would happen
results = {}
self.console.print("[yellow]Post-exploitation activities (simulated):[/yellow]")
self.console.print("• Privilege escalation enumeration")
self.console.print("• Credential harvesting")
self.console.print("• Lateral movement assessment")
self.console.print("• Data exfiltration simulation")
# AI provides post-exploitation guidance
ai_analysis = self.ai.analyze(
f"Post-exploitation guidance for {self.target}",
"Simulated successful exploitation",
"post_exploitation"
)
results['ai_analysis'] = ai_analysis
self.results['phase4'] = results
self.console.print(Panel(ai_analysis, title="[green]AI Analysis[/green]"))
# Save results
self._save_phase_results("phase4_post_exploitation", results)
return results
def phase5_reporting(self):
"""Phase 5: Reporting"""
self.console.print(Panel("[bold blue]Phase 5: Reporting[/bold blue]"))
results = {}
# Generate comprehensive report
report_data = {
'target': self.target,
'scope': self.scope,
'timestamp': datetime.now().isoformat(),
'phases': self.results
}
# AI-generated executive summary
all_findings = json.dumps(self.results, indent=2)
executive_summary = self.ai.analyze(
f"Generate executive summary for pentest of {self.target}",
all_findings,
"executive_summary"
)
# Technical report
technical_report = self.ai.analyze(
f"Generate technical report for pentest of {self.target}",
all_findings,
"technical_report"
)
# Create reports
self._create_markdown_report(report_data, executive_summary, technical_report)
self._create_json_report(report_data)
results['executive_summary'] = executive_summary
results['technical_report'] = technical_report
self.results['phase5'] = results
self.console.print("[green]Reports generated successfully![/green]")
self.console.print(f"[cyan]Report location: {self.report_dir}[/cyan]")
return results
def phase6_retesting(self):
"""Phase 6: Retesting"""
self.console.print(Panel("[bold blue]Phase 6: Retesting[/bold blue]"))
results = {}
# AI guidance on retesting
ai_analysis = self.ai.analyze(
f"Retesting strategy for {self.target}",
"After remediation efforts",
"retesting"
)
results['ai_analysis'] = ai_analysis
results['retest_plan'] = "Focused retesting on identified vulnerabilities"
self.console.print(Panel(ai_analysis, title="[green]Retesting Plan[/green]"))
# Save results
self._save_phase_results("phase6_retesting", results)
return results
def _save_phase_results(self, phase_name, results):
"""Save phase results to file"""
with open(self.report_dir / f"{phase_name}.json", 'w') as f:
json.dump(results, f, indent=2, default=str)
def _create_markdown_report(self, report_data, executive_summary, technical_report):
"""Create markdown report"""
markdown_content = f"""
# Penetration Testing Report
## Executive Summary
{executive_summary}
## Technical Report
{technical_report}
## Test Details
- **Target:** {report_data['target']}
- **Scope:** {report_data['scope']}
- **Date:** {report_data['timestamp']}
## Detailed Findings
"""
for phase, data in report_data['phases'].items():
markdown_content += f"\n### {phase.replace('_', ' ').title()}\n"
if 'ai_analysis' in data:
markdown_content += f"{data['ai_analysis']}\n"
# Save markdown
with open(self.report_dir / "report.md", 'w') as f:
f.write(markdown_content)
# Convert to PDF if possible
try:
pypandoc.convert_file(
str(self.report_dir / "report.md"),
'pdf',
outputfile=str(self.report_dir / "report.pdf")
)
except Exception as e:
self.console.print(f"[yellow]Could not generate PDF: {e}[/yellow]")
def _create_json_report(self, report_data):
"""Create JSON report"""
with open(self.report_dir / "report.json", 'w') as f:
json.dump(report_data, f, indent=2, default=str)
class VoiceInterface:
"""Voice command interface"""
def __init__(self):
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone()
self.tts_engine = pyttsx3.init()
self.console = Console()
def listen_for_command(self):
"""Listen for voice commands"""
try:
with self.microphone as source:
self.console.print("[cyan]Listening for voice command...[/cyan]")
audio = self.recognizer.listen(source, timeout=5)
command = self.recognizer.recognize_google(audio)
self.console.print(f"[green]Heard: {command}[/green]")
return command.lower()
except sr.UnknownValueError:
return None
except sr.RequestError:
self.console.print("[red]Voice recognition service unavailable[/red]")
return None
except sr.WaitTimeoutError:
return None
def speak(self, text):
"""Text-to-speech output"""
self.tts_engine.say(text)
self.tts_engine.runAndWait()
class WebDashboard:
"""Flask web dashboard"""
def __init__(self, hackgpt_instance):
self.app = Flask(__name__)
self.hackgpt = hackgpt_instance
self.setup_routes()
def setup_routes(self):
"""Setup Flask routes"""
@self.app.route('/')
def index():
return render_template('dashboard.html')
@self.app.route('/api/status')
def status():
return jsonify({'status': 'running'})
@self.app.route('/api/run_pentest', methods=['POST'])
def run_pentest():
data = request.json
# Run pentest in background thread
thread = threading.Thread(
target=self.hackgpt.run_full_pentest,
args=(data['target'], data['scope'], data['auth_key'])
)
thread.start()
return jsonify({'status': 'started'})
def run(self, host='0.0.0.0', port=5000):
"""Run the web dashboard"""
self.app.run(host=host, port=port, debug=False)
class HackGPT:
"""Main HackGPT application"""
def __init__(self):
self.ai_engine = AIEngine()
self.tool_manager = ToolManager()
self.voice_interface = VoiceInterface()
self.console = Console()
self.web_dashboard = None
def show_banner(self):
"""Display the HackGPT banner"""
self.console.print(BANNER)
def show_menu(self):
"""Display main menu"""
table = Table(title="HackGPT Main Menu")
table.add_column("Option", style="cyan")
table.add_column("Description", style="magenta")
table.add_row("1", "Full Pentest (All 6 Phases)")
table.add_row("2", "Run Specific Phase")
table.add_row("3", "View Reports")
table.add_row("4", "Configure AI Mode")
table.add_row("5", "Start Web Dashboard")
table.add_row("6", "Voice Command Mode")
table.add_row("0", "Exit")
self.console.print(table)
def get_target_info(self):
"""Get target information from user"""
target = Prompt.ask("[cyan]Enter target (IP/domain)[/cyan]")
scope = Prompt.ask("[cyan]Enter scope description[/cyan]")
auth_key = Prompt.ask("[cyan]Enter authorization key[/cyan]", password=True)
if not all([target, scope, auth_key]):
self.console.print("[red]All fields are required![/red]")
return None, None, None
return target, scope, auth_key
def run_full_pentest(self, target=None, scope=None, auth_key=None):
"""Run complete penetration test"""
if not all([target, scope, auth_key]):
target, scope, auth_key = self.get_target_info()
if not target:
return
self.console.print(f"[green]Starting full pentest against {target}[/green]")
# Initialize pentesting phases
phases = PentestingPhases(self.ai_engine, self.tool_manager, target, scope, auth_key)
try:
# Run all phases
phases.phase1_reconnaissance()
phases.phase2_scanning_enumeration()
phases.phase3_exploitation()
phases.phase4_post_exploitation()
phases.phase5_reporting()
phases.phase6_retesting()
self.console.print("[bold green]Full pentest completed![/bold green]")
except KeyboardInterrupt:
self.console.print("[yellow]Pentest interrupted by user[/yellow]")
except Exception as e:
self.console.print(f"[red]Error during pentest: {e}[/red]")
def run_specific_phase(self):
"""Run a specific phase"""
target, scope, auth_key = self.get_target_info()
if not target:
return
phases_menu = Table(title="Select Phase")
phases_menu.add_column("Phase", style="cyan")
phases_menu.add_column("Description", style="magenta")
phases_menu.add_row("1", "Planning & Reconnaissance")
phases_menu.add_row("2", "Scanning & Enumeration")
phases_menu.add_row("3", "Exploitation")
phases_menu.add_row("4", "Post-Exploitation")
phases_menu.add_row("5", "Reporting")
phases_menu.add_row("6", "Retesting")
self.console.print(phases_menu)
choice = Prompt.ask("[cyan]Select phase[/cyan]", choices=["1", "2", "3", "4", "5", "6"])
phases = PentestingPhases(self.ai_engine, self.tool_manager, target, scope, auth_key)
phase_methods = {
"1": phases.phase1_reconnaissance,
"2": phases.phase2_scanning_enumeration,
"3": phases.phase3_exploitation,
"4": phases.phase4_post_exploitation,
"5": phases.phase5_reporting,
"6": phases.phase6_retesting
}
phase_methods[choice]()
def view_reports(self):
"""View existing reports"""
reports_dir = Path("/reports")
if not reports_dir.exists():
self.console.print("[yellow]No reports directory found[/yellow]")
return
targets = [d.name for d in reports_dir.iterdir() if d.is_dir()]
if not targets:
self.console.print("[yellow]No reports found[/yellow]")
return
table = Table(title="Available Reports")
table.add_column("Target", style="cyan")
table.add_column("Reports", style="magenta")
for target in targets:
target_dir = reports_dir / target
reports = [f.name for f in target_dir.iterdir() if f.is_file()]
table.add_row(target, ", ".join(reports))
self.console.print(table)
def configure_ai_mode(self):
"""Configure AI mode"""
current_mode = "Local LLM" if self.ai_engine.local_mode else "OpenAI API"
self.console.print(f"[cyan]Current AI mode: {current_mode}[/cyan]")
if Confirm.ask("Switch AI mode?"):
if self.ai_engine.local_mode:
api_key = Prompt.ask("Enter OpenAI API key", password=True)
if api_key:
os.environ['OPENAI_API_KEY'] = api_key
self.ai_engine = AIEngine()
self.console.print("[green]Switched to OpenAI API mode[/green]")
else:
if 'OPENAI_API_KEY' in os.environ:
del os.environ['OPENAI_API_KEY']
self.ai_engine = AIEngine()
self.console.print("[green]Switched to Local LLM mode[/green]")
def start_web_dashboard(self):
"""Start web dashboard"""
self.web_dashboard = WebDashboard(self)
self.console.print("[cyan]Starting web dashboard on http://0.0.0.0:5000[/cyan]")
# Create dashboard template
self.create_dashboard_template()
try:
self.web_dashboard.run()
except Exception as e:
self.console.print(f"[red]Error starting web dashboard: {e}[/red]")
def create_dashboard_template(self):
"""Create HTML template for dashboard"""
template_dir = Path("templates")
template_dir.mkdir(exist_ok=True)
dashboard_html = """
<!DOCTYPE html>
<html>
<head>
<title>HackGPT Dashboard</title>
<style>
body { background: #000; color: #0f0; font-family: monospace; }
.container { margin: 20px; }
.panel { border: 1px solid #0f0; padding: 20px; margin: 10px 0; }
button { background: #333; color: #0f0; border: 1px solid #0f0; padding: 10px; }
input { background: #333; color: #0f0; border: 1px solid #0f0; padding: 5px; }
</style>
</head>
<body>
<div class="container">
<h1>HackGPT - AI-Powered Penetration Testing</h1>
<div class="panel">
<h2>Start Pentest</h2>
<input type="text" id="target" placeholder="Target IP/Domain">
<input type="text" id="scope" placeholder="Scope">
<input type="password" id="auth" placeholder="Authorization Key">
<button onclick="startPentest()">Start Full Pentest</button>
</div>