1+ #!/usr/bin/env python3
2+ """
3+ SQLMap AI - Unified CLI Runner
4+ Supports both simple and enhanced modes for SQL injection testing
5+ """
6+
7+ import sys
8+ import os
9+ import asyncio
10+ import argparse
11+ from pathlib import Path
12+
13+ # Import UI functions
14+ try :
15+ from sqlmap_ai .ui import print_info , print_success , print_error , print_warning
16+ except ImportError :
17+ # Fallback UI functions if sqlmap_ai.ui is not available
18+ def print_info (msg ): print (f"[INFO] { msg } " )
19+ def print_success (msg ): print (f"[SUCCESS] { msg } " )
20+ def print_error (msg ): print (f"[ERROR] { msg } " )
21+ def print_warning (msg ): print (f"[WARNING] { msg } " )
22+
23+ def show_usage ():
24+ """Show usage information"""
25+ print ("""
26+ SQLMap AI - Next Generation AI-Powered SQL Injection Testing
27+
28+ USAGE:
29+ sqlmap-ai [MODE] [OPTIONS]
30+
31+ MODES:
32+ simple - Basic SQL injection testing (legacy mode)
33+ enhanced - Advanced AI-powered testing with full features (default)
34+
35+ EXAMPLES:
36+ # Enhanced mode (default)
37+ sqlmap-ai -u "http://example.com/page.php?id=1"
38+ sqlmap-ai --enhanced -u "http://example.com/page.php?id=1"
39+
40+ # Simple mode
41+ sqlmap-ai --simple -u "http://example.com/page.php?id=1"
42+
43+ # Configuration
44+ sqlmap-ai --config-wizard
45+ sqlmap-ai --show-config
46+
47+ For full help: sqlmap-ai --help
48+ For enhanced mode help: sqlmap-ai --enhanced --help
49+ For simple mode help: sqlmap-ai --simple --help
50+ """ )
51+
52+ def show_simple_help ():
53+ """Show simple mode help"""
54+ print ("""
55+ SQLMap AI - Simple Mode Help (Legacy Mode)
56+
57+ USAGE:
58+ sqlmap-ai --simple [OPTIONS]
59+
60+ DESCRIPTION:
61+ Simple mode provides basic SQL injection testing without AI features.
62+ It's faster and has minimal dependencies.
63+
64+ OPTIONS:
65+ -u, --url URL Target URL (e.g., http://example.com/page.php?id=1)
66+ -r, --request-file FILE Load HTTP request from file
67+ --timeout SECONDS Timeout in seconds (default: 60)
68+ --level LEVEL SQLMap level (1-5, default: 1)
69+ --risk RISK SQLMap risk (1-3, default: 1)
70+ --batch Never ask for user input, use default behavior
71+ --random-agent Use randomly selected HTTP User-Agent header
72+ --save-report Save results to file
73+ -h, --help Show this help message
74+
75+ EXAMPLES:
76+ # Basic scan
77+ sqlmap-ai --simple -u "http://example.com/page.php?id=1"
78+
79+ # Scan with custom options
80+ sqlmap-ai --simple -u "http://example.com/page.php?id=1" --level 2 --risk 2
81+
82+ # Interactive mode (no arguments)
83+ sqlmap-ai --simple
84+
85+ FEATURES:
86+ ✓ Basic SQL injection detection
87+ ✓ Standard SQLMap functionality
88+ ✓ Minimal dependencies
89+ ✓ Fast execution
90+ ✓ Simple text output
91+ ✓ Basic result saving
92+ """ )
93+
94+ def show_enhanced_help ():
95+ """Show enhanced mode help"""
96+ print ("""
97+ SQLMap AI - Enhanced Mode Help (AI-Powered Mode)
98+
99+ USAGE:
100+ sqlmap-ai --enhanced [OPTIONS]
101+
102+ DESCRIPTION:
103+ Enhanced mode provides AI-powered SQL injection testing with advanced features.
104+ It includes adaptive testing, WAF evasion, and comprehensive reporting.
105+
106+ OPTIONS:
107+ Target Specification:
108+ -u, --url URL Target URL (e.g., http://example.com/page.php?id=1)
109+ -r, --request-file FILE Load HTTP request from file
110+ --crawl DEPTH Crawl website starting from target URL (depth: 1-3)
111+
112+ AI Configuration:
113+ --ai-provider PROVIDER AI provider to use (groq, openai, anthropic, ollama, auto)
114+ --adaptive Enable adaptive step-by-step testing
115+ --ai-analysis Enable AI-powered result analysis
116+
117+ Testing Options:
118+ --level LEVEL SQLMap level (1-5, default: 1)
119+ --risk RISK SQLMap risk (1-3, default: 1)
120+ --timeout SECONDS Timeout in seconds (default: 300)
121+ --threads THREADS Number of concurrent threads (default: 1)
122+ --batch Never ask for user input, use default behavior
123+ --random-agent Use randomly selected HTTP User-Agent header
124+
125+ WAF Evasion:
126+ --tamper SCRIPT Use given script(s) for tampering injection data
127+ --waf-bypass Enable automatic WAF bypass techniques
128+
129+ Reporting:
130+ --html-report Generate HTML report
131+ --json-report Generate JSON report
132+ --no-report Disable report generation
133+ --report-dir DIR Directory to save reports (default: reports/)
134+
135+ Configuration:
136+ --config-wizard Run configuration wizard
137+ --show-config Show current configuration
138+ --export-config FILE Export configuration to file
139+
140+ Other:
141+ --debug Enable debug mode
142+ --interactive Enable interactive mode
143+ -h, --help Show this help message
144+
145+ EXAMPLES:
146+ # Basic enhanced scan
147+ sqlmap-ai --enhanced -u "http://example.com/page.php?id=1"
148+
149+ # Adaptive testing with AI analysis
150+ sqlmap-ai --enhanced -u "http://example.com/page.php?id=1" --adaptive --ai-provider groq
151+
152+ # Using Ollama for local AI analysis
153+ sqlmap-ai --enhanced -u "http://example.com/page.php?id=1" --ai-provider ollama
154+
155+ # Advanced scan with custom options
156+ sqlmap-ai --enhanced -u "http://example.com/page.php?id=1" --level 3 --risk 2 --threads 5
157+
158+ # Configuration
159+ sqlmap-ai --enhanced --config-wizard
160+
161+ FEATURES:
162+ ✓ AI-powered vulnerability analysis
163+ ✓ Adaptive testing strategies
164+ ✓ WAF evasion techniques
165+ ✓ Beautiful HTML reports
166+ ✓ Risk assessment and remediation guidance
167+ ✓ Interactive CLI with progress tracking
168+ ✓ Multiple AI providers (Groq, OpenAI, Anthropic, Ollama)
169+ ✓ Advanced configuration management
170+ ✓ Comprehensive logging and audit trails
171+ """ )
172+
173+ def create_simple_parser ():
174+ """Create parser for simple mode"""
175+ parser = argparse .ArgumentParser (
176+ description = "SQLMap AI - Simple Mode (Legacy Mode)" ,
177+ add_help = False
178+ )
179+
180+ # Target options
181+ parser .add_argument ('-u' , '--url' , help = 'Target URL' )
182+ parser .add_argument ('-r' , '--request-file' , help = 'Load HTTP request from file' )
183+
184+ # SQLMap options
185+ parser .add_argument ('--timeout' , type = int , default = 60 , help = 'Timeout in seconds' )
186+ parser .add_argument ('--level' , type = int , choices = [1 ,2 ,3 ,4 ,5 ], default = 1 , help = 'SQLMap level' )
187+ parser .add_argument ('--risk' , type = int , choices = [1 ,2 ,3 ], default = 1 , help = 'SQLMap risk' )
188+ parser .add_argument ('--batch' , action = 'store_true' , help = 'Never ask for user input' )
189+ parser .add_argument ('--random-agent' , action = 'store_true' , help = 'Use random User-Agent' )
190+
191+ # Output options
192+ parser .add_argument ('--save-report' , action = 'store_true' , help = 'Save results to file' )
193+
194+ # Help
195+ parser .add_argument ('-h' , '--help' , action = 'store_true' , help = 'Show help message' )
196+
197+ return parser
198+
199+ def run_simple_mode ():
200+ """Run in simple mode - basic SQLMap functionality"""
201+ print ("🔧 Starting SQLMap AI in SIMPLE mode (Legacy Mode)..." )
202+ print ("This mode provides basic SQL injection testing without AI features." )
203+ print ()
204+
205+ # Parse arguments for simple mode
206+ parser = create_simple_parser ()
207+ args , unknown = parser .parse_known_args ()
208+
209+ # Show help if requested
210+ if args .help or len (sys .argv ) == 2 and sys .argv [1 ] == '--simple' :
211+ show_simple_help ()
212+ return
213+
214+ # If no URL provided, run interactive mode
215+ if not args .url and not args .request_file :
216+ try :
217+ from sqlmap_ai .main import main_simple
218+ main_simple ()
219+ except ImportError :
220+ print ("[ERROR] Simple mode not available. Falling back to enhanced mode..." )
221+ run_enhanced_mode ()
222+ return
223+
224+ # Run with provided arguments
225+ try :
226+ from sqlmap_ai .runner import SQLMapRunner
227+ runner = SQLMapRunner ()
228+
229+ # Build SQLMap options
230+ options = []
231+ if args .batch :
232+ options .append ("--batch" )
233+ if args .random_agent :
234+ options .append ("--random-agent" )
235+ options .extend (["--level" , str (args .level )])
236+ options .extend (["--risk" , str (args .risk )])
237+
238+ # Run scan
239+ result = runner .run_sqlmap (args .url , " " .join (options ), timeout = args .timeout , interactive_mode = False )
240+
241+ if result :
242+ print_success ("Scan completed!" )
243+
244+ # Extract basic info
245+ from sqlmap_ai .parser import extract_sqlmap_info
246+ scan_info = extract_sqlmap_info (result )
247+
248+ if scan_info .get ('vulnerable_parameters' ):
249+ print_success (f"Vulnerabilities found: { len (scan_info ['vulnerable_parameters' ])} " )
250+ for param in scan_info ['vulnerable_parameters' ]:
251+ print (f" - Parameter: { param } " )
252+ else :
253+ print_info ("No vulnerabilities detected" )
254+
255+ # Save report if requested
256+ if args .save_report :
257+ import time
258+ timestamp = int (time .time ())
259+ filename = f"reports/simple_scan_{ timestamp } .txt"
260+
261+ import os
262+ os .makedirs ("reports" , exist_ok = True )
263+
264+ with open (filename , 'w' , encoding = 'utf-8' ) as f :
265+ f .write (f"SQLMap AI Simple Scan Report\n " )
266+ f .write (f"Generated: { time .strftime ('%Y-%m-%d %H:%M:%S' )} \n " )
267+ f .write (f"Target: { args .url } \n " )
268+ f .write (f"Options: { ' ' .join (options )} \n " )
269+ f .write ("-" * 50 + "\n " )
270+ f .write (result )
271+
272+ print_success (f"Results saved to: { filename } " )
273+ else :
274+ print_error ("Scan failed or no results obtained" )
275+
276+ except Exception as e :
277+ print_error (f"Error during scan: { e } " )
278+ print_info ("Try enhanced mode for more features and better error handling" )
279+
280+ def run_enhanced_mode ():
281+ """Run in enhanced mode - full AI-powered features"""
282+ print ("🚀 Starting SQLMap AI in ENHANCED mode (AI-Powered Mode)..." )
283+ print ("This mode includes AI-powered analysis, adaptive testing, and advanced features." )
284+ print ()
285+
286+ # Import and run enhanced version
287+ try :
288+ from sqlmap_ai .main import main
289+ # Pass the modified sys.argv to main
290+ main ()
291+ except ImportError as e :
292+ print (f"[ERROR] Enhanced mode failed to load: { e } " )
293+ print ("Falling back to simple mode..." )
294+ run_simple_mode ()
295+
296+ def main ():
297+ """Main entry point with mode selection"""
298+
299+ # Check for mode-specific help first
300+ if "--simple" in sys .argv :
301+ if "--help" in sys .argv or "-h" in sys .argv or len (sys .argv ) == 2 :
302+ show_simple_help ()
303+ return
304+
305+ if "--enhanced" in sys .argv :
306+ if "--help" in sys .argv or "-h" in sys .argv :
307+ show_enhanced_help ()
308+ return
309+
310+ # Check for general help
311+ if len (sys .argv ) == 1 or "--help" in sys .argv or "-h" in sys .argv :
312+ show_usage ()
313+ return
314+
315+ # Check for mode flags
316+ args = sys .argv [1 :]
317+
318+ # Remove mode flags from args for passing to main functions
319+ if "--simple" in args :
320+ args .remove ("--simple" )
321+ # Update sys.argv for the main function
322+ sys .argv = [sys .argv [0 ]] + args
323+ run_simple_mode ()
324+ elif "--enhanced" in args :
325+ args .remove ("--enhanced" )
326+ # Update sys.argv for the main function
327+ sys .argv = [sys .argv [0 ]] + args
328+ run_enhanced_mode ()
329+ else :
330+ # Default to enhanced mode
331+ run_enhanced_mode ()
332+
333+ if __name__ == "__main__" :
334+ try :
335+ main ()
336+ except KeyboardInterrupt :
337+ print ("\n [INFO] Scan interrupted by user" )
338+ sys .exit (1 )
339+ except Exception as e :
340+ print (f"\n [ERROR] Unexpected error: { e } " )
341+ print ("Try running with --help for usage information" )
342+ sys .exit (1 )
0 commit comments