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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::errors::Error;
use askama::Template;
use log::debug;
use std::io::Write;
use std::path::PathBuf;
pub struct InternalIps(Vec<String>);
impl InternalIps {
pub fn new(ips: Vec<String>) -> Self {
InternalIps(ips)
}
}
impl std::fmt::Display for InternalIps {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let ips = &self.0;
write!(fmt, "{}", ips.join(","))?;
Ok(())
}
}
#[derive(Template)]
#[template(path = "suricata.yaml.in", escape = "none")]
struct ConfigTemplate<'a> {
rules: &'a str,
eve: &'a EveConfiguration,
community_id: &'a str,
suricata_config_path: &'a str,
internal_ips: &'a InternalIps,
stats: &'a str,
flows: bool,
http: bool,
dns: bool,
tls: bool,
smtp: bool,
max_pending_packets: &'a str,
}
pub struct Redis {
pub server: String,
pub port: u16,
}
impl Default for Redis {
fn default() -> Self {
Self {
server: "redis".into(),
port: 6379,
}
}
}
pub struct Uds {
pub path: PathBuf,
pub external_listener: bool,
}
impl Default for Uds {
fn default() -> Self {
Self {
path: PathBuf::from("/tmp/suricata.alerts"),
external_listener: false,
}
}
}
pub enum EveConfiguration {
Redis(Redis),
Uds(Uds),
}
impl EveConfiguration {
pub fn uds(path: PathBuf) -> Self {
Self::Uds(Uds {
path: path,
external_listener: false,
})
}
}
impl Default for EveConfiguration {
fn default() -> Self {
Self::Uds(Uds::default())
}
}
pub struct Config {
pub enable_stats: bool,
pub enable_flows: bool,
pub enable_http: bool,
pub enable_dns: bool,
pub enable_smtp: bool,
pub enable_tls: bool,
pub enable_community_id: bool,
pub materialize_config_to: PathBuf,
pub exe_path: PathBuf,
pub eve: EveConfiguration,
pub rule_path: PathBuf,
pub suriata_config_path: PathBuf,
pub internal_ips: InternalIps,
pub max_pending_packets: u16,
}
impl Default for Config {
fn default() -> Self {
Config {
enable_stats: true,
enable_flows: true,
enable_dns: false,
enable_smtp: false,
enable_http: false,
enable_tls: false,
enable_community_id: true,
materialize_config_to: PathBuf::from("/etc/suricata/suricata-rs.yaml"),
exe_path: {
if let Some(e) = std::env::var_os("SURICATA_EXE").map(|s| PathBuf::from(s)) {
e
} else {
PathBuf::from("/usr/local/bin/suricata")
}
},
eve: EveConfiguration::default(),
rule_path: PathBuf::from("/etc/suricata/custom.rules"),
suriata_config_path: {
if let Some(e) = std::env::var_os("SURICATA_CONFIG_DIR").map(|s| PathBuf::from(s)) {
e
} else {
PathBuf::from("/etc/suricata")
}
},
internal_ips: InternalIps(vec![
String::from("10.0.0.0/8,172.16.0.0/12"),
String::from("e80:0:0:0:0:0:0:0/64"),
String::from("127.0.0.1/32"),
String::from("fc00:0:0:0:0:0:0:0/7"),
String::from("192.168.0.0/16"),
String::from("169.254.0.0/16"),
]),
max_pending_packets: 800,
}
}
}
impl Config {
pub fn materialize(&self) -> Result<(), Error> {
let rules = self.rule_path.to_string_lossy().to_owned();
let suricata_config_path = self.suriata_config_path.to_string_lossy().to_owned();
let internal_ips = &self.internal_ips;
let stats = if self.enable_stats { "yes" } else { "no" };
let community_id = if self.enable_community_id {
"yes"
} else {
"no"
};
let max_pending_packets = format!("{}", self.max_pending_packets);
let template = ConfigTemplate {
rules: &rules,
eve: &self.eve,
community_id: &community_id,
suricata_config_path: &suricata_config_path,
internal_ips: internal_ips,
stats: &stats,
flows: self.enable_flows,
http: self.enable_http,
dns: self.enable_dns,
smtp: self.enable_smtp,
tls: self.enable_tls,
max_pending_packets: &max_pending_packets,
};
debug!("Attempting to render");
let rendered = template.render().map_err(Error::from)?;
debug!("Writing output.yaml to {:?}", self.materialize_config_to);
let mut f = std::fs::File::create(&self.materialize_config_to).map_err(Error::Io)?;
f.write(rendered.as_bytes()).map_err(Error::from)?;
debug!("Output file written");
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::config::InternalIps;
#[test]
fn test_internal_ip_display() {
let internal_ips = InternalIps(vec![
"169.254.0.0/16".to_owned(),
"192.168.0.0/16".to_owned(),
"fc00:0:0:0:0:0:0:0/7".to_owned(),
"127.0.0.1/32".to_owned(),
"10.0.0.0/8".to_owned(),
"172.16.0.0/12".to_owned(),
]);
assert_eq!(format!("{}", internal_ips), "169.254.0.0/16,192.168.0.0/16,fc00:0:0:0:0:0:0:0/7,127.0.0.1/32,10.0.0.0/8,172.16.0.0/12");
}
}