-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcount-ports.sh
executable file
·172 lines (152 loc) · 5.15 KB
/
count-ports.sh
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
#!/bin/bash
# Copyright (C) 2018 Stephen Farrell, [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# set -x
# count how many IPs were zmap'd, how many p25 listeners and percentages
TOP="$HOME/data/smtp/runs"
portstrings="p22 p25 p110 p143 p443 p587 p993"
akfile="fingerprints.json"
# some older scans (NZ in particular) have 1-line fingerprints.json
# files that won't work here (we'll get counts of 0), so to fix that
# do: cat fingerprints.json | json_pp >fingerprints_pp.json
# and then this script will read the latter file and count correctly
akfile_pp="fingerprints-pp.json"
# ok this is hacky (well, it is me:-), but we'd like a 2D array for
# counts, so we'll declare an assoc array with the index being the
# combo of runname and port, e.g. counts["PT-20180403-005552,p25"]
# would contain a counter
declare -A overall_arr
declare -A empties_arr
declare -A nonempties_arr
declare -A dodgy_arr
for rundir in $TOP/??-201[89]*
do
runname=`basename $rundir | awk -F'-' '{print $1"-"$2}'`
for port in $portstrings
do
overall_arr["$runname,$port"]=0
empties_arr["$runname,$port"]=0
nonempties_arr["$runname,$port"]=0
done
done
for rundir in $TOP/??-201[89]*
do
runname=`basename $rundir | awk -F'-' '{print $1"-"$2}'`
echo "Checking $rundir"
f2c=$akfile
if [ -f $rundir/$akfile_pp ]
then
f2c=$akfile_pp
fi
if [ -f $rundir/$f2c ]
then
for port in $portstrings
do
overall=`grep -c '"'$port'"[ ]*: {' $rundir/$f2c`
empties=`grep -c '"'$port'"[ ]*:[ ]* {}' $rundir/$f2c`
nonempties=$((overall-empties))
overall_arr["$runname,$port"]=$overall
empties_arr["$runname,$port"]=$empties
nonempties_arr["$runname,$port"]=$nonempties
#echo "$runname: has $overall IPs with $nonempties doing crypto on $port and $empties without"
done
dcount=`grep '"ip"' $rundir/dodgy.json | grep , | sed -e 's/ //g' | sort | uniq | wc -l`
#dcount=`grep -c '^ "ip":' $rundir/dodgy.json`
if [[ "$dcount" == "0" ]]
then
# the NZ run has another format for some reason
dcount=`grep -c '^ "ip" :' $rundir/dodgy.json`
if [[ "$dcount" == "0" ]]
then
# another try...
dcount=`grep '"ip"' $rundir/dodgy.json | grep , | sed -e 's/ //g' | sort | uniq | wc -l`
fi
fi
dodgy_arr[$runname]=$dcount
fi
done
# Ok, we'll do output as a latex table...
cat <<EOF
\\begin{table*}
\\centering
\\caption{Counts of ports with crypto}
\\begin{tabular} { | l | r | r | r | r | r | r | r | r | r | r | r |}
\\hline
\\hline Run & 22 & 25 & 110 & 143 & 443 & 587 & 993 & TLS total & Crypto-IP & No-crypto-IP & Total-IPs \\\\
\\hline
EOF
# totals
declare -A totals
grandportstotal=0
grandsomecryptototal=0
grandnocryptototal=0
grandtotal=0
for port in $portstrings
do
totals[$port]=0
done
# print it out
for rundir in $TOP/??-201[89]*
do
runname=`basename $rundir | awk -F'-' '{print $1"-"$2}'`
echo -n "\\hline $runname "
portstotal=0
for port in $portstrings
do
#echo -n "$runname,$port,"
#echo -n ${overall_arr["$runname,$port"]}
#echo -n ","
#echo -n ${empties_arr["$runname,$port"]}
#echo -n ","
#echo -n ${nonempties_arr["$runname,$port"]}
#echo
totals[$port]=$((${totals[$port]}+${nonempties_arr["$runname,$port"]}))
if [[ "$port" != "p22" ]]
then
# count TLS services totals
portstotal=$((portstotal+${nonempties_arr["$runname,$port"]}))
fi
# latex table line
echo -n " & ${nonempties_arr["$runname,$port"]}"
done
# same for all so p25 will do
iptotal=$((${overall_arr["$runname,p25"]} + ${dodgy_arr[$runname]} ))
echo -n " & $portstotal & ${overall_arr["$runname,p25"]} & ${dodgy_arr[$runname]} & $iptotal"
grandportstotal=$((grandportstotal+portstotal))
grandsomecryptototal=$((grandsomecryptototal+${overall_arr["$runname,p25"]}))
grandnocryptototal=$((grandnocryptototal+${dodgy_arr[$runname]}))
grandtotal=$((grandtotal+$iptotal))
echo "\\\\"
done
echo "\\hline"
echo -n "\\hline Totals "
for port in $portstrings
do
echo -n " & ${totals[$port]}"
done
echo -n " & $grandportstotal & $grandsomecryptototal & $grandnocryptototal & $grandtotal"
echo "\\\\"
echo "\\hline"
# end of table
cat <<EOF
\\end{tabular}
\\label{tab:countports}
\\end{table*}
EOF