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
217
218
219
220
221
222
223
use std::collections::HashSet;
use std::io::Read;
use std::time::SystemTime;
use chrono::{DateTime, SecondsFormat, Utc};
use curl::easy::Easy;
use registry::{Hive, Security};
use serde::Serialize;
use crate::config::{Config, Param};
use crate::connectors::connector::{Connector, ConnectorError};
use crate::process::ProcessRecord;
pub struct SitinCloud;
impl SitinCloud {
fn name() -> String {
String::from("SitinCloud")
}
fn host() -> String {
let regkey = Hive::LocalMachine
.open(r"SOFTWARE\Owlyshield\SitinCloud", Security::Read)
.expect("Cannot open registry hive");
return regkey
.value("API_HOST")
.expect(&format!("Cannot open registry key HOST"))
.to_string();
}
fn client() -> String {
let regkey = Hive::LocalMachine
.open(r"SOFTWARE\Owlyshield\SitinCloud", Security::Read)
.expect("Cannot open registry hive");
return regkey
.value("CLIENT_ID")
.expect(&format!("Cannot open registry key CLIENT ID"))
.to_string();
}
fn license_key() -> String {
let regkey = Hive::LocalMachine
.open(r"SOFTWARE\Owlyshield\SitinCloud", Security::Read)
.expect("Cannot open registry hive");
return regkey
.value("LICENSE_KEY")
.expect(&format!("Cannot open registry key CLIENT ID"))
.to_string();
}
}
#[derive(Serialize)]
#[allow(non_snake_case)]
struct SecurityEvent {
appName: String,
clientId: String,
hostname: String,
killTime: String,
clientKey: String,
pidsCount: usize,
predScore: f32,
startTime: String,
filesChanged: HashSet<String>,
filesCreated: HashSet<String>,
filesMovedCount: usize,
filesChangedCount: usize,
filesCreatedCount: usize,
filesDeletedCount: usize,
filesRenamedCount: usize,
secondsSinceLaunch: i64,
dirWithFilesChanged: HashSet<String>,
dirWithFilesCreated: HashSet<String>,
extensionsWriteCount: usize,
sumWeightReadEntropy: f64,
sumWeightWriteEntropy: f64,
filesExtensionChangedCount: usize,
}
impl SecurityEvent {
fn from(proc: &ProcessRecord, prediction: f32) -> SecurityEvent {
let start: DateTime<Utc> = proc.time_started.into();
let kill: DateTime<Utc> = proc.time_killed.unwrap_or(SystemTime::now()).into();
return SecurityEvent {
appName: proc.appname.clone(),
clientId: SitinCloud::client(),
hostname: hostname::get()
.unwrap()
.to_str()
.unwrap_or("Unknown host")
.to_string(),
killTime: kill.to_rfc3339_opts(SecondsFormat::Micros, true),
clientKey: SitinCloud::client(),
pidsCount: proc.pids.len(),
predScore: prediction,
startTime: start.to_rfc3339_opts(SecondsFormat::Micros, true),
filesChanged: proc.fpaths_updated.clone(),
filesCreated: proc.fpaths_created.clone(),
filesMovedCount: proc.files_read.len(),
filesChangedCount: proc.files_written.len(),
filesCreatedCount: proc.files_opened.len(),
filesDeletedCount: proc.files_deleted.len(),
filesRenamedCount: proc.files_renamed.len(),
secondsSinceLaunch: (kill - start).num_seconds() * 10,
dirWithFilesChanged: proc.dirs_with_files_updated.clone(),
dirWithFilesCreated: proc.dirs_with_files_created.clone(),
extensionsWriteCount: proc.extensions_written.count_all(),
sumWeightReadEntropy: proc.entropy_read,
sumWeightWriteEntropy: proc.entropy_written,
filesExtensionChangedCount: proc.extensions_read.count_all(),
};
}
fn to_json(&self) -> String {
return serde_json::to_string(&self).unwrap_or("{}".to_string());
}
}
#[derive(Debug, Serialize)]
#[allow(non_snake_case)]
struct PingData {
clientId: String,
hostname: String,
numVersion: String,
licenseKey: String,
killPolicy: String,
}
impl PingData {
fn from(config: &Config) -> PingData {
return PingData {
clientId: SitinCloud::client(),
hostname: hostname::get()
.unwrap()
.to_str()
.unwrap_or("Unknown host")
.to_string(),
numVersion: config[Param::NumVersion].clone(),
licenseKey: SitinCloud::license_key(),
killPolicy: config[Param::KillPolicy].clone(),
};
}
fn to_json(&self) -> String {
return serde_json::to_string(&self).unwrap_or("{}".to_string());
}
}
impl Connector for SitinCloud {
fn to_string(&self) -> String {
return SitinCloud::name();
}
fn on_startup(&self, config: &Config) -> Result<(), ConnectorError> {
let event = PingData::from(config).to_json();
eprintln!("event = {:?}", event);
let mut data = event.as_bytes();
let mut easy = Easy::new();
let mut api_url = SitinCloud::host();
api_url.push_str("/ping");
easy.url(api_url.as_str()).unwrap();
easy.post(true).unwrap();
easy.post_field_size(data.len() as u64).unwrap();
let mut transfer = easy.transfer();
transfer.read_function(|buf| Ok(data.read(buf).unwrap_or(0)))?;
match transfer.perform() {
Ok(()) => Ok(()),
Err(e) => Err(ConnectorError::new(
SitinCloud::name().as_str(),
format!("Connector error: {}", e.description()).as_str(),
)),
}
}
fn on_event_kill(
&self,
_config: &Config,
proc: &ProcessRecord,
prediction: f32,
) -> Result<(), ConnectorError> {
let event = SecurityEvent::from(proc, prediction).to_json();
let mut data = event.as_bytes();
let mut easy = Easy::new();
let mut api_url = SitinCloud::host();
api_url.push_str("/security-event");
easy.url(api_url.as_str()).unwrap();
easy.post(true).unwrap();
easy.post_field_size(data.len() as u64).unwrap();
let mut transfer = easy.transfer();
transfer.read_function(|buf| Ok(data.read(buf).unwrap_or(0)))?;
match transfer.perform() {
Ok(()) => Ok(()),
Err(e) => Err(ConnectorError::new(
SitinCloud::name().as_str(),
format!("Connector error: {}", e.description()).as_str(),
)),
}
}
}
impl From<curl::Error> for ConnectorError {
fn from(e: curl::Error) -> Self {
ConnectorError::new(SitinCloud::name().as_str(), e.description())
}
}