Skip to content

Commit

Permalink
update file scan to properly upload file with forms params
Browse files Browse the repository at this point in the history
  • Loading branch information
LordNoteworthy committed Jun 14, 2024
1 parent b2a0d85 commit 933eb20
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 67 deletions.
12 changes: 7 additions & 5 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ var deleteCmd = &cobra.Command{
Long: `Delete a binary sample given a sha256.`,
Run: func(cmd *cobra.Command, args []string) {

svc := webapi.New(cfg.Credentials.URL)

var token string
var sto s.Storage
var err error

if delFromDB {
// Authenticate to Saferwall web service.
token, err = webapi.Login(cfg.Credentials.URL, cfg.Credentials.Username, cfg.Credentials.Password)
token, err = svc.Login(cfg.Credentials.Username, cfg.Credentials.Password)
if err != nil {
log.Fatalf("failed to login to saferwall web service")
}
Expand Down Expand Up @@ -76,7 +78,7 @@ var deleteCmd = &cobra.Command{

// Delete a single binary.
if sha256Flag != "" {
delete(sha256Flag, token, sto)
delete(svc, sha256Flag, token, sto)
} else if txtFlag != "" {
// Delete a list of sha256 hashes.
data, err := util.ReadAll(txtFlag)
Expand All @@ -87,19 +89,19 @@ var deleteCmd = &cobra.Command{
sha256list := strings.Split(string(data), "\n")
for _, sha256 := range sha256list {
if len(sha256) >= 64 {
delete(sha256, token, sto)
delete(svc, sha256, token, sto)
}
}
}
},
}

func delete(sha256, token string, sto s.Storage) error {
func delete(svc webapi.Service, sha256, token string, sto s.Storage) error {

log.Printf("deleting %s", sha256)

if token != "" {
err := webapi.Delete(sha256, token)
err := svc.Delete(sha256, token)
if err != nil {
log.Fatalf("failed to delete %s, err: %v", sha256, err)
return err
Expand Down
12 changes: 7 additions & 5 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ var downloadCmd = &cobra.Command{
var sto s.Storage
var err error

webSvc := webapi.New(cfg.Credentials.URL)

if useWebAPIs {
// Authenticate to Saferwall web service.
token, err = webapi.Login(cfg.Credentials.URL, cfg.Credentials.Username, cfg.Credentials.Password)
token, err = webSvc.Login(cfg.Credentials.Username, cfg.Credentials.Password)
if err != nil {
log.Fatalf("failed to login to saferwall web service")
}
Expand Down Expand Up @@ -89,7 +91,7 @@ var downloadCmd = &cobra.Command{

// download a single binary.
if sha256Flag != "" {
download(sha256Flag, token, sto)
download(sha256Flag, token, webSvc, sto)
} else if txtFlag != "" {
// Download a list of sha256 hashes.
data, err := util.ReadAll(txtFlag)
Expand All @@ -100,7 +102,7 @@ var downloadCmd = &cobra.Command{
sha256list := strings.Split(string(data), "\n")
for _, sha256 := range sha256list {
if len(sha256) >= 64 {
err = download(sha256, token, sto)
err = download(sha256, token, webSvc, sto)
if err != nil {
log.Fatalf("failed to download sample (%s): %v", sha256, err)
}
Expand All @@ -116,7 +118,7 @@ var downloadCmd = &cobra.Command{
},
}

func download(sha256, token string, sto s.Storage) error {
func download(sha256, token string, web webapi.Service, sto s.Storage) error {

var err error
var data bytes.Buffer
Expand All @@ -125,7 +127,7 @@ func download(sha256, token string, sto s.Storage) error {
log.Printf("downloading %s to %s", sha256, outputFlag)

if token != "" {
dataContent, err := webapi.Download(sha256, token)
dataContent, err := web.Download(sha256, token)
if err != nil {
log.Fatalf("failed to download %s, err: %v", sha256, err)
return err
Expand Down
11 changes: 6 additions & 5 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ var listUsersCmd = &cobra.Command{
Long: `Paginate over the list of users in DB or in S3.`,
Run: func(cmd *cobra.Command, args []string) {

token, err := webapi.Login(cfg.Credentials.URL, cfg.Credentials.Username, cfg.Credentials.Password)
webSvc := webapi.New(cfg.Credentials.URL)
token, err := webSvc.Login(cfg.Credentials.Username, cfg.Credentials.Password)
if err != nil {
log.Fatalf("failed to login to saferwall web service")
}
Expand All @@ -70,14 +71,14 @@ var listFilesCmd = &cobra.Command{
if walkDBFlag {

var fileContent bytes.Buffer

token, err := webapi.Login(cfg.Credentials.URL, cfg.Credentials.Username, cfg.Credentials.Password)
webSvc := webapi.New(cfg.Credentials.URL)
token, err := webSvc.Login(cfg.Credentials.Username, cfg.Credentials.Password)
if err != nil {
log.Fatalf("failed to login to saferwall web service")
}

// Do an initial call to get the page's count.
pages, err := webapi.ListFiles(token, 1)
pages, err := webSvc.ListFiles(token, 1)
if err != nil {
log.Fatalf("failed to list files: %v", err)
}
Expand All @@ -87,7 +88,7 @@ var listFilesCmd = &cobra.Command{
for page := 1; page <= pages.PageCount; page++ {

log.Printf("getting files for page: %d", page)
results, err := webapi.ListFiles(token, page)
results, err := webSvc.ListFiles(token, page)
if err != nil {
log.Fatalf("failed to list files: %v", err)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/rescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}

// reScanFile re-scans a list of SHA256.
func reScanFile(shaList []string, token string) error {
func reScanFile(web webapi.Service, shaList []string, token string) error {

if asyncScanFlag {
// Create a worker pool
Expand All @@ -46,7 +46,7 @@ func reScanFile(shaList []string, token string) error {
for _, sha256 := range shaList {
wp.Submit(func() {
log.Printf("rescanning %s", sha256)
err := webapi.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
err := web.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
if err != nil {
log.Fatalf("failed to rescan file: %v", sha256)
}
Expand All @@ -62,7 +62,7 @@ func reScanFile(shaList []string, token string) error {
for _, sha256 := range shaList {

log.Printf("re-scanning %s", sha256)
err := webapi.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
err := web.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
if err != nil {
log.Fatalf("failed to rescan file: %v", sha256)
}
Expand All @@ -83,7 +83,8 @@ var reScanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

// Login to saferwall web service
token, err := webapi.Login(cfg.Credentials.URL, cfg.Credentials.Username, cfg.Credentials.Password)
webSvc := webapi.New(cfg.Credentials.URL)
token, err := webSvc.Login(cfg.Credentials.Username, cfg.Credentials.Password)
if err != nil {
log.Fatalf("failed to login to saferwall web service")
}
Expand All @@ -101,6 +102,6 @@ var reScanCmd = &cobra.Command{
sha256List = append(sha256List, fileHash)
}

reScanFile(sha256List, token)
reScanFile(webSvc, sha256List, token)
},
}
19 changes: 10 additions & 9 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
}

// scanFile scans an individual file or a directory.
func scanFile(filePath, token string) error {
func scanFile(web webapi.Service, filePath, token string) error {

_, err := os.Stat(filePath)
if os.IsNotExist(err) {
Expand Down Expand Up @@ -79,21 +79,21 @@ func scanFile(filePath, token string) error {
sha256 := util.GetSha256(data)

// Check if we the file exists in the DB.
exists, err := webapi.FileExists(sha256)
exists, err := web.FileExists(sha256)
if err != nil {
log.Fatalf("failed to check existence of file: %v", filename)
}

// Upload the file to be scanned, this will automatically trigger a scan request.
if !exists {
_, err = webapi.Scan(filename, token, osFlag, skipDetonationFlag, timeoutFlag)
_, err = web.Scan(filename, token, osFlag, skipDetonationFlag, timeoutFlag)
if err != nil {
log.Fatalf("failed to upload file: %v", filename)
}
} else {
// Force rescan the file
if forceRescanFlag {
err = webapi.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
err = web.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
if err != nil {
log.Fatalf("failed to rescan file: %v", filename)
}
Expand All @@ -119,15 +119,15 @@ func scanFile(filePath, token string) error {
log.Printf("processing %s", sha256)

// Check if we the file exists in the DB.
exists, err := webapi.FileExists(sha256)
exists, err := web.FileExists(sha256)
if err != nil {
log.Fatalf("failed to check existence of file: %s, error: %v", filename, err)
}

// Upload the file to be scanned, this will automatically
// trigger a scan request.
if !exists {
body, err := webapi.Scan(filename, token, osFlag, skipDetonationFlag, timeoutFlag)
body, err := web.Scan(filename, token, osFlag, skipDetonationFlag, timeoutFlag)
if err != nil {
log.Fatalf("failed to upload file: %s, error: %v", filename, err)
}
Expand All @@ -136,7 +136,7 @@ func scanFile(filePath, token string) error {
} else {
// Force re-scan the file
if forceRescanFlag {
err = webapi.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
err = web.Rescan(sha256, token, osFlag, skipDetonationFlag, timeoutFlag)
if err != nil {
log.Fatalf("failed to re-scan file: %v", filename)
}
Expand All @@ -155,11 +155,12 @@ var scanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

// login to saferwall web service
token, err := webapi.Login(cfg.Credentials.URL, cfg.Credentials.Username, cfg.Credentials.Password)
webSvc := webapi.New(cfg.Credentials.URL)
token, err := webSvc.Login(cfg.Credentials.Username, cfg.Credentials.Password)
if err != nil {
log.Fatalf("failed to login to saferwall web service")
}

scanFile(filePath, token)
scanFile(webSvc, filePath, token)
},
}
8 changes: 6 additions & 2 deletions cmd/souk.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func init() {
func addFamilyToSouk(familyYamlPath string) error {
log.Printf("processing %s", familyYamlPath)

webSvc := webapi.New(cfg.Credentials.URL)

familyData, err := util.ReadAll(familyYamlPath)
if err != nil {
log.Fatalf("failed to read yaml file, err: %v ", err)
Expand Down Expand Up @@ -112,7 +114,7 @@ func addFamilyToSouk(familyYamlPath string) error {
log.Printf("processing %s | %s | %s | %s",
sample.SHA256, sample.Platform, sample.FileFormat, sample.Category)

err = webapi.GetFile(sample.SHA256, &file)
err = webSvc.GetFile(sample.SHA256, &file)
if err != nil {
log.Fatalf("failed to read doc from saferwall web service: %v", err)
}
Expand Down Expand Up @@ -178,6 +180,8 @@ func generateMalwareSoukDB() error {
initMalwareSouk()
}

webSvc := webapi.New(cfg.Credentials.URL)

yamlCorpus, err := loadCorpus(soukFlag)
if err != nil {
log.Fatalf("failed to load corpus, err: %v ", err)
Expand Down Expand Up @@ -209,7 +213,7 @@ func generateMalwareSoukDB() error {
log.Printf("processing %s | %s | %s | %s",
sample.SHA256, sample.Platform, sample.FileFormat, sample.Category)

err = webapi.GetFile(sample.SHA256, &file)
err = webSvc.GetFile(sample.SHA256, &file)
if err != nil {
log.Fatalf("failed to read doc from saferwall web service: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/webapi/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ type Pages struct {
Items interface{} `json:"items"`
}

func Login(url, username, password string) (string, error) {
func (s Service) Login(username, password string) (string, error) {

Check failure on line 25 in internal/webapi/auth.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.19.x, ubuntu-latest)

undefined: Service

Check failure on line 25 in internal/webapi/auth.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.19.x, macos-latest)

undefined: Service

Check failure on line 25 in internal/webapi/auth.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.20.x, ubuntu-latest)

undefined: Service

Check failure on line 25 in internal/webapi/auth.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.20.x, macos-latest)

undefined: Service

authURL := url + "/v1/auth/login/"
requestBody, err := json.Marshal(map[string]string{
"username": username,
"password": password,
Expand All @@ -38,7 +37,7 @@ func Login(url, username, password string) (string, error) {
Timeout: timeout,
}
body := bytes.NewBuffer(requestBody)
request, err := http.NewRequest(http.MethodPost, authURL, body)
request, err := http.NewRequest(http.MethodPost, s.authURL, body)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 933eb20

Please sign in to comment.