-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
76 lines (68 loc) · 1.97 KB
/
scanner.go
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
package main
import (
"os/exec"
"github.com/sirupsen/logrus"
"strconv"
"encoding/json"
)
// Database handles the connection with a SQL Database
type Scanner struct {
config ScannerConfig
}
// NewDatabase creates a new instance of a database
func NewScanner(config ScannerConfig) *Scanner {
scan := &Scanner{
config: config,
}
if scan.config.Logger == nil {
scan.config.Logger = logrus.New()
}
return scan
}
func Enrichment(j Job, sensors Sensors) (enrichSensor string) {
logger := db.config.Logger
for i := 0; i < len(sensors.Sensors); i++ {
if j.Uuid == sensors.Sensors[i].Uuid {
json_struct, err := json.Marshal(sensors.Sensors[i])
if err != nil {
logger.Error("malformed namespace info in config file")
}
response := string(json_struct)
return response
}
}
return "{}"
}
func (scan *Scanner) StartScan(j Job, sensors Sensors) (pid int, err error) {
enrich := Enrichment(j, sensors)
broker := kafkaConfig.Broker
logger := db.config.Logger
logger.Info("Enrichment: ", enrich)
logger.Info("start scan for id ", j.Id)
logger.Info("kafka ", broker)
logger.Info("ports ", j.Ports)
logger.Info("target ", j.Target)
cmd := exec.Command(VulnerabilitiesScan,"-t",j.Target,"-p",j.Ports,"-s",strconv.Itoa(j.Jobid),"-k",broker,"-d", "-e", enrich)
err = cmd.Start()
if err != nil {
return 0, err
} else {
logger.Info("started new job with pid ", cmd.Process.Pid)
go cmd.Wait()
return cmd.Process.Pid, nil
}
}
func (scan *Scanner) CancelScan(job_pid int)(err error) {
job_pid_s := strconv.Itoa(job_pid)
logger.Warning("DANGEROUSLY KILLING SCAN WITH PKILL")
Kill := "/usr/bin/pkill"
cmd := exec.Command(Kill, "-P", job_pid_s)
err = cmd.Start()
if err == nil {
logger.Info("killing job with pid ", job_pid_s)
go cmd.Wait()
} else {
logger.Error("Error killing to cancel process: ", err)
}
return err
}