Skip to content

Commit 4640be4

Browse files
committed
SSADetector: Added ext record modification methods
1 parent 79aac69 commit 4640be4

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

process/ssadetector.cpp

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ __attribute__((constructor)) static void register_this_plugin()
5757
RecordExtSSADetector::REGISTERED_ID = register_extension();
5858
}
5959

60+
6061
SSADetectorPlugin::SSADetectorPlugin()
6162
{
63+
close();
6264
}
6365

6466
SSADetectorPlugin::~SSADetectorPlugin()
@@ -97,25 +99,133 @@ inline bool transition_from_syn_ack(RecordExtSSADetector *record,
9799
{
98100
return record->syn_table.check_range_for_presence(len, 12, !dir, ts);
99101
}
102+
103+
void SSADetectorPlugin::update_record(RecordExtSSADetector *record, const Packet &pkt)
104+
{
105+
/**
106+
* 0 - client -> server
107+
* 1 - server -> client
108+
*/
109+
uint8_t dir = pkt.source_pkt ? 0 : 1;
110+
uint16_t len = pkt.payload_len;
111+
timeval ts = pkt.ts;
112+
113+
if ( !(MIN_PKT_SIZE <= len && len <= MAX_PKT_SIZE) ) {
114+
return;
115+
}
116+
117+
bool reached_end_state = transition_from_syn_ack(record, len, ts, dir);
118+
119+
if (reached_end_state) {
120+
record->reset();
121+
if (record->syn_pkts_idx < SYN_RECORDS_NUM) {
122+
record->syn_pkts[record->syn_pkts_idx] = len;
123+
record->syn_pkts_idx += 1;
124+
}
125+
record->suspects += 1;
126+
return;
127+
}
128+
129+
transition_from_syn(record, len, ts, dir);
130+
transition_from_init(record, len, ts, dir);
100131
}
101132

102133
int SSADetectorPlugin::post_create(Flow &rec, const Packet &pkt)
103134
{
135+
RecordExtSSADetector *record = new RecordExtSSADetector();
136+
rec.add_extension(record);
137+
138+
update_record(record, pkt);
104139
return 0;
105140
}
106141

107-
int SSADetectorPlugin::pre_update(Flow &rec, Packet &pkt)
142+
int SSADetectorPlugin::post_update(Flow &rec, const Packet &pkt)
108143
{
144+
RecordExtSSADetector *record = (RecordExtSSADetector *) rec.get_extension(RecordExtSSADetector::REGISTERED_ID);
145+
update_record(record, pkt);
109146
return 0;
110147
}
111148

112-
int SSADetectorPlugin::post_update(Flow &rec, const Packet &pkt)
149+
double classes_ratio(uint8_t* syn_pkts, uint8_t size)
113150
{
114-
return 0;
151+
uint8_t unique_members = 0;
152+
bool marked[size];
153+
for (uint8_t i = 0; i < size; ++i) marked[i] = false;
154+
for (uint8_t i = 0; i < size; ++i) {
155+
if (marked[i]) {
156+
continue;
157+
}
158+
uint8_t akt_pkt_size = syn_pkts[i];
159+
unique_members++;
160+
marked[i] = true;
161+
for (uint8_t j = i + 1; j < size; ++j) {
162+
if (marked[j]) {
163+
continue;
164+
}
165+
if (syn_pkts[j] == akt_pkt_size) {
166+
marked[j] = true;
167+
}
168+
}
169+
}
170+
171+
return double(unique_members) / double(size);
115172
}
116173

117174
void SSADetectorPlugin::pre_export(Flow &rec)
118175
{
176+
//do not export for small packets flows
177+
uint32_t packets = rec.src_packets + rec.dst_packets;
178+
if (packets <= 30) {
179+
rec.remove_extension(RecordExtSSADetector::REGISTERED_ID);
180+
return;
181+
}
182+
183+
RecordExtSSADetector *record = (RecordExtSSADetector *) rec.get_extension(RecordExtSSADetector::REGISTERED_ID);
184+
const auto& suspects = record->suspects;
185+
if (suspects < 3) {
186+
return;
187+
}
188+
if (double(packets)/double(suspects) > 2500) {
189+
return;
190+
}
191+
if (suspects < 15) {
192+
if (classes_ratio(record->syn_pkts, record->syn_pkts_idx) > 0.6) {
193+
return;
194+
}
195+
} else if (suspects < 40) {
196+
if (classes_ratio(record->syn_pkts, record->syn_pkts_idx) > 0.4) {
197+
return;
198+
}
199+
} else {
200+
if (classes_ratio(record->syn_pkts, record->syn_pkts_idx) > 0.2) {
201+
return;
202+
}
203+
}
204+
205+
record->possible_vpn = 1;
206+
}
207+
208+
void SSADetectorPlugin::transition_from_init(RecordExtSSADetector *record,
209+
uint16_t len, const timeval& ts, uint8_t dir)
210+
{
211+
record->syn_table.update_entry(len, dir, ts);
212+
}
213+
214+
void SSADetectorPlugin::transition_from_syn(RecordExtSSADetector *record,
215+
uint16_t len, const timeval& ts, uint8_t dir)
216+
{
217+
bool can_transit = record->syn_table.check_range_for_presence(len, 10, !dir, ts);
218+
if (can_transit) {
219+
record->syn_ack_table.update_entry(len, dir, ts);
220+
}
221+
}
222+
223+
bool SSADetectorPlugin::transition_from_syn_ack(RecordExtSSADetector *record, uint16_t len,
224+
const timeval& ts, uint8_t dir)
225+
{
226+
return record->syn_table.check_range_for_presence(len, 12, !dir, ts);
227+
}
228+
119229
//--------------------RecordExtSSADetector::pkt_entry-------------------------------
120230
void RecordExtSSADetector::pkt_entry::reset()
121231
{

process/ssadetector.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,10 @@ class SSADetectorPlugin : public ProcessPlugin
179179
RecordExt *get_ext() const { return new RecordExtSSADetector(); }
180180
ProcessPlugin *copy();
181181

182-
int pre_create(Packet &pkt);
183182
int post_create(Flow &rec, const Packet &pkt);
184-
int pre_update(Flow &rec, Packet &pkt);
185183
int post_update(Flow &rec, const Packet &pkt);
186184
void pre_export(Flow &rec);
185+
void update_record(RecordExtSSADetector *record, const Packet &pkt);
187186
static inline void transition_from_init(RecordExtSSADetector *record, uint16_t len,
188187
const timeval& ts, uint8_t dir);
189188
static inline void transition_from_syn(RecordExtSSADetector *record, uint16_t len,

0 commit comments

Comments
 (0)