From 8a61a52fa1e3471183c9ff1430fc2793de01372e Mon Sep 17 00:00:00 2001 From: Yvan Janssens Date: Fri, 7 Apr 2023 14:22:47 +0200 Subject: [PATCH] Implement /status API endpoint --- malscan.go | 11 ++++++----- rest.go | 28 +++++++++++++++++++--------- scanner.go | 22 ++++++++++------------ 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/malscan.go b/malscan.go index 772cba5..5c2a35e 100644 --- a/malscan.go +++ b/malscan.go @@ -11,15 +11,15 @@ func banner() { fmt.Println("") } -func sanity_check() bool { - vName, error := scan_data([]byte("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")) +func sanity_check() (bool, int) { + vName, error, sigNo := scan_data([]byte("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")) if error != nil && vName == "" { panic(error) } if vName == "Win.Test.EICAR_HDB-1" { - return true + return true, sigNo } - return false + return false, 0 } func main() { @@ -29,7 +29,8 @@ func main() { go freshclam_update() log.Println("Carrying out sanity checks...") - if !sanity_check() { + scannerIsSane, _ := sanity_check() + if !scannerIsSane { log.Println("Sanity check failed!") return } diff --git a/rest.go b/rest.go index 69dcc29..33e703a 100644 --- a/rest.go +++ b/rest.go @@ -1,17 +1,21 @@ package main -import "github.com/gin-gonic/gin" +import ( + "net/http" + + "github.com/gin-gonic/gin" +) type status_response struct { - cvdVersion int `json:"cvd_version"` - sanity_check bool `json:"sanity_check"` - scanning_engine string `json:"scanning_engine"` + Scanning_engine string `json:"scanning_engine"` + Signature_count int `json:"signature_count"` + Sanity_check bool `json:"sanity_check"` } type scan_response struct { - malware_detected bool `json:"malware_detected"` - malware_name string `json:"malware_name"` - engine status_response `json:"engine"` + Malware_detected bool `json:"malware_detected"` + Malware_name string `json:"malware_name"` + Engine status_response `json:"engine"` } func scan_api(c *gin.Context) { @@ -19,11 +23,17 @@ func scan_api(c *gin.Context) { } func status_api(c *gin.Context) { - + scannerIsSane, sigCount := sanity_check() + responseData := status_response{ + Sanity_check: scannerIsSane, + Signature_count: sigCount, + Scanning_engine: "clamav", + } + c.IndentedJSON(http.StatusOK, responseData) } func start_api() { - //gin.SetMode(gin.ReleaseMode) + gin.SetMode(gin.ReleaseMode) router := gin.Default() router.PUT("/scan", scan_api) diff --git a/scanner.go b/scanner.go index e129d36..7f0d873 100644 --- a/scanner.go +++ b/scanner.go @@ -3,33 +3,31 @@ package main import ( "errors" "io/ioutil" - "log" "os" clamav "git.cyber.gent/friedkiwi/go-clamav" ) -func scan_data(data []byte) (string, error) { - log.Println("scan_data(): scanning data...") +func scan_data(data []byte) (string, error, int) { // write data out to file to be scanned tempFile, err := ioutil.TempFile("", "*.bin") if err != nil { - return "", err + return "", err, 0 } defer os.Remove(tempFile.Name()) bytesWritten, writeErr := tempFile.Write(data) if writeErr != nil { - return "", writeErr + return "", writeErr, 0 } if bytesWritten != len(data) { - return "", errors.New("scan_data(): bytesWritten!= len(data)") + return "", errors.New("scan_data(): bytesWritten!= len(data)"), 0 } if err := tempFile.Close(); err != nil { - return "", err + return "", err, 0 } // temporary file is now written to disk @@ -44,22 +42,22 @@ func scan_data(data []byte) (string, error) { }) if clamInitError != nil { - return "", clamInitError + return "", clamInitError, 0 } // free clamav memory defer clamavInstance.Free() // load db - _, loadDbError := clamavInstance.LoadDB("/usr/local/share/clamav", uint(clamav.CL_DB_DIRECTORY)) + sigNo, loadDbError := clamavInstance.LoadDB("/usr/local/share/clamav", uint(clamav.CL_DB_DIRECTORY)) if loadDbError != nil { - return "", err + return "", err, 0 } // compile engine err = clamavInstance.CompileEngine() if err != nil { - return "", err + return "", err, 0 } clamavInstance.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40) @@ -68,5 +66,5 @@ func scan_data(data []byte) (string, error) { // scan _, virusName, ret := clamavInstance.ScanFile(tempFile.Name()) - return virusName, ret + return virusName, ret, int(sigNo) }