opensnitch/daemon/tasks/iocscanner/config/config.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

205 lines
4.5 KiB
Go

// Copyright 2025 The OpenSnitch Authors. All rights reserved.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.
package config
import (
"encoding/json"
"github.com/evilsocket/opensnitch/daemon/tasks/scheduler"
)
type NotifyType string
type ActionType string
type ActionData string
type ReportType string
var (
Enabled = "enabled"
NotifyDesktop = NotifyType("desktop")
ActionDelete = ActionType("delete")
ActionKill = ActionType("kill")
ActionNotify = ActionType("notify")
ReportFile = ReportType("file")
)
type NotifyStatus struct {
Type NotifyType `json:"type"`
Message string `json:"message"`
}
type NotifyOpts struct {
Success NotifyStatus `json:"success"`
Error NotifyStatus `json:"error"`
Enabled bool `json:"enabled"`
}
type ActionsOpts struct {
Type ActionType
Data ActionData
}
type List struct {
Name string
}
type ExclusionsOpts struct {
Dirs []string
Files []string
Tags []string
}
type ReportOpts struct {
Type ReportType
Path string
Format string
Sync bool
}
// ScanOpts holds the options of each tool.
// Some fields will be common to all tools, and some will be ignored if they
// don't apply to a particular tool (MaxSize -> debsums)
// yara tool configuration example:
// "options": {
// "debug": false,
// "recursive": true,
// "scanprocs": false,
// "fastscan": false,
// "maxSize": 0,
// "maxProcessMem": 0,
// "maxRunningTime": "1h",
// "threads": 1,
// "priority": 0,
// "reports": [
// {
// "type": "file",
// "path": "/etc/opensnitchd/tasks/iocscanner/reports",
// "format": ""
// }
// ],
// "dirs": ["/dev/shm", "/tmp",
// "/etc/cron.d", "/etc/cron.daily", "/etc/cron.weekly",
// ],
// "files": ["/etc/ld.so.config"],
// "rules": [ "/etc/opensnitchd/tasks/iocscanner/rules/*.yar" ]
// }
type ScanOpts struct {
Dirs []string
Files []string
Rules []string
Tags []string
Exclusions ExclusionsOpts
Reports []ReportOpts
MaxRunningTime string
// -z size --skip-larger=size
// Skip files larger than the given size in bytes
MaxSize int
//MaxProcessMem int
// max number of threads (yara)
// 0 == use all available cores.
Threads int
// nice value: -20 (maximum priority) to 20 (less priority)
Priority int
ScanProcs bool
Recursive bool
FastScan bool
Debug bool
}
// ToolOpts holds the configuration of the tools to launch.
// IOCScanner understands 3 tools: yara, script and debsums/dpkg. Defined by
// the Name field.
// Each tool has its own configuration, sharing some fields.
// Coniguration example:
// {
// "name": "yara",
// "msgstart": "IOC scanner yara started",
// "msgend": "IOC scanner yara finished",
// "enabled": false,
// "cmd": ["/usr/bin/yara"],
// "dataDir": "/etc/opensnitchd/tasks/iocscanner/data/",
// "reports": [
// {
// "type": "file",
// "path": "/etc/opensnitchd/tasks/iocscanner/reports",
// "format": ""
// }
// ],
// (...)
// },
type ToolOpts struct {
Options ScanOpts
Cmd []string
// Name defines the tool to launch (yara, scripts, debsums, dpkg)
Name string
// MsgStart allows to use a custom message for the notification
MsgStart string
MsgEnd string
// to store temp files, rules, etc
DataDir string
// depending on what we execute, we may need to increase this buffer to
// read the output from the command.
ReadBuffer int
// read workers to handle stdout
Workers int
Enabled bool
}
// IOCConfig holds the configuration of a task instance.
// The Schedule object holds when the tools defined will be launched.
// Tools holds the defined tools to run.
// TODO: actions
//
// "schedule": [
// {
// "weekday": [0,1,2,3,4,5,6],
// "time": ["09:55:00", "20:15:20", "22:10:50", "23:45:00", "01:17:55"],
// "hour": [],
// "minute": [],
// "second": []
// }
// ],
// "tools": [
// {
// "name": "script-ls",
// "enabled": false,
// "cmd": ["ls", "/tmp"],
// "options": {
// "recursive": false,
// "priority": 0,
// "dirs": [],
// "files": []
// }
// },
//]
type IOCConfig struct {
Schedule []scheduler.Config `json:"schedule"`
Tools []ToolOpts
Notify NotifyOpts
Actions []ActionsOpts
Interval string
}
func LoadConfig(data map[string]interface{}) (IOCConfig, error) {
dataStr, err := json.Marshal(data)
if err != nil {
return IOCConfig{}, err
}
var dc IOCConfig
err = json.Unmarshal([]byte(dataStr), &dc)
return dc, err
}