-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyn_attack.cpp
65 lines (57 loc) · 1.63 KB
/
syn_attack.cpp
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
#include "syn_attack.h"
#include <cstdint>
#include <mutex>
#define SYN_THRESHOLD 10
#define SYN_RST_THRESHOLD 10
#define SYN_PERIOD 5
struct syn_entry {
steady_clock::time_point timestamp;
set<uint32_t> acks;
uint32_t rst_count;
};
map<string, syn_entry> syn_map;
mutex syn_map_mutex;
bool suspicious_request(string dst_ip) {
uint32_t diff =
duration_cast<seconds>(steady_clock::now() - syn_map[dst_ip].timestamp)
.count();
if ((syn_map[dst_ip].acks.size() > SYN_THRESHOLD ||
syn_map[dst_ip].rst_count > SYN_RST_THRESHOLD) &&
diff < SYN_PERIOD) {
cout << "SYN attack detected" << endl;
return true;
} else if (diff >= SYN_PERIOD) {
syn_map[dst_ip].acks.clear();
syn_map[dst_ip].rst_count = 0;
}
syn_map[dst_ip].timestamp = steady_clock::now();
return false;
}
bool is_syn_attck(string src_ip, string dst_ip, bool is_sent,
struct tcphdr *tcp_hdr) {
if (tcp_hdr->syn && tcp_hdr->ack && is_sent) {
if (suspicious_request(dst_ip)) {
return true;
}
syn_map[dst_ip].acks.insert(tcp_hdr->seq + 1);
} else if (tcp_hdr->rst && tcp_hdr->ack && is_sent) {
if (suspicious_request(dst_ip)) {
return true;
}
syn_map[dst_ip].rst_count++;
} else if (tcp_hdr->ack && !is_sent) {
syn_map[src_ip].acks.erase(tcp_hdr->ack_seq);
}
return false;
}
void clean_up_syn_map() {
lock_guard<mutex> lock(syn_map_mutex);
for (auto it = syn_map.begin(); it != syn_map.end();) {
if (duration_cast<seconds>(steady_clock::now() - it->second.timestamp)
.count() >= SYN_PERIOD) {
it = syn_map.erase(it);
} else {
it++;
}
}
}