-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.zeek
155 lines (132 loc) · 5.17 KB
/
script.zeek
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
@load base/protocols/conn
@load base/protocols/http
@load base/protocols/dns
@load base/protocols/ftp
@load base/protocols/ssh
module ExtractFeatures;
export {
# Define a new logging stream
redef enum Log::ID += { LOG };
# Connection state tracking
global conn_state_tracker: table[addr, addr] of count &create_expire = 5 mins;
# Define a global variable for the log directory, with a default value
global log_dir: string &redef;
# Define the log record structure
type Info: record {
ts: time &log; # Stime (25)
uid: string &log;
srcip: addr &log; # srcip (1)
sport: port &log; # sport (2)
dstip: addr &log; # dstip (3)
dsport: port &log; # dsport (4)
service: string &log; # service (14)
proto: string &log &default="-"; # proto (5)
trans_depth: count &log &default=0; # Transaction depth for HTTP
is_sm_ips_ports: count &log &default=0; # is_sm_ips_ports (30)
ct_flw_http_mthd: count &log &default=0; # ct_flw_http_mthd (32)
is_ftp_login: count &log &default=0; # is_ftp_login (33)
};
}
event zeek_init() {
local output_dir = log_dir == "" ? "ALERT" : log_dir;
Log::create_stream(ExtractFeatures::LOG,
[$columns=Info,
$path=output_dir]
);
# Disable logs for common Zeek protocols and activities
Log::disable_stream(Conn::LOG); # conn.log: Connection information (IP addresses, ports, protocols, durations)
Log::disable_stream(DNS::LOG); # dns.log: DNS queries and responses
Log::disable_stream(HTTP::LOG); # http.log: HTTP requests and responses
Log::disable_stream(Files::LOG); # files.log: File transfer activities over the network
Log::disable_stream(SSL::LOG); # ssl.log: SSL/TLS session information
Log::disable_stream(X509::LOG); # x509.log: X.509 certificate details
Log::disable_stream(SMTP::LOG); # smtp.log: SMTP session-level activity (email traffic)
Log::disable_stream(FTP::LOG); # ftp.log: FTP session details
Log::disable_stream(Weird::LOG); # weird.log: Logs unusual or unexpected events detected by Zeek
Log::disable_stream(SNMP::LOG); # snmp.log: SNMP traffic and related information
Log::disable_stream(DHCP::LOG); # dhcp.log: DHCP transactions and leases
Log::disable_stream(SSH::LOG); # ssh.log: SSH session details
#packet filter disable
Log::disable_stream(PacketFilter::LOG); # packet_filter.log: Logs packet filter activity
}
# Helper function to determine protocol
function get_protocol(c: connection): string {
if ( c?$conn ) {
if ( c$conn?$proto ) {
return fmt("%s", c$conn$proto);
}
}
return "-";
}
# Update connection state tracking
function update_conn_tracking(c: connection) {
local src = c$id$orig_h;
local dst = c$id$resp_h;
if ([src, dst] !in conn_state_tracker)
conn_state_tracker[src, dst] = 0;
conn_state_tracker[src, dst] += 1;
}
event connection_state_remove(c: connection) {
local is_sm_ips_ports: count = 0;
if (c$id$orig_h == c$id$resp_h && c$id$orig_p == c$id$resp_p) {
is_sm_ips_ports = 1;
}
local info: Info = [
$ts = c$start_time,
$uid = c$uid,
$srcip = c$id$orig_h,
$sport = c$id$orig_p,
$dstip = c$id$resp_h,
$dsport = c$id$resp_p,
$service = (c?$conn && c$conn?$service) ? c$conn$service : "-",
$proto = get_protocol(c),
$is_sm_ips_ports = is_sm_ips_ports
];
Log::write(LOG, info);
}
# Handle HTTP requests
event http_request(c: connection, method: string, original_URI: string,
unescaped_URI: string, version: string) {
update_conn_tracking(c);
local is_sm_ips_ports: count = 0;
if (c$id$orig_h == c$id$resp_h && c$id$orig_p == c$id$resp_p) {
is_sm_ips_ports = 1;
}
local info: Info = [
$ts = network_time(),
$uid = c$uid,
$srcip = c$id$orig_h,
$sport = c$id$orig_p,
$dstip = c$id$resp_h,
$dsport = c$id$resp_p,
$service = "http",
$proto = get_protocol(c),
$trans_depth = c$http$trans_depth,
$ct_flw_http_mthd = conn_state_tracker[c$id$orig_h, c$id$resp_h],
$is_sm_ips_ports = is_sm_ips_ports
];
Log::write(LOG, info);
}
# Handle FTP commands
event ftp_request(c: connection, command: string, arg: string) {
if (command == "PASS") {
local is_ftp_login = 1;
local is_sm_ips_ports: count = 0;
if (c$id$orig_h == c$id$resp_h && c$id$orig_p == c$id$resp_p) {
is_sm_ips_ports = 1;
}
local info: Info = [
$ts = network_time(),
$uid = c$uid,
$srcip = c$id$orig_h,
$sport = c$id$orig_p,
$dstip = c$id$resp_h,
$dsport = c$id$resp_p,
$service = "ftp",
$proto = get_protocol(c),
$is_ftp_login = is_ftp_login,
$is_sm_ips_ports = is_sm_ips_ports
];
Log::write(LOG, info);
}
}