log.go 6.23 KB
Newer Older
1 2
package model

3 4 5 6 7 8 9 10 11
import (
	"encoding/json"
	"time"
)

const (
	ScopeKindCluster = "cluster"
)

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
type WafDetectionMessage struct {
	WafDetectionMessageBasic
	AttackedLog []WafDetectionAttackedLog `json:"attacked_log"`
	CreatedAt   int64                     `json:"created_at"`
}

type WafDetectionMessageBasic struct {
	ClusterKey string `json:"cluster_key"`
	Namespace  string `json:"namespace"`
	ResKind    string `json:"res_kind"`
	ResName    string `json:"res_name"`
	ServiceID  int64  `json:"service_id"`
	AppName    string `json:"app_name"`
}

type WafDetectionAttackedLog struct {
	ID             string `json:"id"`
	RuleID         int64  `json:"rule_id"`
	RuleName       string `json:"rule_name"`
	Action         string `json:"action"`
	AttackIP       string `json:"attack_ip"`
	AttackLoad     string `json:"attack_load"`
	AttackTime     int64  `json:"attack_time"`
	AttackType     string `json:"attack_type"`
	AttackedApp    string `json:"attacked_app"`
	AttackedURL    string `json:"attacked_url"`
	ReqPkg         string `json:"req_pkg"`
	RspPkg         string `json:"rsp_pkg,omitempty"`
	RspContentType string `json:"rsp_content_type,omitempty"`
}

type WafDetection struct {
	WafDetectionMessageBasic
	WafDetectionAttackedLog
	CreatedAt int64 `json:"created_at"`
}
48

49 50 51 52 53 54 55 56 57 58 59 60 61
type Signal struct {
	ID                string                 `json:"id"`
	RuleKey           *RuleKey               `json:"ruleKey"`
	Scope             *map[string]Scope      `json:"scope"`
	Severity          int                    `json:"severity"`           // 信号严重程度,原始信号不携带,从mysql-rules表填充
	Tags              []string               `json:"tags"`               // 信号标签,一期只有系统生成,内容为规则标签,原始信号不携带,从mysql-rules表填充
	EventIDs          []string               `json:"eventIDs,omitempty"` // 维护当前signal被哪些events关联,内容为event.id;原始信号不携带,palace daemon存储时进行初始化(空)或append
	Context           map[string]interface{} `json:"context"`            // 检测端只传递原始的key-value,多语言交给后面处理;只用于展示,不用做筛选搜索
	CreatedAt         int64                  `json:"createdAt"`
	IsWhitelistFilter bool                   `json:"isWhitelistFilter"` // 是否被白名单过滤
	WhitelistIDs      []int64                `json:"whitelistIDs"`      // 记录匹配的白名单策略id
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
type Event struct {
	ID           string                 `json:"id"`
	Type         string                 `json:"type"`         // 事件类型(关联类型)
	Description  string                 `json:"description"`  // 事件描述
	RuleKeys     []RuleKey              `json:"ruleKeys"`     // 该event涉及到的signals,触发的rule列表,去重
	Scopes       map[string][]Scope     `json:"scopes"`       // 关联后,需要保留每个signal的scope,去重
	Resources    []map[string]Scope     `json:"resources"`    // 资源列表,关联信号的所有scope,保留了scope内部的关系,去重
	Relation     Relation               `json:"relation"`     // 事件的关联表达,可能是图、时间轴
	Severity     int                    `json:"severity"`     // 严重程度 枚举;算法见下方
	Tags         []string               `json:"tags"`         // 事件标签,一期只有系统生成(规则子标签),二期用户可自定义(标签系统)
	SignalsCount map[int]int            `json:"signalsCount"` // 关联的信号数量,按严重程度拆分
	UpdatedAt    int64                  `json:"updatedAt"`    // 更新时间
	CreatedAt    int64                  `json:"createdAt"`    // 创建时间
	Timestamp    time.Time              `json:"timestamp"`
	Context      map[string]interface{} `json:"context"` // 只有规则关联才有,取交集

	Process *Process `json:"process,omitempty"`
}

type Process struct {
	Status    int    `json:"status"`
	Processor string `json:"processor"`
	Remark    string `json:"remark"`
	Timestamp int64  `json:"timestamp"`
}

const (
	ProcessStatusUnProcessed      = 0
	ProcessStatusProcessed        = 1
	ProcessStatusOmitted          = 2
	ProcessStatusPendingProcessed = 3
)

type Relation struct {
	Type  string `json:"type"`            // 关系类型,可能是图、时间轴
	Graph *Graph `json:"graph,omitempty"` // 图
}

type Graph struct {
	Nodes map[string]*Node `json:"nodes"` // 节点,map类型用于快速确定node是否存在
	Edges []*Edge          `json:"edges"` // 边
}

type Node struct {
	ID         string                 `json:"id"`         // 根据规则生成,不同的关联方式会不同,如进程树可能是pid+pname
	Label      string                 `json:"label"`      // 节点标签,即类型,如:signal、process
	Properties map[string]interface{} `json:"properties"` // 其他信息
}

type Edge struct {
	Type        int                    `json:"type"`        // 边的类型,0-无向边,1-有向边。无向边时,Source Destination仅表示节点
	Relation    string                 `json:"relation"`    // 关系名称,如:父进程关系、网络调用关系、关联信号关系
	Source      *Node                  `json:"source"`      // 边的起点节点
	Destination *Node                  `json:"destination"` // 边的终点节点
	Properties  map[string]interface{} `json:"properties"`  // 其他信息
}

type AliasRuleKey RuleKey
type RuleKey struct {
	Version1 uint16 `json:"version1"`
	Name     string `json:"name"`
	Category string `json:"category"`
}

func (r RuleKey) MarshalJSON() ([]byte, error) {
	path := ""
	if r.Category != "" {
		path = r.Category
		if r.Name != "" {
			path = path + "/" + r.Name
		}
	}
	x := struct {
		AliasRuleKey
		Path string `json:"path"`
	}{AliasRuleKey(r), path}
	return json.Marshal(x)
}

func (r *RuleKey) GenKey() string {
	return r.Category + "$" + r.Name
}

type Scope struct {
	Kind string `json:"kind"` // required
	ID   string `json:"id"`   // optional,视具体情况
	Name string `json:"name"` // required
}

func (s *Scope) GenKey() string {
	return s.Kind + "$" + s.ID + "$" + s.Name
}

// GenSuggestionKey
// 集群以id+name去重,其他以name去重
func (s *Scope) GenSuggestionKey() string {
	if s.Kind == ScopeKindCluster {
		return s.Kind + "$" + s.ID + "$" + s.Name
	}
	return s.Kind + "$" + s.Name
}