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
|
package fetcher
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/cheggaaa/pb"
"github.com/gocarina/gocsv"
"github.com/inconshreveable/log15"
"github.com/mozqnet/go-exploitdb/extractor"
"github.com/mozqnet/go-exploitdb/models"
"github.com/mozqnet/go-exploitdb/util"
"github.com/pkg/errors"
"golang.org/x/net/html/charset"
)
// FetchExploitDB :
func FetchExploitDB(deep bool) (exploits []models.Exploit, err error) {
var exploitCvesMap map[string][]string
if exploitCvesMap, err = FetchExploitCvesMap(deep); err != nil {
return nil, err
}
var exploitShellCodeMap map[string]*models.ShellCode
if exploitShellCodeMap, err = FetchExploitShellCodeMap(); err != nil {
return nil, err
}
var exploitPaperMap map[string]*models.Paper
if exploitPaperMap, err = FetchExploitPaperMap(); err != nil {
return nil, err
}
var exploitDocMap map[string]*models.Document
if exploitDocMap, err = FetchExploitDocumentMap(); err != nil {
return nil, err
}
// append exploit db ids
uniqExploitDBIDs := map[string]struct{}{}
for id := range exploitCvesMap {
uniqExploitDBIDs[id] = struct{}{}
}
for id := range exploitShellCodeMap {
uniqExploitDBIDs[id] = struct{}{}
}
for id := range exploitPaperMap {
uniqExploitDBIDs[id] = struct{}{}
}
for id := range exploitDocMap {
uniqExploitDBIDs[id] = struct{}{}
}
for id := range uniqExploitDBIDs {
cveIDs, ok := exploitCvesMap[id]
if ok {
for _, cveID := range cveIDs {
var description string
if e, ok := exploitPaperMap[id]; ok {
description = e.Description
}
if e, ok := exploitShellCodeMap[id]; ok {
description = e.Description
}
if e, ok := exploitDocMap[id]; ok {
description = e.Description
}
if len(description) == 0 {
continue
}
exploit := models.Exploit{
ExploitUniqueID: id,
ExploitType: models.OffensiveSecurityType,
URL: "https://www.exploit-db.com/exploits/" + id,
CveID: cveID,
Description: description,
OffensiveSecurity: &models.OffensiveSecurity{
ExploitUniqueID: id,
Document: exploitDocMap[id],
ShellCode: exploitShellCodeMap[id],
Paper: exploitPaperMap[id],
},
}
exploits = append(exploits, exploit)
}
} else {
// No CveID
var description string
if e, ok := exploitPaperMap[id]; ok {
description = e.Description
}
if e, ok := exploitShellCodeMap[id]; ok {
description = e.Description
}
if e, ok := exploitDocMap[id]; ok {
description = e.Description
}
if len(description) == 0 {
continue
}
exploit := models.Exploit{
ExploitUniqueID: id,
ExploitType: models.OffensiveSecurityType,
URL: "https://www.exploit-db.com/exploits/" + id,
Description: description,
OffensiveSecurity: &models.OffensiveSecurity{
Document: exploitDocMap[id],
ShellCode: exploitShellCodeMap[id],
Paper: exploitPaperMap[id],
},
}
exploits = append(exploits, exploit)
}
}
return exploits, nil
}
// FetchExploitCvesMap :
func FetchExploitCvesMap(deep bool) (exploitCveMap map[string][]string, err error) {
exploitCveMap = map[string][]string{}
for year := 1999; year <= time.Now().Year(); year++ {
url := fmt.Sprintf("http://cve.mitre.org/data/downloads/allitems-cvrf-year-%d.xml", year)
log15.Info("Fetching", "URL", url)
cveXML, err := util.FetchURL(url)
if err != nil {
return nil,
errors.Wrapf(err, "Failed to fetch cve data from Mitre. targetURL: %s", url)
}
var mitreCve models.MitreXML
// https://stackoverflow.com/questions/6002619/unmarshal-an-iso-8859-1-xml-input-in-go
decoder := xml.NewDecoder(bytes.NewReader(cveXML))
decoder.CharsetReader = charset.NewReaderLabel
if err = decoder.Decode(&mitreCve); err != nil {
return nil, errors.Wrap(err, "Failed to Unmarshal XML")
}
for _, vuln := range mitreCve.Vulnerability {
for _, ref := range vuln.References {
desc := strings.Split(ref.Description, ":")
if len(desc) != 2 {
continue
}
refType, exploitID := desc[0], desc[1]
// https://cve.mitre.org/data/refs/index.html
if refType != "EXPLOIT-DB" {
continue
}
exploitCveMap[exploitID] = append(exploitCveMap[exploitID], vuln.CVE)
}
}
}
if !deep {
return exploitCveMap, nil
}
// https://github.com/offensive-security/exploitdb/search?q=CVE&unscoped_q=CVE
// over 500 count
heavyExts := []string{
"txt",
"rb",
"py",
}
// under 500 count
lightExts := []string{
"c",
"html",
"pl",
"pm",
"sh",
"md",
"php",
"cpp",
"java",
"go",
"cs",
"nse",
"asm",
"sql",
}
for year := 1999; year <= time.Now().Year(); year++ {
log15.Info("Fetching Github Sources", "year", year)
if exploitCveMap, err = SearchGithubCodeWithExt(heavyExts, year, exploitCveMap); err != nil {
return nil, err
}
}
if exploitCveMap, err = SearchGithubCodeWithExt(lightExts, 0, exploitCveMap); err != nil {
return nil, err
}
return exploitCveMap, nil
}
// SearchGithubCodeWithExt :
func SearchGithubCodeWithExt(exts []string, year int, exploitCveMap map[string][]string) (newExploitCveMap map[string][]string, err error) {
if exploitCveMap == nil {
exploitCveMap = map[string][]string{}
}
for _, ext := range exts {
page := 1
// max request of search code is 1000
maxPage := 10
for {
if maxPage < page {
break
}
// https://developer.github.com/v3/search/#search-code
var url string
if year == 0 {
url = fmt.Sprintf("https://api.github.com/search/code?q=CVE+extension:%s+repo:offensive-security/exploitdb&page=%d&per_page=100&sort=indexed&order=desc", ext, page)
} else {
spaceCode := "%20"
url = fmt.Sprintf("https://api.github.com/search/code?q=CVE%s%d+extension:%s+repo:offensive-security/exploitdb&page=%d&per_page=100&sort=indexed&order=desc", spaceCode, year, ext, page)
}
log15.Info("Fetching", "URL", url)
githubJSON, err := util.FetchURL(url)
if err != nil {
log15.Warn("Failed to fetch cve data from Github.", "url", url)
break
}
var github models.GithubJSON
if err = json.Unmarshal(githubJSON, &github); err != nil {
return nil, err
}
bar := pb.StartNew(len(github.Items))
// for github rate limit
if 1000 < len(github.Items) {
log15.Warn("More than 1000 data can not be acquired due to rate limit of github")
}
if len(github.Items) < 30 {
log15.Info("Sleep 10 seconds for github rate limit")
time.Sleep(10 * time.Second)
}
for _, item := range github.Items {
path := "https://raw.githubusercontent.com/offensive-security/exploitdb/master/" + item.Path
exploitDBID := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
if _, ok := exploitCveMap[exploitDBID]; !ok {
doc, err := util.FetchURL(path)
if err != nil {
return nil, err
}
exploitCveMap[exploitDBID] = extractor.ExtractCveID(doc)
}
bar.Increment()
}
bar.Finish()
if page == 1 {
totalPageSize := (github.TotalCount / 100) + 1
if totalPageSize < maxPage {
maxPage = totalPageSize
}
}
page++
}
}
return exploitCveMap, nil
}
// FetchExploitShellCodeMap :
func FetchExploitShellCodeMap() (exploitShellCodeMap map[string]*models.ShellCode, err error) {
exploitShellCodeMap = map[string]*models.ShellCode{}
url := "https://raw.githubusercontent.com/offensive-security/exploitdb/master/files_shellcodes.csv"
log15.Info("Fetching", "URL", url)
cveCsv, err := util.FetchURL(url)
shellCodes := []*models.ShellCode{}
if err := gocsv.UnmarshalBytes(cveCsv, &shellCodes); err != nil {
return nil, err
}
for _, shellCode := range shellCodes {
shellCode.ShellCodeURL = "https://github.com/offensive-security/exploitdb/" + shellCode.ShellCodeURL
exploitShellCodeMap[shellCode.ExploitUniqueID] = shellCode
}
return exploitShellCodeMap, nil
}
// FetchExploitPaperMap :
func FetchExploitPaperMap() (exploitPaperMap map[string]*models.Paper, err error) {
exploitPaperMap = map[string]*models.Paper{}
url := "https://raw.githubusercontent.com/offensive-security/exploitdb-papers/master/files_papers.csv"
log15.Info("Fetching", "URL", url)
cveCsv, err := util.FetchURL(url)
papers := []*models.Paper{}
if err := gocsv.UnmarshalBytes(cveCsv, &papers); err != nil {
return nil, err
}
for _, paper := range papers {
paper.PaperURL = "https://github.com/offensive-security/exploitdb-papers/" + paper.PaperURL
exploitPaperMap[paper.ExploitUniqueID] = paper
}
return exploitPaperMap, nil
}
// FetchExploitDocumentMap :
func FetchExploitDocumentMap() (exploitDocMap map[string]*models.Document, err error) {
exploitDocMap = map[string]*models.Document{}
url := "https://raw.githubusercontent.com/offensive-security/exploitdb/master/files_exploits.csv"
log15.Info("Fetching", "URL", url)
cveCsv, err := util.FetchURL(url)
docs := []*models.Document{}
if err := gocsv.UnmarshalBytes(cveCsv, &docs); err != nil {
return nil, err
}
for _, doc := range docs {
doc.DocumentURL = "https://github.com/offensive-security/exploitdb/" + doc.DocumentURL
exploitDocMap[doc.ExploitUniqueID] = doc
}
return exploitDocMap, nil
}
|