-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
239 lines (200 loc) · 7.54 KB
/
main.go
File metadata and controls
239 lines (200 loc) · 7.54 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
package main
import (
"encoding/csv"
"flag"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
)
type Exploit struct {
ID string `json:"id"`
File string `json:"file"`
Description string `json:"description"`
DatePublished string `json:"date_published"` // Changed from DatePublished
Author string `json:"author"`
Type string `json:"type"`
Platform string `json:"platform"`
Port string `json:"port"`
DateAdded string `json:"date_added"` // Changed from DateAdded
DateUpdated string `json:"date_updated"` // Changed from DateUpdated
Verified string `json:"verified"`
Codes string `json:"codes"`
Tags string `json:"tags"`
Aliases string `json:"aliases"`
ScreenshotURL string `json:"screenshot_url"` // Changed from ScreenshotURL
ApplicationURL string `json:"application_url"` // Changed from ApplicationURL
SourceURL string `json:"source_url"` // Changed from SourceURL
}
type SearchResult struct {
Exploits []Exploit `json:"exploits"`
}
var exploits []Exploit
var shellcodes []Exploit
func loadCSVData(filename string) ([]Exploit, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
var exploits []Exploit
for _, record := range records[1:] {
if len(record) >= 17 {
exploit := Exploit{
ID: record[0],
File: record[1],
Description: record[2],
DatePublished: record[3],
Author: record[4],
Type: record[5],
Platform: record[6],
Port: record[7],
DateAdded: record[8],
DateUpdated: record[9],
Verified: record[10],
Codes: record[11],
Tags: record[12],
Aliases: record[13],
ScreenshotURL: record[14],
ApplicationURL: record[15],
SourceURL: record[16],
}
exploits = append(exploits, exploit)
}
}
log.Printf("Loaded %d records from %s", len(exploits), filename)
return exploits, nil
}
func fuzzySearch(query string, exploits []Exploit) []Exploit {
var results []Exploit
// Split query into terms and convert to lowercase
terms := strings.Fields(strings.ToLower(query))
log.Printf("Searching through %d exploits with terms: %v", len(exploits), terms)
for _, exploit := range exploits {
// Convert fields to lowercase once for comparison
descLower := strings.ToLower(exploit.Description)
fileLower := strings.ToLower(exploit.File)
tagsLower := strings.ToLower(exploit.Tags)
// Check if ALL terms match
allTermsMatch := true
for _, term := range terms {
termMatches := strings.Contains(descLower, term) ||
strings.Contains(fileLower, term) ||
strings.Contains(tagsLower, term)
if !termMatches {
allTermsMatch = false
break
}
}
if allTermsMatch {
results = append(results, exploit)
log.Printf("Found match: %s", exploit.Description)
}
}
log.Printf("Found %d results in fuzzySearch", len(results))
return results
}
func getExecutablePath() string {
execPath, err := os.Executable()
if err != nil {
log.Fatal("Could not get executable path:", err)
}
// Resolve symlink to get the real path
realPath, err := filepath.EvalSymlinks(execPath)
if err != nil {
log.Fatal("Could not resolve executable symlink:", err)
}
dirPath := filepath.Dir(realPath)
log.Printf("Loading files from: %s", dirPath) // Debug line
return dirPath
}
func main() {
port := flag.String("port", "7777", "Port to run the server on")
flag.Parse()
// Get the directory where the executable is located and change to it
execPath := getExecutablePath()
err := os.Chdir(execPath)
if err != nil {
log.Fatal("Could not change to executable directory:", err)
}
// Load the exploits database
exploits, err = loadCSVData("exploitdb/files_exploits.csv")
if err != nil {
log.Fatal(err)
}
shellcodes, err = loadCSVData("exploitdb/files_shellcodes.csv")
if err != nil {
log.Fatal(err)
}
total := len(exploits) + len(shellcodes)
log.Printf("Loaded %d records from exploitdb/files_exploits.csv", len(exploits))
log.Printf("Loaded %d records from exploitdb/files_shellcodes.csv", len(shellcodes))
log.Printf("Total records loaded: %d", total)
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
router.GET("/search", func(c *gin.Context) {
log.Printf("Search handler called")
query := c.Query("q")
log.Printf("Received search query: %s", query)
allRecords := append(exploits, shellcodes...)
log.Printf("Total records to search: %d", len(allRecords))
results := fuzzySearch(query, allRecords)
log.Printf("Found %d results for query: %s", len(results), query)
// Format results for display - updated field names to match frontend
formattedResults := make([]gin.H, 0)
for _, result := range results {
formattedResults = append(formattedResults, gin.H{
"ID": result.ID,
"title": result.Description, // Description shows as title
"File": result.File, // Keep original File for links
"DatePublished": result.DatePublished,
"Author": result.Author,
"Type": result.Type,
"Platform": result.Platform,
"Verified": result.Verified,
"Tags": result.Tags,
})
}
response := gin.H{
"exploits": formattedResults,
"count": len(formattedResults),
"query": query,
}
log.Printf("Sending response with %d results", len(formattedResults))
c.JSON(http.StatusOK, response)
})
router.GET("/exploit/*filepath", func(c *gin.Context) {
filepath := c.Param("filepath")
filepath = strings.TrimPrefix(filepath, "/")
filePath := "exploitdb/" + filepath
content, err := os.ReadFile(filePath)
if err != nil {
c.String(http.StatusNotFound, "Exploit not found")
return
}
c.String(http.StatusOK, string(content))
})
router.GET("/download/*filepath", func(c *gin.Context) {
filepath := c.Param("filepath")
filepath = strings.TrimPrefix(filepath, "/")
filePath := "exploitdb/" + filepath
// Check if file exists
if _, err := os.Stat(filePath); err == nil {
c.File(filePath)
return
}
c.String(http.StatusNotFound, "Exploit not found")
})
log.Printf("Starting server on http://localhost:%s", *port)
router.Run(":" + *port)
}