Implement /status API endpoint
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Yvan Janssens 2023-04-07 14:22:47 +02:00
parent 21d3278841
commit 8a61a52fa1
3 changed files with 35 additions and 26 deletions

View File

@ -11,15 +11,15 @@ func banner() {
fmt.Println("") fmt.Println("")
} }
func sanity_check() bool { func sanity_check() (bool, int) {
vName, error := scan_data([]byte("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")) 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 == "" { if error != nil && vName == "" {
panic(error) panic(error)
} }
if vName == "Win.Test.EICAR_HDB-1" { if vName == "Win.Test.EICAR_HDB-1" {
return true return true, sigNo
} }
return false return false, 0
} }
func main() { func main() {
@ -29,7 +29,8 @@ func main() {
go freshclam_update() go freshclam_update()
log.Println("Carrying out sanity checks...") log.Println("Carrying out sanity checks...")
if !sanity_check() { scannerIsSane, _ := sanity_check()
if !scannerIsSane {
log.Println("Sanity check failed!") log.Println("Sanity check failed!")
return return
} }

28
rest.go
View File

@ -1,17 +1,21 @@
package main package main
import "github.com/gin-gonic/gin" import (
"net/http"
"github.com/gin-gonic/gin"
)
type status_response struct { type status_response struct {
cvdVersion int `json:"cvd_version"` Scanning_engine string `json:"scanning_engine"`
sanity_check bool `json:"sanity_check"` Signature_count int `json:"signature_count"`
scanning_engine string `json:"scanning_engine"` Sanity_check bool `json:"sanity_check"`
} }
type scan_response struct { type scan_response struct {
malware_detected bool `json:"malware_detected"` Malware_detected bool `json:"malware_detected"`
malware_name string `json:"malware_name"` Malware_name string `json:"malware_name"`
engine status_response `json:"engine"` Engine status_response `json:"engine"`
} }
func scan_api(c *gin.Context) { func scan_api(c *gin.Context) {
@ -19,11 +23,17 @@ func scan_api(c *gin.Context) {
} }
func status_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() { func start_api() {
//gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
router := gin.Default() router := gin.Default()
router.PUT("/scan", scan_api) router.PUT("/scan", scan_api)

View File

@ -3,33 +3,31 @@ package main
import ( import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
clamav "git.cyber.gent/friedkiwi/go-clamav" clamav "git.cyber.gent/friedkiwi/go-clamav"
) )
func scan_data(data []byte) (string, error) { func scan_data(data []byte) (string, error, int) {
log.Println("scan_data(): scanning data...")
// write data out to file to be scanned // write data out to file to be scanned
tempFile, err := ioutil.TempFile("", "*.bin") tempFile, err := ioutil.TempFile("", "*.bin")
if err != nil { if err != nil {
return "", err return "", err, 0
} }
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
bytesWritten, writeErr := tempFile.Write(data) bytesWritten, writeErr := tempFile.Write(data)
if writeErr != nil { if writeErr != nil {
return "", writeErr return "", writeErr, 0
} }
if bytesWritten != len(data) { 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 { if err := tempFile.Close(); err != nil {
return "", err return "", err, 0
} }
// temporary file is now written to disk // temporary file is now written to disk
@ -44,22 +42,22 @@ func scan_data(data []byte) (string, error) {
}) })
if clamInitError != nil { if clamInitError != nil {
return "", clamInitError return "", clamInitError, 0
} }
// free clamav memory // free clamav memory
defer clamavInstance.Free() defer clamavInstance.Free()
// load db // 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 { if loadDbError != nil {
return "", err return "", err, 0
} }
// compile engine // compile engine
err = clamavInstance.CompileEngine() err = clamavInstance.CompileEngine()
if err != nil { if err != nil {
return "", err return "", err, 0
} }
clamavInstance.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40) clamavInstance.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40)
@ -68,5 +66,5 @@ func scan_data(data []byte) (string, error) {
// scan // scan
_, virusName, ret := clamavInstance.ScanFile(tempFile.Name()) _, virusName, ret := clamavInstance.ScanFile(tempFile.Name())
return virusName, ret return virusName, ret, int(sigNo)
} }