-
Notifications
You must be signed in to change notification settings - Fork 0
/
netdfir
657 lines (528 loc) · 16.7 KB
/
netdfir
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/bin/bash
clear
BLACK='\e[30m'
RED='\033[31m'
GREEN='\033[32m'
BLUE='\033[34m'
CYAN='\033[35m'
WHITE='\033[1;37m'
RESET='\033[0m'
BOLD='\033[1m'
BOLD_PINK='\033[1;35m'
YELLOW_BG="\e[1;43m"
RED_BG='\e[1;41m'
DIV="=============================="
GEO_HIGHLIGHT="russia|iran|lithuania|china|cyprus|hong\skong|united\sarab\semirates|$"
AGENT_HIGHLIGHT="python|curl|wget|$"
HOST_HIGHLIGHT="python|simplehttp|$"
FILE_HIGHLIGHT="\.[a-zA-Z]+|$"
PORT_HIGHLIGHT="139|445|135|4444|9001|8080|8001|8000|3389$"
OBJECT_HIGHLIGHT="\.zip|\.rar|\.exe|\.dat|\.dll|$"
PCAP=""
EXPORT=false
ADVERSARY_IP=""
while getopts "r:a:e" opt; do
case $opt in
r)
PCAP="$OPTARG"
;;
e)
EXPORT=true
;;
a)
ADVERSARY_IP="$OPTARG"
;;
esac
done
banner() {
echo
echo -e "\e[31m███╗ ██╗███████╗████████╗ ██████╗ ███████╗██╗██████╗ \e[0m"
echo -e "\e[33m████╗ ██║██╔════╝╚══██╔══╝ ██╔══██╗██╔════╝██║██╔══██╗\e[0m"
echo -e "\e[32m██╔██╗ ██║█████╗ ██║ ██║ ██║█████╗ ██║██████╔╝\e[0m"
echo -e "\e[34m██║╚██╗██║██╔══╝ ██║ ██║ ██║██╔══╝ ██║██╔══██╗\e[0m"
echo -e "\e[35m██║ ╚████║███████╗ ██║ ██████╔╝██║ ██║██║ ██║\e[0m"
echo -e "\e[36m╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝\e[0m"
echo
echo -e "${RED}Perform baselining and analysis on network captures.${RESET}"
echo
}
help() {
banner
echo "Usage: $0 [-r <pcap_file>] [-a <ip_address>] [-e]"
echo
echo "Options:"
echo " -r <pcap_file> Specify the input PCAP file for analysis (required)"
echo " -a <ip_address> Specify an adversaries IP address to flag (optional)"
echo " -e Export files detected in packet streams (optional)"
echo
echo "Example:"
echo " $0 -r capture.pcap"
echo " $0 -r capture.pcap -e"
echo " $0 -r capture.pcap -a 203.103.50.19 -e"
exit
}
if [[ "$PCAP" == *.pcap || "$PCAP" == *.cap ]]; then
DIR="$(realpath "$PCAP".zeek)"
else
help
fi
log_header() {
local heading=$1
local sub_heading=$2
if [ -z "$sub_heading" ]; then
echo -e "${BLUE}${DIV}[${BOLD}${GREEN} ${heading} ${RESET}${BLUE}]${DIV}${RESET}"
else
echo -e "${BLUE}${DIV}|${BOLD}${GREEN} ${heading} - ${sub_heading} ${RESET}${BLUE}|${DIV}${RESET}"
fi
}
log_value() {
local title=$1
local val=$2
echo -e "${RED}${BOLD}[+] ${BLUE}${title}: ${RESET}${val}"
}
zeek_create() {
echo "Generating Zeek Output..."
echo
if [ ! -d "$DIR" ]; then
mkdir "$DIR"
fi
zeek -r "$PCAP"
mv *.log "$DIR"
}
mmdb_check() {
if [ ! -x "$(command -v mmdblookup)" ]; then
echo "Installing mmdblookup..."
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin geoipupdate -y &>/dev/null
fi
if [ ! -f "/tmp/geo-city.mmdb" ]; then
echo "Installing mmdb database..."
wget https://git.io/GeoLite2-City.mmdb -O /tmp/geo-city.mmdb &>/dev/null
fi
}
download_threat_intel() {
if [ ! -f "/tmp/ipsum.txt" ]; then
echo "Downloading threat intel database..."
wget https://raw.githubusercontent.com/stamparm/ipsum/refs/heads/master/ipsum.txt -O /tmp/ipsum.txt &>/dev/null
fi
}
get_first_last() {
awk 'NR==1{first=$0} {last=$0} END{print first; print last}'
}
get_environment() {
echo $(log_header "ENVIRONMENT")
echo
local ad_domain=$(
cat "$DIR"/kerberos.log | \
zeek-cut service | \
grep krbtgt | \
head -n 1 | \
cut -d "/" -f 2
)
echo $(log_value "AD Domain" $ad_domain)
echo
}
get_active_directory() {
echo $(log_header "DOMAIN CONTROLLER")
echo
dc_ip=$(
cat "$DIR"/kerberos.log | \
zeek-cut id.resp_h service | \
grep LDAP | \
head -n 1 | \
awk '{$1=$1; print}' | \
cut -d " " -f 1
)
echo $(log_value "IP Address" $dc_ip)
dc_name=$(
cat "$DIR"/kerberos.log | \
zeek-cut id.resp_h service | \
grep LDAP | \
head -n 1 | \
cut -d'/' -f2 | \
cut -d'.' -f1
)
echo $(log_value "Hostname" $dc_name)
dc_os=$(
tshark -r "$PCAP" -Y "smb.native_os&&ip.src==$dc_ip" -T fields -e smb.native_os | \
grep -v '^\s*$' | \
sort | \
uniq
)
echo $(log_value "Operating System" "$dc_os")
dc_mac=$(
tshark -r "$PCAP" -Y "ip.dst==$dc_ip" -T fields -e eth.dst | head -n 1
)
echo $(log_value "MAC Address" "$dc_mac")
echo
}
get_win_computers() {
echo $(log_header "WINDOWS HOSTS")
echo
local win_hosts=$(
tshark -r "$PCAP" -Y "ip.dst!=$dc_ip&&kerberos.CNameString contains '$'" -T fields -e eth.dst -e ip.dst -e kerberos.CNameString | \
awk '{print toupper($0)}' | \
sed 's/\$//g' | \
column -t | \
sort | \
uniq
)
echo "$win_hosts"
echo
}
get_win_users() {
echo $(log_header "WINDOWS USERS")
echo
local win_users=$(
tshark -r "$PCAP" -Y "kerberos.CNameString&&ip.dst!=$dc_ip&& not (kerberos.CNameString contains '$')" -T fields -e eth.dst -e ip.dst -e kerberos.CNameString | \
column -t | \
sort | \
uniq
)
echo "$win_users"
echo
}
get_dhcp_information() {
echo $(log_header "DHCP")
echo
local dhcp_packets=$(tshark -r "$PCAP" -Y dhcp | wc -l)
if [ $dhcp_packets != 0 ]; then
local dhcp=$(
tshark -r "$PCAP" -Y "dhcp.type==2" -T fields \
-e dhcp.option.domain_name \
-e dhcp.option.domain_name_server \
-e dhcp.option.dhcp_server_id \
-e dhcp.option.subnet_mask \
-e dhcp.option.router | \
column -t | \
sort | \
uniq
)
local dhcp_domain=$(echo $dhcp | cut -d " " -f 1)
local dhcp_dns=$(echo $dhcp | cut -d " " -f 2)
local dhcp_server=$(echo $dhcp | cut -d " " -f 3)
local dhcp_subnet=$(echo $dhcp | cut -d " " -f 4)
local dhcp_router=$(echo $dhcp | cut -d " " -f 5)
echo $(log_value "Domain" $dhcp_domain)
echo $(log_value "DNS" $dhcp_dns)
echo $(log_value "Server" $dhcp_server)
echo $(log_value "Subnet" $dhcp_subnet)
echo $(log_value "Router" $dhcp_router)
echo
echo $(log_value "DHCP Devices")
tshark -r "$PCAP" -Y "dhcp.option.dhcp==5&&ip.src!=0.0.0.0" -T fields -e dhcp.ip.your | \
sort | \
uniq
else
echo "No DHCP Traffic..."
fi
echo
}
check_ip() {
ip=$1
if cat /tmp/ipsum.txt | grep -q "$ip"; then
echo -e "${RED_BG}${WHITE}$ip${RESET}"
elif [ -n "$ADVERSARY_IP" ] && echo "$ip" | grep -q "$ADVERSARY_IP"; then
echo -e "${YELLOW_BG}${BLACK}$ip${RESET}"
else
echo -e "$ip"
fi
}
get_anomolous_dc_activity() {
echo $(log_header "DC ACTIVITY")
echo
local list=""
local traffic=$(
tshark -r "$PCAP" -Y "tcp.srcport<10000&&ip.src==$dc_ip" -T fields -e ip.src -e ip.dst -e tcp.srcport | \
sort | \
uniq -c | \
column -t
)
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local src_ip=$(echo $line | cut -d " " -f 2)
local dst_ip=$(echo $line | cut -d " " -f 3)
local port=$(echo $line | cut -d " " -f 4)
local country=$(
mmdblookup -f /tmp/geo-city.mmdb -i $dst_ip country names en 2>/dev/null | \
cut -d '"' -f 2 | \
egrep -v '^$' | \
egrep -i --color=always $GEO_HIGHLIGHT
)
local src_ip_stat=$(check_ip $src_ip)
local dst_ip_stat=$(check_ip $dst_ip)
list+="$occurences,$src_ip_stat -> $dst_ip_stat,:$port,$country\n"
done <<< "$traffic"
echo $(log_value "Outgoing Traffic")
echo -e "$list" | column -t -s "," | sort -k 1 -n -r
echo
}
get_malicious_ips() {
echo $(log_header "IP ADDRESSES")
echo
list=""
traffic=$(
tshark -r "$PCAP" -T fields -e ip.addr | \
tr ',' '\n' | \
sort | \
uniq -c | \
column -t
)
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local ip=$(echo $line | cut -d " " -f 2)
local ip_stat=$(check_ip $ip)
local country=$(
mmdblookup -f /tmp/geo-city.mmdb -i $ip country names en 2>/dev/null | \
cut -d '"' -f 2 | \
egrep -v '^$' | \
egrep -i --color=always $GEO_HIGHLIGHT
)
list+="$occurences,$ip_stat,$country\n"
done <<< $traffic
echo -e "$list" | column -t -s "," | sort -k 1 -n -r | awk '$3 !=""'
echo
}
get_user_agents() {
echo $(log_header "USER AGENTS")
echo
tshark -r "$PCAP" -Y http.user_agent -T fields -e ip.src -e http.user_agent | \
egrep -i --color=always $AGENT_HIGHLIGHT | \
sort | \
uniq -c | \
sort -r
echo
}
get_server_hosts() {
echo $(log_header "SERVER HOSTS")
echo
tshark -r "$PCAP" -Y http.server -T fields -e ip.src -e http.server | \
egrep -i --color=always $HOST_HIGHLIGHT | \
sort | \
uniq -c | \
sort -r
echo
}
get_uris() {
echo $(log_header "REQUEST URI")
echo
list=""
uris=$(
tshark -r "$PCAP" -Y http.request.uri -T fields -e ip.dst -e tcp.dstport -e http.request.uri | \
egrep -i --color=always $FILE_HIGHLIGHT | \
sort | \
uniq -c | \
column -t
)
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local ip=$(echo $line | cut -d " " -f 2)
local port=$(echo $line | cut -d " " -f 3)
local path=$(echo $line | cut -d " " -f 4 | awk 'length > 130{$0=substr($0,0,131)"..."}1')
local ip_stat=$(check_ip $ip)
list+="$occurences,$ip_stat:$port,$path\n"
done <<< $uris
echo -e "$list" | column -t -s "," | sort -k 1 -n -r
echo
}
get_external_connections() {
echo $(log_header "EXTERNAL CONNECTIONS")
echo
local list=""
local ports=$(
tshark -r "$PCAP" -T fields -e ip.dst -e tcp.srcport | \
awk '$2 <= 10000' | \
sort | \
uniq -c | \
column -t
)
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local ip=$(echo $line | cut -d " " -f 2)
local port=$(echo $line | cut -d " " -f 3 | egrep --color=always $PORT_HIGHLIGHT)
local country=$(
mmdblookup -f /tmp/geo-city.mmdb -i $ip country names en 2>/dev/null | \
cut -d '"' -f 2 | \
egrep -v '^$' | \
egrep -i --color=always $GEO_HIGHLIGHT
)
local ip_stat=$(check_ip $ip)
list+="$occurences,$ip_stat,$port,$country\n"
done <<< $ports
echo -e "$list" | column -t -s "," | sort -k 1 -n -r | awk '$4 !=""'
echo
}
get_http_objects() {
echo $(log_header "HTTP OBJECTS")
echo
rm -rf "$DIR"/http.files
mkdir http.files
tshark -r "$PCAP" --export-objects http,http.files &>/dev/null
md5sum http.files/*.* | \
sed 's/http\.files/ /g' | \
tr '/' ' ' | \
column -t | \
egrep -i --color=always $OBJECT_HIGHLIGHT
mv http.files "$DIR"
echo
}
get_smb_objects() {
echo $(log_header "SMB OBJECTS")
echo
rm -rf "$DIR"/smb.files
mkdir smb.files
tshark -r "$PCAP" --export-objects smb,smb.files &>/dev/null
md5sum smb.files/*.* | \
sed 's/smb\.files/ /g' | \
tr '/' ' ' | \
column -t | \
egrep -i --color=always $OBJECT_HIGHLIGHT
mv smb.files "$DIR"
echo
}
get_ports() {
echo $(log_header "PORTS")
echo
tshark -r "$PCAP" -Y "tcp.srcport<10000" -T fields -e tcp.srcport | \
sort | \
uniq -c | \
sort -r
echo
}
get_dns_hostnames() {
echo $(log_header "DNS HOSTNAMES")
echo
tshark -r "$PCAP" -Y dns -T fields -e dns.qry.name | \
egrep -v "in-addr.arpa|ip6.arpa" | \
sort | \
uniq -c | \
sort -k 1 -n -r
echo
}
get_smb_traffic() {
echo $(log_header "SMB TRAFFIC")
echo
local traffic=$(
tshark -r "$PCAP" -t ud -Y smb -T fields -e _ws.col.Time -e ip.addr | \
column -t | \
sort -k 1 -n
)
local time=$(echo "$traffic" | awk '{print $1, $2}')
local ips=$(echo "$traffic" | awk '{print $3}' | tr ',' '\n' | sort | uniq -c)
local number=$(echo "$traffic" | wc -l)
local first=$(echo "$time" | head -n 1 | cut -d "." -f 1)
local last=$(echo "$time" | tail -n 1 | cut -d "." -f 1)
echo $(log_value "Packets" $number)
echo $(log_value "First Packet" "$first")
echo $(log_value "Last Packet" "$last")
echo
echo $(log_value "IP Addresses")
local list=""
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local ip=$(echo $line | cut -d " " -f 2)
local country=$(
mmdblookup -f /tmp/geo-city.mmdb -i $ip country names en 2>/dev/null | \
cut -d '"' -f 2 | \
egrep -v '^$' | \
egrep -i --color=always $GEO_HIGHLIGHT
)
local ip_stat=$(check_ip $ip)
list+=" $occurences,$ip_stat,$country\n"
done <<< "$ips"
echo -e "$list" | column -t -s "," | sort -k 1 -n -r
echo
}
get_ssh_traffic() {
echo $(log_header "SSH TRAFFIC")
echo
local traffic=$(
tshark -r "$PCAP" -t ud -Y ssh -T fields -e _ws.col.Time -e ip.addr | \
column -t | \
sort -k 1 -n
)
local time=$(echo "$traffic" | awk '{print $1, $2}')
local ips=$(echo "$traffic" | awk '{print $3}' | tr ',' '\n' | sort | uniq -c)
local number=$(echo "$traffic" | wc -l)
local first=$(echo "$time" | head -n 1 | cut -d "." -f 1)
local last=$(echo "$time" | tail -n 1 | cut -d "." -f 1)
echo $(log_value "Packets" $number)
echo $(log_value "First Packet" "$first")
echo $(log_value "Last Packet" "$last")
echo
echo $(log_value "IP Addresses")
local list=""
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local ip=$(echo $line | cut -d " " -f 2)
local country=$(
mmdblookup -f /tmp/geo-city.mmdb -i $ip country names en 2>/dev/null | \
cut -d '"' -f 2 | \
egrep -v '^$' | \
egrep -i --color=always $GEO_HIGHLIGHT
)
local ip_stat=$(check_ip $ip)
list+=" $occurences,$ip_stat,$country\n"
done <<< "$ips"
echo -e "$list" | column -t -s "," | sort -k 1 -n -r
echo
}
get_http_traffic() {
echo $(log_header "HTTP TRAFFIC")
echo
local traffic=$(
tshark -r "$PCAP" -t ud -Y "http" -T fields -e _ws.col.Time -e ip.addr | \
column -t | \
sort -k 1 -n
)
local time=$(echo "$traffic" | awk '{print $1, $2}')
local ips=$(echo "$traffic" | awk '{print $3}' | tr ',' '\n' | sort | uniq -c)
local number=$(echo "$traffic" | wc -l)
local first=$(echo "$time" | head -n 1 | cut -d "." -f 1)
local last=$(echo "$time" | tail -n 1 | cut -d "." -f 1)
echo $(log_value "Packets" $number)
echo $(log_value "First Packet" "$first")
echo $(log_value "Last Packet" "$last")
echo
echo $(log_value "IP Addresses")
local list=""
while IFS= read -r line; do
local occurences=$(echo $line | cut -d " " -f 1)
local ip=$(echo $line | cut -d " " -f 2)
local country=$(
mmdblookup -f /tmp/geo-city.mmdb -i $ip country names en 2>/dev/null | \
cut -d '"' -f 2 | \
egrep -v '^$' | \
egrep -i --color=always $GEO_HIGHLIGHT
)
local ip_stat=$(check_ip $ip)
list+=" $occurences,$ip_stat,$country\n"
done <<< "$ips"
echo -e "$list" | column -t -s "," | sort -k 1 -n -r
echo
}
execute_all() {
banner
mmdb_check
download_threat_intel
zeek_create
get_environment
get_active_directory
get_win_computers
get_win_users
get_dhcp_information
get_anomolous_dc_activity
get_malicious_ips
get_user_agents
get_server_hosts
get_uris
get_ports
get_dns_hostnames
get_smb_traffic
get_ssh_traffic
get_http_traffic
if [ $EXPORT == true ]; then
get_http_objects
get_smb_objects
fi
}
execute_all