opensnitch/daemon/tasks/load.go
Gustavo Iñiguez Goia 1673856037
tasks: added IOC scanner
Preliminary version of a task for scanning IOCs in background
(Indicator Of Compromise).

It can execute scripts and commands in background periodically.

 - Added a new task scheduler to configure when the task is launched.
   Previously we only had an "Interval" field to run the task
periodically. Now it's possible to select the day of the week, specify
multiple times, hours or minutes.

        "schedule": [
            {
                "weekday": [5,6],
                "time": ["09:55:00", "20:15:20"],
            },
            {
                "weekday": [0,1,2,3,4],
                "time": ["15:00:00"],
            }
        ],
   Used only in this task for now.

 - This task supports 4 "tools": yara, scripts and debsums/dpkg.

   * The "yara" tool acts as a frontend to the yara cli tool, used to scan
     for IOCs with YARA rules.
   * The "debsums"/"dpkg" tools can be used to:
     - report changes in the MD5 checksums of installed Debian
       packages (debsums -c).
     - report changes in configuration files of installed Debian
       packages (debsums -ce).
     - verify the integrity of Debian packages (dpkg --verify, dpkg
       --verify <package1> <packag2>).
       The output of the format (rpm) is not parsed yet.

Configuration example of the task to report the output of the command
debsums:
{
  "name": "IOC-scanner",
  "data": {
     "interval": "15s",
     "schedule": [
         {
             "weekday": [0,1,2,3,4,5,6],
             "time": ["22:00:00"]
         }
     ],
     "tools": [
     {
         "name": "debsums",
         "msgStart": "IOC scanner debsums started",
         "msgEnd": "IOC scanner debsums finished",
         "enabled": false,
         "cmd": ["debsums", "-c"],
         "options": {
             "dirs": [],
             "files": [],
             "reports": {
                 "path": "/etc/opensnitchd/tasks/iocscanner/reports",
                 "format": ""
             }
         }
     }
   ]
}

The field "name" defines which tool to use. It's also used to parse and
format the output of the specified command.

The output of the command is sent to the GUI as an alert, and the
results can be reviewed under the Rules tab -> Alerts.
2025-09-22 00:11:28 +02:00

141 lines
4.1 KiB
Go

package tasks
import (
//"fmt"
"github.com/evilsocket/opensnitch/daemon/log"
"github.com/evilsocket/opensnitch/daemon/tasks/config"
"github.com/evilsocket/opensnitch/daemon/tasks/downloader"
"github.com/evilsocket/opensnitch/daemon/tasks/iocscanner"
"github.com/evilsocket/opensnitch/daemon/tasks/looptask"
//"github.com/evilsocket/opensnitch/daemon/tasks/netsniffer"
)
func (tm *TaskManager) ReloadTaskFile(cfgfile string) error {
bakFile := tm.loader.CfgFile
err := tm.LoadTaskFile(cfgfile)
if err != nil {
tm.loader.CfgFile = bakFile
log.Debug("[tasks] ReloadTaskFile: %s", err)
return err
}
return nil
}
// LoadTaskFile loads the file where all the tasks are defined.
// This file is configurable, saved under TaskManager.cfgFile, and if it's
// not supplied, we'll try to use /etc/opensnitchd/tasks/tasks.json
func (tm *TaskManager) LoadTaskFile(cfgfile string) error {
/*tm.cfgFile = cfgfile
if !core.Exists(cfgfile) {
log.Warning("[Tasks] tasks config file does not exist: %s", cfgfile)
return fmt.Errorf("%s doesn't exist", cfgfile)
}*/
tasks, err := tm.loader.Load(cfgfile)
if err != nil {
//return fmt.Errorf("error loading tasks from %s: %s", cfgfile, err)
log.Warning("[tasks] LoadTaskFile, error loading tasks (%s), %s", cfgfile, err)
}
// listen for tasks file config changes.
go func() {
for {
select {
case file := <-tm.loader.TaskChanged:
log.Info("[tasks] Task changed: %s", file)
if file == tm.loader.CfgFile {
goto ReloadTasks
}
taskConf, err := config.LoadTaskData(file)
if err != nil {
log.Error("[tasks] LoadTaskFile() error loading %s: %s", file, err)
continue
}
if taskConf.Parent == "" {
taskConf.Parent = taskConf.Name
}
log.Info("[tasks] LoadTaskFile, task %s running", taskConf.Name)
if err := tm.RemoveTask(taskConf.Name); err != nil {
log.Error("[tasks] LoadTaskFile, error removing task %s: %s", taskConf.Name, err)
}
if err := tm.loadDiskTask(taskConf.Name, taskConf); err != nil {
log.Error("[tasks] loading task %s: %s", taskConf.Name, err)
}
}
}
ReloadTasks:
log.Info("[tasks] reloading tasks from %s", tm.loader.CfgFile)
tm.LoadTaskFile(cfgfile)
}()
tm.loadDiskTasks(tasks)
return nil
}
func (tm *TaskManager) loadDiskTasks(tasks []config.TaskConfig) {
for _, task := range tasks {
taskConf, err := config.LoadTaskData(task.ConfigFile)
_, running := tm.GetTask(taskConf.Name)
// We need the name of the task instance (not the task name),
// in order to stop the task.
if !task.Enabled {
log.Info("TaskMgr.loadDiskTasks() disabled: %s, %s", task.Name, taskConf.Name)
tm.loader.RemoveWatch(task.ConfigFile)
tm.RemoveTask(taskConf.Name)
continue
}
if running {
log.Debug("TaskMgr.loadDiskTasks() %s already running, %s", task.Name, taskConf.Name)
continue
}
if err != nil {
log.Error("TaskMgr.LoadTaskFile() error loading %s: %s", task.ConfigFile, err)
continue
}
if err := tm.loadDiskTask(task.Name, taskConf); err != nil {
log.Error("TaskMgr.loadDiskTasks() loading task %s: %s", task.Name, err)
}
}
}
// loadDiskTask loads a permanent task from disk.
func (tm *TaskManager) loadDiskTask(name string, taskConf config.TaskData) error {
log.Debug("TaskMgr.loadDiskTask() %s, %s", name, taskConf.Name)
switch name {
case looptask.Name:
// TODO: check interface errors
taskName, looper := looptask.New(taskConf.Name, taskConf.Data["interval"].(string))
_, err := tm.AddTask(taskName, looper)
if err != nil {
log.Error("loading task %s: %s", taskName, err)
return err
}
case downloader.Name:
downloader := downloader.New(taskConf.Data, false)
log.Info("LoadTaskData, downloader: %s", taskConf.Name)
_, err := tm.AddTask(taskConf.Name, downloader)
if err != nil {
log.Error("loading task %s: %s", taskConf.Name, err)
return err
}
case iocscanner.Name:
taskName, iocscanner := iocscanner.New(taskConf.Name, taskConf.Data, false)
_, err := tm.AddTask(taskName, iocscanner)
if err != nil {
log.Error("loading task %s: %s", taskConf.Name, err)
return err
}
default:
log.Debug("TaskStart, unknown task %s: %s", name, taskConf.Name)
}
return nil
}