Skip to content

Commit b0ce0f5

Browse files
author
xorhex
committed
Handle file not found correctly for Malware Bazaar.
1 parent 03905ec commit b0ce0f5

4 files changed

Lines changed: 67 additions & 24 deletions

File tree

download.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type MalwareBazarQueryData struct {
5757
File_name string `json:"file_name"`
5858
}
5959

60+
type MalwareBazaarQueryStatus struct {
61+
Status string `json:"query_status"`
62+
}
63+
6064
type AssemblyLineQuery struct {
6165
Error_message string `json:"api_error_message"`
6266
Response *AssemblyLineQueryResponse `json:"api_response"`
@@ -958,33 +962,43 @@ func malwareBazaarDownload(uri string, api string, hash Hash, doNotExtract bool,
958962
fmt.Printf(" [!] Normally the response code: %s means that the provided URL %s needs a trailing slash (to avoid the redirect), but this already has a trailing slash.\nPlease file a bug report at https://github.com/xorhex/mlget/issues\n", response.Status, uri)
959963
}
960964
} else {
961-
fmt.Printf(" [!] %s\n", response.Status)
962-
}
963-
}
965+
byteValue, _ := io.ReadAll(response.Body)
964966

965-
err = writeToFile(response.Body, hash.Hash+".zip")
966-
if err != nil {
967-
fmt.Println(err)
968-
return false, ""
969-
}
967+
var data = MalwareBazaarQueryStatus{}
968+
error = json.Unmarshal(byteValue, &data)
970969

971-
fmt.Printf(" [+] Downloaded %s\n", hash.Hash+".zip")
972-
if doNotExtract {
973-
return true, hash.Hash + ".zip"
974-
} else {
975-
fmt.Println(" [-] Extracting...")
976-
files, err := extractPwdZip(hash.Hash+".zip", password, true, hash)
977-
if err != nil {
978-
fmt.Println(err)
979-
return false, ""
980-
} else {
981-
for _, f := range files {
982-
fmt.Printf(" [-] Extracted %s\n", f.Name)
970+
if error == nil {
971+
if data.Status == "file_not_found" {
972+
return false, ""
973+
}
974+
} else {
975+
err = writeToFile(io.NopCloser(bytes.NewReader(byteValue)), hash.Hash+".zip")
976+
if err != nil {
977+
fmt.Println(err)
978+
return false, ""
979+
}
980+
981+
fmt.Printf(" [+] Downloaded %s\n", hash.Hash+".zip")
982+
if doNotExtract {
983+
return true, hash.Hash + ".zip"
984+
} else {
985+
fmt.Println(" [-] Extracting...")
986+
files, err := extractPwdZip(hash.Hash+".zip", password, true, hash)
987+
if err != nil {
988+
fmt.Println(err)
989+
return false, ""
990+
} else {
991+
for _, f := range files {
992+
fmt.Printf(" [-] Extracted %s\n", f.Name)
993+
}
994+
}
995+
os.Remove(hash.Hash + ".zip")
996+
return true, hash.Hash
997+
}
983998
}
984999
}
985-
os.Remove(hash.Hash + ".zip")
986-
return true, hash.Hash
9871000
}
1001+
return false, ""
9881002
}
9891003

9901004
func filescanio(uri string, api string, hash Hash, doNotExtract bool, password string) (bool, string) {

mlget-test-config/samples.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,7 @@ test 22:
6363
hash: fc17c021f18ec73d1544ad46dde6a1f1949f126bf3e75f97e241f982e2b07c86
6464
test 23:
6565
name: TestVirusExchangeV2
66-
hash: 0cacdb88b24bd34b9d8ef600b06814f76206e60e70f975c8e4bdaa1ab7cebb80
66+
hash: 0cacdb88b24bd34b9d8ef600b06814f76206e60e70f975c8e4bdaa1ab7cebb80
67+
test 24:
68+
name: TestMalwareBazaarNotFound
69+
hash: fee889e9518d1c660bd6fa331c19aabada7eeff8f1c99f2ef4d64c662ed5805a

mlget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var uploadToAssemblyLineFlag bool
3434
var uploadToAssemblyLineAndDeleteFlag bool
3535
var forceResubmission bool
3636

37-
var version string = "3.4.2"
37+
var version string = "3.4.3"
3838

3939
func usage() {
4040
fmt.Println("mlget - A command line tool to download malware from a variety of sources")

mlget_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,32 @@ func TestMalwareBazaar(t *testing.T) {
509509
}
510510
}
511511

512+
func TestMalwareBazaarNotFound(t *testing.T) {
513+
home, _ := os.UserHomeDir()
514+
cfg, err := LoadConfig(path.Join(home, ".mlget.yml"))
515+
if err != nil {
516+
log.Fatal()
517+
t.Errorf("%v", err)
518+
}
519+
520+
scfg, err := parseTestConfig("./mlget-test-config/samples.yaml", t.Name())
521+
if err != nil {
522+
log.Fatal()
523+
t.Errorf("%v", err)
524+
}
525+
526+
ht, _ := hashType(scfg.Hash)
527+
hash := Hash{HashType: ht, Hash: scfg.Hash}
528+
529+
var osq ObjectiveSeeQuery
530+
result, filename, _ := MalwareBazaar.QueryAndDownload(cfg, hash, false, osq)
531+
532+
if result {
533+
os.Remove(filename)
534+
t.Errorf("MalwareBazaar failed")
535+
}
536+
}
537+
512538
func TestMalpedia(t *testing.T) {
513539
home, _ := os.UserHomeDir()
514540
cfg, err := LoadConfig(path.Join(home, ".mlget.yml"))

0 commit comments

Comments
 (0)