Eunomia  0.1.0
A lightweight eBPF-based CloudNative Monitor tool for Container Security and Observability
sec_analyzer.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2  *
3  * Copyright (c) 2022, 郑昱笙,濮雯旭,张典典(牛校牛子队)
4  * All rights reserved.
5  */
6 
7 #ifndef EUNOMIA_SEC_ANALYZER_H
8 #define EUNOMIA_SEC_ANALYZER_H
9 
10 #include "files.h"
11 #include "model/event_handler.h"
12 #include "process.h"
13 #include "prometheus_server.h"
14 #include "syscall.h"
15 
17 enum class sec_rule_level
18 {
19  event,
20  warnning,
21  alert,
22  // TODO: add more levels?
23 };
24 
26 
28 enum class sec_rule_type
29 {
30  syscall,
31  tcp,
32  process,
33  files,
34  mix,
35  // TODO: add more types?
36 };
37 
39 
42 {
44  std::string name;
45  std::string message;
46  int pid;
47 
48  std::string container_id;
49  std::string container_name;
50 };
51 
54 {
57  std::string name;
58  std::string message;
59 
61  std::string signature;
62 };
63 
66 {
67 public:
68  // EVNETODO: use the mutex
69  std::mutex mutex;
70  const std::vector<sec_rule_describe> rules;
71 
72  sec_analyzer(const std::vector<sec_rule_describe> &in_rules) : rules(in_rules)
73  {
74  }
75  virtual ~sec_analyzer() = default;
76  virtual void report_event(const rule_message &msg);
77  void print_event(const rule_message &msg);
78 
79  static std::shared_ptr<sec_analyzer> create_sec_analyzer_with_default_rules(void);
80  static std::shared_ptr<sec_analyzer> create_sec_analyzer_with_additional_rules(const std::vector<sec_rule_describe> &rules);
81 };
82 
85 {
86 private:
87  prometheus::Family<prometheus::Counter> &eunomia_sec_warn_counter;
88  prometheus::Family<prometheus::Counter> &eunomia_sec_event_counter;
89  prometheus::Family<prometheus::Counter> &eunomia_sec_alert_counter;
90 public:
91  void report_prometheus_event(const struct rule_message &msg);
92  void report_event(const rule_message &msg);
93  sec_analyzer_prometheus(prometheus_server &server, const std::vector<sec_rule_describe> &rules);
94 
95  static std::shared_ptr<sec_analyzer> create_sec_analyzer_with_default_rules(prometheus_server &server);
96  static std::shared_ptr<sec_analyzer> create_sec_analyzer_with_additional_rules(const std::vector<sec_rule_describe> &rules, prometheus_server &server);
97 };
98 
100 template<typename EVNET>
101 class rule_base :public event_handler<EVNET>
102 {
103 public:
104  std::shared_ptr<sec_analyzer> analyzer;
105  rule_base(std::shared_ptr<sec_analyzer> analyzer_ptr) : analyzer(analyzer_ptr) {}
106  virtual ~rule_base() = default;
107 
108  // return rule id if matched
109  // return -1 if not matched
110  virtual int check_rule(const tracker_event<EVNET> &e, rule_message &msg) = 0;
112  {
113  if (!analyzer)
114  {
115  std::cout << "analyzer is null" << std::endl;
116  }
117  struct rule_message msg;
118  int res = check_rule(e, msg);
119  if (res != -1)
120  {
121  analyzer->report_event(msg);
122  }
123  }
124 };
125 
127 
129 class files_rule_checker : public rule_base<files_event>
130 {
131 public:
132  virtual ~files_rule_checker() = default;
133  files_rule_checker(std::shared_ptr<sec_analyzer> analyzer_ptr) : rule_base(analyzer_ptr)
134  {}
136 };
137 
139 
141 class process_rule_checker : public rule_base<process_event>
142 {
143 public:
144  virtual ~process_rule_checker() = default;
145  process_rule_checker(std::shared_ptr<sec_analyzer> analyzer_ptr) : rule_base(analyzer_ptr)
146  {}
148 };
149 
151 
153 class syscall_rule_checker : public rule_base<syscall_event>
154 {
155 public:
156  syscall_rule_checker(std::shared_ptr<sec_analyzer> analyzer_ptr) : rule_base(analyzer_ptr)
157  {}
159 };
160 
161 #endif
files_rule_checker::~files_rule_checker
virtual ~files_rule_checker()=default
sec_analyzer::sec_analyzer
sec_analyzer(const std::vector< sec_rule_describe > &in_rules)
Definition: sec_analyzer.h:72
process_rule_checker::~process_rule_checker
virtual ~process_rule_checker()=default
sec_analyzer::mutex
std::mutex mutex
Definition: sec_analyzer.h:69
rule_base
base class for securiy rules detect handler
Definition: sec_analyzer.h:101
eunomia_mode::server
@ server
rule_message::container_name
std::string container_name
Definition: sec_analyzer.h:49
sec_rule_level::alert
@ alert
syscall_rule_checker
syscall rule:
Definition: sec_analyzer.h:153
sec_analyzer_prometheus::sec_analyzer_prometheus
sec_analyzer_prometheus(prometheus_server &server, const std::vector< sec_rule_describe > &rules)
Definition: sec_analyzer.cpp:28
event_handler.h
sec_analyzer_prometheus::report_prometheus_event
void report_prometheus_event(const struct rule_message &msg)
Definition: sec_analyzer.cpp:78
sec_rule_describe::message
std::string message
Definition: sec_analyzer.h:58
sec_rule_describe::type
sec_rule_type type
Definition: sec_analyzer.h:56
rule_message::message
std::string message
Definition: sec_analyzer.h:45
event_handler
the event handler for single type
Definition: event_handler.h:52
sec_rule_type::tcp
@ tcp
sec_rule_level::event
@ event
syscall.h
rule_message
message for sec_rule
Definition: sec_analyzer.h:41
sec_rule_type::files
@ files
sec_rule_type::process
@ process
rule_message::name
std::string name
Definition: sec_analyzer.h:44
rule_base::analyzer
std::shared_ptr< sec_analyzer > analyzer
Definition: sec_analyzer.h:104
process.h
sec_rule_level
sec_rule_level
sec rules info level
Definition: sec_analyzer.h:17
rule_base::handle
void handle(tracker_event< EVNET > &e)
implement this function to handle the event
Definition: sec_analyzer.h:111
sec_rule_type::syscall
@ syscall
rule_message::level
sec_rule_level level
Definition: sec_analyzer.h:43
process_rule_checker::check_rule
int check_rule(const tracker_event< process_event > &e, rule_message &msg)
process_rule_checker::process_rule_checker
process_rule_checker(std::shared_ptr< sec_analyzer > analyzer_ptr)
Definition: sec_analyzer.h:145
sec_rule_type::mix
@ mix
rule_message::pid
int pid
Definition: sec_analyzer.h:46
sec_analyzer::create_sec_analyzer_with_additional_rules
static std::shared_ptr< sec_analyzer > create_sec_analyzer_with_additional_rules(const std::vector< sec_rule_describe > &rules)
Definition: sec_analyzer.cpp:165
syscall_rule_checker::check_rule
int check_rule(const tracker_event< syscall_event > &e, rule_message &msg)
Definition: sec_analyzer.cpp:116
rule_base::~rule_base
virtual ~rule_base()=default
sec_rule_level::warnning
@ warnning
rule_base::check_rule
virtual int check_rule(const tracker_event< EVNET > &e, rule_message &msg)=0
prometheus_server.h
sec_analyzer_prometheus::create_sec_analyzer_with_additional_rules
static std::shared_ptr< sec_analyzer > create_sec_analyzer_with_additional_rules(const std::vector< sec_rule_describe > &rules, prometheus_server &server)
Definition: sec_analyzer.cpp:182
tracker_event
the basic event type
Definition: event_handler.h:31
sec_analyzer
sec analyzer manager
Definition: sec_analyzer.h:65
sec_analyzer::report_event
virtual void report_event(const rule_message &msg)
Definition: sec_analyzer.cpp:67
files_rule_checker::files_rule_checker
files_rule_checker(std::shared_ptr< sec_analyzer > analyzer_ptr)
Definition: sec_analyzer.h:133
sec_analyzer::print_event
void print_event(const rule_message &msg)
Definition: sec_analyzer.cpp:56
syscall_rule_checker::syscall_rule_checker
syscall_rule_checker(std::shared_ptr< sec_analyzer > analyzer_ptr)
Definition: sec_analyzer.h:156
sec_analyzer::~sec_analyzer
virtual ~sec_analyzer()=default
process_rule_checker
process rule:
Definition: sec_analyzer.h:141
sec_rule_describe::name
std::string name
Definition: sec_analyzer.h:57
sec_analyzer_prometheus
sec analyzer manager with prometheus exporter
Definition: sec_analyzer.h:84
files_rule_checker::check_rule
int check_rule(const tracker_event< files_event > &e, rule_message &msg)
files_rule_checker
files rule:
Definition: sec_analyzer.h:129
prometheus_server
Definition: prometheus_server.h:22
sec_analyzer::rules
const std::vector< sec_rule_describe > rules
Definition: sec_analyzer.h:70
files.h
sec_rule_describe::level
sec_rule_level level
Definition: sec_analyzer.h:55
sec_rule_type
sec_rule_type
sec rules type
Definition: sec_analyzer.h:28
sec_rule_describe::signature
std::string signature
signature: the signature of the rule, for example, process name, syscall name, etc.
Definition: sec_analyzer.h:61
sec_analyzer::create_sec_analyzer_with_default_rules
static std::shared_ptr< sec_analyzer > create_sec_analyzer_with_default_rules(void)
Definition: sec_analyzer.cpp:160
rule_base::rule_base
rule_base(std::shared_ptr< sec_analyzer > analyzer_ptr)
Definition: sec_analyzer.h:105
rule_message::container_id
std::string container_id
Definition: sec_analyzer.h:48
sec_rule_describe
describe a sec_rule
Definition: sec_analyzer.h:53
sec_analyzer_prometheus::report_event
void report_event(const rule_message &msg)
Definition: sec_analyzer.cpp:72