Subversion Repositories ALCASAR

Rev

Rev 3249 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2189 tom.houday 1
#!/bin/bash
2
# $Id: alcasar-activity_report.sh 3252 2025-02-21 18:40:30Z rexy $
3
#
2138 richard 4
# Create an activity report for ALCASAR every week (sunday at 5.35 pm --> see cron.d).
5
# We read configuration files and logs to create cool charts.
2190 tom.houday 6
# Written by Raphaël PION, Rexy & Tom HOUDAYER
2009 raphael.pi 7
 
2474 tom.houday 8
CONF_FILE='/usr/local/etc/alcasar.conf'
2138 richard 9
# files
10
DIR_TMP="/var/tmp"
11
TMP_AV="$DIR_TMP/av_count.txt"
12
TMP_BL="$DIR_TMP/bl_count.txt"
13
TMP_BL_WEEK="$DIR_TMP/bl_count_week.txt"
14
TMP_BL_WEEK_CAT="$DIR_TMP/bl_count_week_cat.txt"
2009 raphael.pi 15
 
2138 richard 16
# Model loaded to create charts
17
DIR_BUILD="/var/www/html/acc/manager/activity_report/"
18
MODEL_CHARTJS="$DIR_BUILD/models/Chart.report.js"
19
MODEL_TABINFO="$DIR_BUILD/models/tabinfo.html"
2009 raphael.pi 20
 
2138 richard 21
# Where the report will be created.
22
HTML_REPORT="$DIR_BUILD/alcasar-report-$(date +%F).html"
2009 raphael.pi 23
 
2138 richard 24
# TIME VALUE
2009 raphael.pi 25
C_TS=$(date +"%s") #current timestamp
26
MAX_DAY_AGO=7
27
SECS_AGO=$(date --date="$MAX_DAY_AGO days ago" +"%s") #timestamp ago
28
STEP_TS=$((C_TS-$SECS_AGO)) #timestamp between current timestamp and SECS_AGO
29
 
2138 richard 30
# PRIVATE IP OF ALCASAR
2474 tom.houday 31
PRIVATE_IP=$(grep ^PRIVATE_IP= $CONF_FILE | cut -d'=' -f2 | cut -d'/' -f1)
2009 raphael.pi 32
 
2138 richard 33
# COLOR for charts
2009 raphael.pi 34
COLOR="'#ff0000','#3333cc','#009933','#993300','#1720EE','#D30229','#8D726D','#41C4E4','#8574F4','#A0BC1A','#BFDC1F','#5ADDC3','#B05744','#CD9319','#8CA39B','#D4AA1C','#A76752','#B03088','#445E87','#70424D','#D118C3','#46ABEF','#E9F197','#AEC0D4','#755C79','#94BBD7','#E2E9DC','#8B68D0','#F7EC7C','#1F16B8','#F4DA0A','#2EC17A','#E06483','#48B342','#F510CD','#9B2662','#180E98','#988FC1','#209E4E','#034240','#FDB142','#36B445','#CDD5C9','#6FA0DE','#EE2206','#204E19','#15FC93','#161ECE','#83D33B','#11A44A','#B7BF6C','#87274C','#B52C4F','#AD2805','#427E6C','#91341A','#191315','#FCB290','#13D3CD','#90F0E6','#C870C9','#AD2C14','#201D2A','#E4DB79','#90A919','#FE17FE','#09B35C','#88D950','#3440FC','#A9D42F','#E2DFAC','#DA69EC','#67430A','#43E94E','#5F7349','#22CF16','#CF038F','#0F6427','#F7AD0F','#C5E382','#DB49B6','#F760BF','#0BE701','#EF88D8','#79E6D7','#8A2D3D','#435A30','#A3C8AC','#99B118','#A929FF','#08A36D','#0A1654','#6F8283','#E1CA3E','#3E8577','#580FB6','#DB0E16','#386CBE','#FA0C43','#B713C9'"
35
 
2138 richard 36
# Values to create new htdigest user to consult statistique of ACC
2009 raphael.pi 37
DIR_KEY="/usr/local/etc/digest"
2644 lucas.echa 38
tmp_account="tmp_activityreport"
2009 raphael.pi 39
realm="ALCASAR Control Center (ACC)"
40
password=$(openssl rand -base64 32) #random password (length : 32)
41
SED="/usr/bin/sed -i "
2138 richard 42
TMP_STATS="$DIR_TMP/stats.html"
43
TMP_STATS_2="$DIR_TMP/stats2.html"
2009 raphael.pi 44
 
2138 richard 45
# if empty logs, replace charts by text.
2009 raphael.pi 46
ENABLE_BL=0
47
ENABLE_BL_WEEK=0
48
ENABLE_AV=0
49
if [ -e $TMP_AV ]
50
then
51
	rm $TMP_AV
52
fi
53
if [ -e $TMP_BL ]
54
then
2189 tom.houday 55
	rm $TMP_BL
2009 raphael.pi 56
fi
57
if [ -e $TMP_BL_WEEK ]
58
then
2189 tom.houday 59
	rm $TMP_BL_WEEK
2009 raphael.pi 60
fi
61
if [ -e $TMP_BL_WEEK_CAT ]
62
then
2189 tom.houday 63
	rm $TMP_BL_WEEK_CAT
2009 raphael.pi 64
fi
65
if [ -e $HTML_REPORT ]
66
then
2189 tom.houday 67
	rm $HTML_REPORT
2009 raphael.pi 68
fi
69
echo "<!doctype html>" >> $HTML_REPORT
70
echo "<html>" >> $HTML_REPORT
71
echo "<head>" >> $HTML_REPORT
2189 tom.houday 72
echo "<meta charset=\"utf-8\">" >> $HTML_REPORT
2009 raphael.pi 73
echo "<title>ALCASAR report</title>" >> $HTML_REPORT
2189 tom.houday 74
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../../../css/bootstrap.min.css\">" >> $HTML_REPORT
75
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../../../css/report.css\">" >> $HTML_REPORT
2317 tom.houday 76
echo "<script src=\"../../../js/Chart.bundle.min.js\"></script>" >> $HTML_REPORT
2189 tom.houday 77
echo "<script src=\"../../../js/jquery.min.js\"></script>" >> $HTML_REPORT
2009 raphael.pi 78
echo "</head>" >> $HTML_REPORT
79
echo "<body>" >> $HTML_REPORT
3252 rexy 80
echo "<h1><center>Rapport d'activité ALCASAR</center></h1>" >> $HTML_REPORT
81
echo "<h2><center>$(grep ^ORGANISM= $CONF_FILE | cut -d'=' -f2-)</center></h2>" >> $HTML_REPORT
2189 tom.houday 82
echo "<i><p style=\"text-align: right;\">Date de création $(date +%F)</p></i>" >> $HTML_REPORT
83
echo "<font size=\"1\">" >> $HTML_REPORT
2009 raphael.pi 84
 
85
######################TABINFO######################
86
echo "Create information about system and ALCASAR"
87
#contain every information about ALCASAR configuration, system and last update
88
 
89
cat $MODEL_TABINFO | while read LINE_HTML
90
do
2688 lucas.echa 91
	if [ "$(echo $LINE_HTML | grep 'XXORGXX' | wc -l)" -eq 1 ]
92
	then
93
		VALUE=$(grep ^ORGANISM= $CONF_FILE | cut -d'=' -f2-)
94
		echo ${LINE_HTML/XXORGXX/$VALUE} >> $HTML_REPORT
2009 raphael.pi 95
 
2688 lucas.echa 96
	elif [ "$(echo $LINE_HTML | grep 'XXINSTALLXX' | wc -l)" -eq 1 ]
2009 raphael.pi 97
	then
2688 lucas.echa 98
		VALUE=$(grep ^INSTALL_DATE= $CONF_FILE | cut -d'=' -f2)
99
		echo ${LINE_HTML/XXINSTALLXX/$VALUE} >> $HTML_REPORT
2009 raphael.pi 100
 
2688 lucas.echa 101
	elif [ "$(echo $LINE_HTML | grep 'XXAVERSIONXX' | wc -l)" -eq 1 ]
102
	then
103
		VALUE=$(grep ^VERSION= $CONF_FILE | cut -d'=' -f2)
104
		echo ${LINE_HTML/XXAVERSIONXX/$VALUE} >> $HTML_REPORT
105
 
106
	elif [ "$(echo $LINE_HTML | grep 'XXIP_PUBLICXX' | wc -l)" -eq 1 ]
107
	then
108
		VALUE=$(grep ^PUBLIC_IP= $CONF_FILE | cut -d'=' -f2)
109
		echo ${LINE_HTML/XXIP_PUBLICXX/$VALUE} >> $HTML_REPORT
110
 
111
	elif [ "$(echo $LINE_HTML | grep 'XXIP_PRIVEXX' | wc -l)" -eq 1 ]
112
	then
113
		VALUE=$(grep ^PRIVATE_IP= $CONF_FILE | cut -d'=' -f2)
114
		echo ${LINE_HTML/XXIP_PRIVEXX/$VALUE} >> $HTML_REPORT
115
 
116
	elif [ "$(echo $LINE_HTML | grep 'XXGWXX' | wc -l)" -eq 1 ]
117
	then
118
		VALUE=$(grep ^GW= $CONF_FILE | cut -d'=' -f2)
119
		echo ${LINE_HTML/XXGWXX/$VALUE} >> $HTML_REPORT
120
 
121
	elif [ "$(echo $LINE_HTML | grep 'XXDNS1XX' | wc -l)" -eq 1 ]
122
	then
123
		VALUE=$(grep ^DNS1= $CONF_FILE | cut -d'=' -f2)
124
		echo ${LINE_HTML/XXDNS1XX/$VALUE} >> $HTML_REPORT
125
 
126
	elif [ "$(echo $LINE_HTML | grep 'XXDNS2XX' | wc -l)" -eq 1 ]
127
	then
128
		VALUE=$(grep ^DNS2= $CONF_FILE | cut -d'=' -f2)
129
		echo ${LINE_HTML/XXDNS2XX/$VALUE} >> $HTML_REPORT
130
 
131
	elif [ "$(echo $LINE_HTML | grep 'XXHOSTXX' | wc -l)" -eq 1 ]
132
	then
133
		VALUE=$(hostname)
134
		echo ${LINE_HTML/XXHOSTXX/$VALUE} >> $HTML_REPORT
135
 
136
	elif [ "$(echo $LINE_HTML | grep 'XXOS_VERSIONXX' | wc -l)" -eq 1 ]
137
	then
138
		VALUE=$(echo "$(uname -r) [ $(uname -m) ]")
139
		echo ${LINE_HTML/XXOS_VERSIONXX/$VALUE} >> $HTML_REPORT
140
 
141
	elif [ "$(echo $LINE_HTML | grep 'XXREBOOTXX' | wc -l)" -eq 1 ]
142
	then
143
		VALUE=$(who -b | cut -d' ' -f12-)
144
		echo ${LINE_HTML/XXREBOOTXX/$VALUE} >> $HTML_REPORT
145
 
146
	elif [ "$(echo $LINE_HTML | grep 'XXMAJCLAMAVXX' | wc -l)" -eq 1 ]
147
	then
3249 rexy 148
		VALUE=$(date -d "$(rpm -qa --queryformat "%{installtime} %{name}\n" | grep -E "clamav-db" | cut -d' ' -f1 )" "+%Y-%m-%d %H:%M:%S")
2688 lucas.echa 149
		echo ${LINE_HTML/XXMAJCLAMAVXX/$VALUE} >> $HTML_REPORT
3249 rexy 150
echo
2688 lucas.echa 151
	elif [ "$(echo $LINE_HTML | grep 'XXMAJBLXX' | wc -l)" -eq 1 ]
152
	then
153
		VALUE=$(cat /etc/e2guardian/lists/blacklists/README | grep 'Last version' | cut -d' ' -f4-6)
154
		echo ${LINE_HTML/XXMAJBLXX/$VALUE} >> $HTML_REPORT
155
 
156
	elif [ "$(echo $LINE_HTML | grep 'XXRPMXX' | wc -l)" -eq 1 ]
157
	then
158
		#show every ALCASAR RPM updated since X day ago
3252 rexy 159
		#get timestamp of X day ago. Then we get every packets which have been updated since this date.
2688 lucas.echa 160
		if [ "$(rpm -qa --queryformat '%{installtime} %{name} %{version}\n' | awk -v seuil="$SECS_AGO" '$1 > seuil' | sort -n | grep -E "$PACKAGE" | wc -l)" -gt 1 ]
161
		then
3230 rexy 162
			PACKAGE='php|apache|iptables|unbound|radius|nfdump|e2guardian|clamav|ulogd|chilli|fail2ban|openssh|ipt-netflow|wget|mariadb|gnupg|openssl'
2688 lucas.echa 163
			rpm -qa --queryformat '%{installtime} %{name} %{version}\n' | awk -v seuil="$SECS_AGO" '$1 > seuil' | sort -n | grep -E "$PACKAGE" | while read RPM_ALCASAR
164
			do
165
				RPM_TIMESTAMP=$(echo $RPM_ALCASAR | cut -d' ' -f1)
166
				RPM_DATE=$(date -d "@$(echo $RPM_TIMESTAMP)" "+%Y-%m-%d %H:%M:%S")
167
				RPM_NAME=$(echo $RPM_ALCASAR | cut -d' ' -f2)
168
				RPM_VERSION=$(echo $RPM_ALCASAR | cut -d' ' -f3)
169
 
170
				echo "<tr>" >> $HTML_REPORT
171
				echo "<td>$RPM_NAME</td>" >> $HTML_REPORT
172
				echo "<td>$RPM_DATE</td>" >> $HTML_REPORT
173
				echo "<td>$RPM_VERSION</td>" >> $HTML_REPORT
174
				echo "</tr>" >> $HTML_REPORT
175
			done
176
		else
177
			echo "<tr><td colspan=\"3\">Pas de RPM mis à jour cette semaine</td></tr>" >> $HTML_REPORT
178
		fi
2009 raphael.pi 179
	else
2688 lucas.echa 180
		echo $LINE_HTML >> $HTML_REPORT
2009 raphael.pi 181
	fi
182
done
183
 
184
######################BL WEBSITE SINCE INSTALLATION######################
185
echo "Create BL website since the installation of ALCASAR"
186
#find data
187
 
188
#decompress every logs
2688 lucas.echa 189
if [ "$(ls -1 /var/log/unbound/unbound-blacklist.log.*.gz 2>/dev/null | wc -l)" -ge 1 ]
2009 raphael.pi 190
then
2688 lucas.echa 191
	gunzip -d unbound-blacklist.log.*.gz
2009 raphael.pi 192
fi
193
 
194
#convert logs date in timestamp and find categories of blacklisted website
2688 lucas.echa 195
tmp_log=$(mktemp)
196
for FILE in /var/log/unbound/unbound-blacklist.log*
2009 raphael.pi 197
do
2688 lucas.echa 198
	grep -E "info: [^ ]+ typetransparent $PRIVATE_IP" /var/log/unbound/unbound-blacklist.log > $tmp_log
2009 raphael.pi 199
	while read LOG_BL
200
	do
2688 lucas.echa 201
		# find the current blacklisted category
202
		website_bl=$(echo $LOG_BL | cut -d' ' -f4)
203
		website_bl=${website_bl%?} # remove the last character
204
 
205
		#we convert www.test.co.uk => test.co.uk to find the category of this website
206
		if [ "$(grep -o '\.' <<< "$website_bl" | wc -l)" -ge "2" ]
2454 tom.houday 207
		then
2688 lucas.echa 208
			website_bl=$(echo $website_bl | cut -d'.' -f2-)
209
		fi
210
		#get BL category
211
		categorie_bl=$(grep -Rl "$website_bl" /usr/local/share/unbound-bl-enabled/ | cut -d'/' -f6 | head -1)
212
		CURRENT_TS=$(echo $LOG_BL | cut -d '[' -f2 | cut -d ']' -f1)
213
		echo "$CURRENT_TS:$categorie_bl:" >> $TMP_BL
214
	done < $tmp_log
2009 raphael.pi 215
done
2688 lucas.echa 216
rm $tmp_log
2009 raphael.pi 217
 
218
#if data exists, create this section in html document
219
if [ -e $TMP_BL ]
220
then
221
	ENABLE_BL=1
222
	#count every BL website consulted since installation (maximum 1 year)
223
	DATE_END=$(cat $TMP_BL | cut -d':' -f1 | sort -n | head -1 )
224
	for TS in $(seq $C_TS -$STEP_TS $DATE_END)
225
	do
226
		DATE_1=$TS
227
		DATE_2=$((TS-$STEP_TS))
2688 lucas.echa 228
		COUNT_BL_INSTALLATION=0
2009 raphael.pi 229
		for LINE in $(cat $TMP_BL)
230
		do
231
			TS_FILE=$(echo $LINE | cut -d':' -f1)
2688 lucas.echa 232
 
233
			if [ "$TS_FILE" -le "$DATE_1" ] && [ "$TS_FILE" -ge "$DATE_2" ]
2454 tom.houday 234
			then
2009 raphael.pi 235
				COUNT_BL_INSTALLATION=$((COUNT_BL_INSTALLATION+1))
236
			fi
237
		done
238
		VALUE_BL_INSTALLATION_LABEL="'$(date -d @$DATE_2 "+%Y-%m-%d" )', $VALUE_BL_INSTALLATION_LABEL"
239
		VALUE_BL_INSTALLATION_DATA="$COUNT_BL_INSTALLATION, $VALUE_BL_INSTALLATION_DATA"
240
	done
241
 
242
	#create Antivirus section in html document
243
	NAME_BL_INSTALLATION='chart_bl_installation'
244
	CONF_BL_INSTALLATION='config_bl_installation'
245
	echo "<center>" >> $HTML_REPORT
246
	echo "<canvas id='$NAME_BL_INSTALLATION' width='450' height='450'></canvas>" >> $HTML_REPORT
247
	echo "</center>" >> $HTML_REPORT
248
 
249
	#create chart bar in html file with javascript (chartjs.com)
250
	echo "<script>" >> $HTML_REPORT
251
	cat $MODEL_CHARTJS | while read LINE_JS
252
	do
253
		#name of variable
2688 lucas.echa 254
		if [ "$(echo $LINE_JS | grep 'XXCONFXX' | wc -l)" -eq 1 ]
2009 raphael.pi 255
		then
256
			echo ${LINE_JS/XXCONFXX/$CONF_BL_INSTALLATION} >> $HTML_REPORT
257
		#chart type
2688 lucas.echa 258
		elif [ "$(echo $LINE_JS | grep 'XXTYPEXX' | wc -l)" -eq 1 ]
2009 raphael.pi 259
		then
260
			echo ${LINE_JS/XXTYPEXX/bar} >> $HTML_REPORT
261
		#chart title
2688 lucas.echa 262
		elif [ "$(echo $LINE_JS | grep 'XXTITLEXX' | wc -l)" -eq 1 ]
2454 tom.houday 263
		then
2009 raphael.pi 264
			echo ${LINE_JS/XXTITLEXX/"Sites bloqués au total"} >> $HTML_REPORT
265
		#chart data
2688 lucas.echa 266
		elif [ "$(echo $LINE_JS | grep 'XXDATAXX' | wc -l)" -eq 1 ]
2009 raphael.pi 267
		then
268
			echo ${LINE_JS/XXDATAXX/$VALUE_BL_INSTALLATION_DATA} >> $HTML_REPORT
269
		#color
2688 lucas.echa 270
		elif [ "$(echo $LINE_JS | grep 'XXCOLORXX' | wc -l)" -eq 1 ]
2009 raphael.pi 271
		then
272
			echo ${LINE_JS/XXCOLORXX/$COLOR} >> $HTML_REPORT
273
		#labels
2688 lucas.echa 274
		elif [ "$(echo $LINE_JS | grep 'XXLABELSXX' | wc -l)" -eq 1 ]
2009 raphael.pi 275
		then
276
			echo ${LINE_JS/XXLABELSXX/$VALUE_BL_INSTALLATION_LABEL} >> $HTML_REPORT
2688 lucas.echa 277
		elif [ "$(echo $LINE_JS | grep 'XXLEGENDXX' | wc -l)" -eq 1 ]
2009 raphael.pi 278
		then
279
			echo ${LINE_JS/XXLEGENDXX/false} >> $HTML_REPORT
280
		#display value of Y axis, only useful for chart bar
2688 lucas.echa 281
		elif [ "$(echo $LINE_JS | grep 'XXCOMMENT-BEGINXX' | wc -l)" -eq 1 ]
2009 raphael.pi 282
		then
283
			echo "" >> $HTML_REPORT
284
		#display value of Y axis, only useful for chart bar
2688 lucas.echa 285
		elif [ "$(echo $LINE_JS | grep 'XXCOMMENT-ENDXX' | wc -l)" -eq 1 ]
2009 raphael.pi 286
		then
287
			echo "" >> $HTML_REPORT
2688 lucas.echa 288
		elif [ "$(echo $LINE_JS | grep 'XXYLABELXX' | wc -l)" -eq 1 ]
2009 raphael.pi 289
		then
2013 raphael.pi 290
			echo "\"Nombre de site bloqué par la blacklist\"" >> $HTML_REPORT
2009 raphael.pi 291
		else
292
			echo $LINE_JS >> $HTML_REPORT
293
		fi
294
	done
295
	echo "</script>" >> $HTML_REPORT
296
else
3252 rexy 297
	echo "<h3>Aucune activité de la Blacklist depuis l'installation.</h3>" >> $HTML_REPORT
2009 raphael.pi 298
fi
299
 
300
 
301
 
2688 lucas.echa 302
######################Unbound BLACKLIST######################
2009 raphael.pi 303
echo "Create BL website since $MAX_DAY_AGO days"
304
 
305
#if data exists, create BL section in html document
306
if [ -e $TMP_BL ]
307
then
308
	ENABLE_BL_WEEK=1
309
	#find data
310
	#count every BL website consulted since DAYS_AGO
311
	DATE_1=$C_TS
312
	DATE_2=$((DATE_1-$STEP_TS))
2287 tom.houday 313
	touch $TMP_BL_WEEK
2009 raphael.pi 314
	for LINE in $(cat $TMP_BL)
315
	do
316
		TS_FILE=$(echo $LINE | cut -d':' -f1)
317
		#select only elements between DATE_1 and DATE_2
2688 lucas.echa 318
		if [ "$TS_FILE" -le "$DATE_1" ] && [ "$TS_FILE" -ge "$DATE_2" ]
2454 tom.houday 319
		then
2009 raphael.pi 320
			echo $LINE >> $TMP_BL_WEEK
321
		fi
322
	done
323
 
324
	#then we count every occurence for each category in TMP_BL_WEEK
2688 lucas.echa 325
	for CAT in $(ls /usr/local/share/unbound-bl/ -1 | cut -d'.' -f1)
2009 raphael.pi 326
	do
2189 tom.houday 327
		echo "$CAT:$(grep -o ":$CAT:" <<< "$(cat $TMP_BL_WEEK)" | wc -l):" >> $TMP_BL_WEEK_CAT
2009 raphael.pi 328
	done
329
 
330
	#we sort by number of occurence and we take the top 10 BL categories
331
	for LINE in $(sort -t':' -k2 -rn $TMP_BL_WEEK_CAT | head -n 10)
332
	do
333
		DATA=$(echo $LINE | cut -d':' -f2)
334
		LABEL=$(echo $LINE | cut -d':' -f1)
335
		if [ $DATA -ne 0 ]
336
		then
2189 tom.houday 337
			VALUE_BL_DATA="$VALUE_BL_DATA $DATA, "
338
			VALUE_BL_LABEL="$VALUE_BL_LABEL '$LABEL ($DATA)',"
2009 raphael.pi 339
		fi
340
	done
341
 
342
	#get other categories (sum them all)
2688 lucas.echa 343
	if [ "$(cat $TMP_BL_WEEK_CAT | cut -d':' -f2 | sort -k1 -rn | tail -n+$(($(echo $VALUE_BL_DATA | wc -w)+1)) | paste -sd+ | bc)" -gt 0 ]
2189 tom.houday 344
	then
2454 tom.houday 345
		VALUE_BL_DATA="$VALUE_BL_DATA $(cat $TMP_BL_WEEK_CAT | cut -d':' -f2 | sort -k1 -rn | tail -n+$(($(echo $VALUE_BL_DATA | wc -w)+1)) | paste -sd+ | bc)"
346
		VALUE_BL_LABEL="$VALUE_BL_LABEL 'autre ($(cat $TMP_BL_WEEK_CAT | cut -d':' -f2 | sort -k1 -rn | tail -n+$(($(echo $VALUE_BL_DATA | wc -w)+1)) | paste -sd+ | bc))'"
2189 tom.houday 347
	fi
2009 raphael.pi 348
 
349
	#create chart pie in html file with javascript (chartjs.com)
350
	NAME_BL='chart_bl'
351
	CONF_BL='config_bl'
352
	echo "<center>" >> $HTML_REPORT
353
	echo "<canvas id='$NAME_BL' width='450' height='450' ></canvas>" >> $HTML_REPORT
354
	echo "</center>" >> $HTML_REPORT
355
	echo "<script>" >> $HTML_REPORT
356
	cat $MODEL_CHARTJS | while read LINE_JS
357
	do
358
		#variable name
2688 lucas.echa 359
		if [ "$(echo $LINE_JS | grep 'XXCONFXX' | wc -l)" -eq 1 ]
2009 raphael.pi 360
		then
361
			echo ${LINE_JS/XXCONFXX/$CONF_BL} >> $HTML_REPORT
362
		#chart type
2688 lucas.echa 363
		elif [ "$(echo $LINE_JS | grep 'XXTYPEXX' | wc -l)" -eq 1 ]
2009 raphael.pi 364
		then
365
			echo ${LINE_JS/XXTYPEXX/pie} >> $HTML_REPORT
366
		#graph title
2688 lucas.echa 367
		elif [ "$(echo $LINE_JS | grep 'XXTITLEXX' | wc -l)" -eq 1 ]
2009 raphael.pi 368
		then
2189 tom.houday 369
			echo ${LINE_JS/XXTITLEXX/"Sites bloqués cette semaine"} >> $HTML_REPORT
2009 raphael.pi 370
		#chart data
2688 lucas.echa 371
		elif [ "$(echo $LINE_JS | grep 'XXDATAXX' | wc -l)" -eq 1 ]
2009 raphael.pi 372
		then
373
			echo ${LINE_JS/XXDATAXX/$VALUE_BL_DATA} >> $HTML_REPORT
374
		#color
2688 lucas.echa 375
		elif [ "$(echo $LINE_JS | grep 'XXCOLORXX' | wc -l)" -eq 1 ]
2009 raphael.pi 376
		then
377
			echo ${LINE_JS/XXCOLORXX/$COLOR} >> $HTML_REPORT
378
		#labels
2688 lucas.echa 379
		elif [ "$(echo $LINE_JS | grep 'XXLABELSXX' | wc -l)" -eq 1 ]
2009 raphael.pi 380
		then
381
			echo ${LINE_JS/XXLABELSXX/$VALUE_BL_LABEL} >> $HTML_REPORT
382
		#display legend, only useful for chart pie
2688 lucas.echa 383
		elif [ "$(echo $LINE_JS | grep 'XXLEGENDXX' | wc -l)" -eq 1 ]
2009 raphael.pi 384
		then
385
			echo ${LINE_JS/XXLEGENDXX/true} >> $HTML_REPORT
386
		#display value of Y axis, only useful for chart bar
2688 lucas.echa 387
		elif [ "$(echo $LINE_JS | grep 'XXCOMMENT-BEGINXX' | wc -l)" -eq 1 ]
2009 raphael.pi 388
		then
389
			echo "/*" >> $HTML_REPORT
390
		#display value of Y axis, only useful for chart bar
2688 lucas.echa 391
		elif [ "$(echo $LINE_JS | grep 'XXCOMMENT-ENDXX' | wc -l)" -eq 1 ]
2009 raphael.pi 392
		then
393
			echo "*/" >> $HTML_REPORT
394
		else
395
			echo $LINE_JS >> $HTML_REPORT
396
		fi
397
	done
398
	echo "</script>" >> $HTML_REPORT
399
else
3252 rexy 400
	echo "<h3>Aucune activité de la Blacklist cette semaine.</h3>" >> $HTML_REPORT
2009 raphael.pi 401
fi
402
 
403
######################VIRUS THREAT######################
404
echo "Create AV logs since the installation of ALCASAR"
405
 
406
#decompress every logs, if they exist
2841 rexy 407
if [ "$(ls -1 /var/log/clamav/clamd.log.*.gz 2>/dev/null | wc -l)" -ge 1 ]
2009 raphael.pi 408
then
2841 rexy 409
	gunzip -d clamd.log.*.gz
2009 raphael.pi 410
fi
2841 rexy 411
for FILE in /var/log/clamav/clamd.log*
2009 raphael.pi 412
do
413
	while read LINE_AV
414
	do
2841 rexy 415
		if [ "`echo $LINE_AV|grep -c FOUND`" == 1 ]
416
		then
417
			Y=$(echo $LINE_AV | cut -d' ' -f5)
418
			M=$(echo $LINE_AV | cut -d' ' -f2)
419
			D=$(echo $LINE_AV | cut -d' ' -f3)
420
			H=$(echo $LINE_AV | cut -d' ' -f4)
421
			CURRENT_TS=$(date -d "$M $D $Y $H" +"%s")
422
			echo $CURRENT_TS >> $TMP_AV
423
		fi
2688 lucas.echa 424
	done < $FILE
2009 raphael.pi 425
done
426
if [ -e $TMP_AV ]
427
then
428
	ENABLE_AV=1
429
	DATE_END=$(cat $TMP_AV | sort -n | head -1)
430
	for TS in $(seq $C_TS -$STEP_TS $DATE_END)
431
	do
432
		DATE_1=$TS
433
		DATE_2=$((TS-$STEP_TS))
2189 tom.houday 434
		COUNT_AV=0
2009 raphael.pi 435
 
436
		for TS_FILE in $(cat $TMP_AV)
437
		do
2688 lucas.echa 438
			if [ "$TS_FILE" -le "$DATE_1" ] && [ "$TS_FILE" -ge "$DATE_2" ]
2189 tom.houday 439
			then
2009 raphael.pi 440
				COUNT_AV=$((COUNT_AV+1))
441
			fi
442
		done
2189 tom.houday 443
 
2009 raphael.pi 444
		VALUE_AV_LABEL="'$(date -d @$DATE_2 "+%Y-%m-%d" )', $VALUE_AV_LABEL"
445
		VALUE_AV_DATA="$COUNT_AV, $VALUE_AV_DATA"
446
	done
2688 lucas.echa 447
 
2009 raphael.pi 448
	#create Antivirus section in html document
449
	NAME_AV='chart_av'
450
	CONF_AV='config_av'
451
	echo "<center>" >> $HTML_REPORT
452
	echo "<canvas id='$NAME_AV' width='450' height='450' ></canvas>" >> $HTML_REPORT
453
	echo "</center>" >> $HTML_REPORT
454
 
455
	#create chart bar in html file with javascript (chartjs.com)
456
	echo "<script>" >> $HTML_REPORT
457
	cat $MODEL_CHARTJS | while read LINE_JS
458
	do
459
		#name of variable
2688 lucas.echa 460
		if [ "$(echo $LINE_JS | grep 'XXCONFXX' | wc -l)" -eq 1 ]
2009 raphael.pi 461
		then
462
			echo ${LINE_JS/XXCONFXX/$CONF_AV} >> $HTML_REPORT
463
		#chart type
2688 lucas.echa 464
		elif [ "$(echo $LINE_JS | grep 'XXTYPEXX' | wc -l)" -eq 1 ]
2009 raphael.pi 465
		then
466
			echo ${LINE_JS/XXTYPEXX/bar} >> $HTML_REPORT
467
		#graph title
2688 lucas.echa 468
		elif [ "$(echo $LINE_JS | grep 'XXTITLEXX' | wc -l)" -eq 1 ]
2009 raphael.pi 469
		then
2189 tom.houday 470
			echo ${LINE_JS/XXTITLEXX/"Menaces bloqués par l\'antivirus"} >> $HTML_REPORT
2009 raphael.pi 471
		#chart data
2688 lucas.echa 472
		elif [ "$(echo $LINE_JS | grep 'XXDATAXX' | wc -l)" -eq 1 ]
2009 raphael.pi 473
		then
474
			echo ${LINE_JS/XXDATAXX/$VALUE_AV_DATA} >> $HTML_REPORT
475
		#color
2688 lucas.echa 476
		elif [ "$(echo $LINE_JS | grep 'XXCOLORXX' | wc -l)" -eq 1 ]
2009 raphael.pi 477
		then
478
			echo ${LINE_JS/XXCOLORXX/$COLOR} >> $HTML_REPORT
479
		#labels
2688 lucas.echa 480
		elif [ "$(echo $LINE_JS | grep 'XXLABELSXX' | wc -l)" -eq 1 ]
2009 raphael.pi 481
		then
482
			echo ${LINE_JS/XXLABELSXX/$VALUE_AV_LABEL} >> $HTML_REPORT
2688 lucas.echa 483
		elif [ "$(echo $LINE_JS | grep 'XXLEGENDXX' | wc -l)" -eq 1 ]
2009 raphael.pi 484
		then
485
			echo ${LINE_JS/XXLEGENDXX/false} >> $HTML_REPORT
486
		#display value of Y axis, only useful for chart bar
2688 lucas.echa 487
		elif [ "$(echo $LINE_JS | grep 'XXCOMMENT-BEGINXX' | wc -l)" -eq 1 ]
2009 raphael.pi 488
		then
489
			echo "" >> $HTML_REPORT
490
		#display value of Y axis, only useful for chart bar
2688 lucas.echa 491
		elif [ "$(echo $LINE_JS | grep 'XXCOMMENT-ENDXX' | wc -l)" -eq 1 ]
2009 raphael.pi 492
		then
493
			echo "" >> $HTML_REPORT
2688 lucas.echa 494
		elif [ "$(echo $LINE_JS | grep 'XXYLABELXX' | wc -l)" -eq 1 ]
2009 raphael.pi 495
		then
2013 raphael.pi 496
			echo "\"Nombre de menaces virales bloqués par l'antivirus\"" >> $HTML_REPORT
2009 raphael.pi 497
		else
498
			echo $LINE_JS >> $HTML_REPORT
499
		fi
500
	done
501
	echo "</script>" >> $HTML_REPORT
502
else
3252 rexy 503
	echo "<h3>Aucune menace virale.</h3>" >> $HTML_REPORT
2009 raphael.pi 504
fi
505
 
506
 
507
######################ALCASAR : DAILY USE######################
508
echo "Get daily use connection of the week"
509
 
3252 rexy 510
echo "<h3>Statistiques volumétrie connexions</h3>" >> $HTML_REPORT
511
 
2009 raphael.pi 512
#create new htdigest user to consult statistique of ACC
513
#if user does not exist, we create him
2688 lucas.echa 514
if [ "$(grep "$tmp_account:" $DIR_KEY/key_only_manager | wc -l)" -lt 1 ]
2009 raphael.pi 515
then
2189 tom.houday 516
	(echo -n "$tmp_account:$realm:" && echo -n "$tmp_account:$realm:$password" | md5sum | awk '{print $1}' ) >> $DIR_KEY/key_only_manager
517
	(echo -n "$tmp_account:$realm:" && echo -n "$tmp_account:$realm:$password" | md5sum | awk '{print $1}' ) >> $DIR_KEY/key_manager
518
	(echo -n "$tmp_account:$realm:" && echo -n "$tmp_account:$realm:$password" | md5sum | awk '{print $1}' ) >> $DIR_KEY/key_all
519
	chown -R root:apache $DIR_KEY
520
	chmod 640 $DIR_KEY/key_*
2009 raphael.pi 521
fi
522
 
523
#get stats.php from ACC
2138 richard 524
wget -q -nv --user $tmp_account --password $password https://alcasar/acc/manager/htdocs/stats.php -O $TMP_STATS --no-check-certificate
2009 raphael.pi 525
 
526
#clean this file to include it in html report.
527
DELIM_1="<td colspan=10 height=20><img src=\"images\/pixel.gif\"><\/td>"
528
DELIM_2="<\/td><\/tr> <\/table> <\/td><\/tr> <\/table> <\/td><\/tr> <\/table> <p>"
529
cat $TMP_STATS | sed -n "/$DELIM_1/,/$DELIM_2/p" | tail -n+3 | head -n-2 >> $TMP_STATS_2
530
cat $TMP_STATS_2 | sed -e 's:images/pixel.gif:../../manager/htdocs/images/pixel.gif:g' >> $HTML_REPORT
531
 
532
#we delete our user if he still exists
2688 lucas.echa 533
if [ "$(grep "$tmp_account:" $DIR_KEY/key_only_manager | wc -l)" -ge 1 ]
2009 raphael.pi 534
then
2189 tom.houday 535
	$SED "/^$tmp_account:/d" $DIR_KEY/key_only_manager
536
	$SED "/^$tmp_account:/d" $DIR_KEY/key_manager
537
	$SED "/^$tmp_account:/d" $DIR_KEY/key_all
2009 raphael.pi 538
fi
539
 
2190 tom.houday 540
 
541
###################### ALCASAR : LOG ACCESS ######################
542
echo "Get ACC log access of the week"
543
 
2209 tom.houday 544
ROWS=""
545
while read -r access ; do
2267 richard 546
	access_datas=(${access//|/ })
2209 tom.houday 547
	accces_date_intl=$(echo "${access_datas[0]} ${access_datas[1]}" | sed -E 's@^([0-9]{2})+/+([0-9]{2})+/+([0-9]{4})+@\3-\2-\1@') # Convert date format DD/MM/YYYY to YYYY-MM-DD
548
	access_date=$(date -d "$accces_date_intl" +%s)
549
	access_user=${access_datas[2]}
550
	access_ip=${access_datas[3]}
2286 tom.houday 551
	access_agent=$(echo "$access" | cut -d'|' -f4)
2209 tom.houday 552
	if [ $access_date -lt $SECS_AGO ]; then
553
		break
554
	fi
555
	access_date_formatted=$(date -d @$access_date +"%x %X")
2267 richard 556
	ROWS="$ROWS<tr><td>$access_date_formatted</td><td>$access_user</td><td>$access_ip</td><td>$access_agent</td></tr>"
557
done < <(cat /var/Save/security/acc_access.log | sort -r)
2209 tom.houday 558
# TODO: Read archives if necessary
559
if [ -z "$ROWS" ]; then
2267 richard 560
	ROWS="<tr><td colspan=\"4\" style=\"text-align: center;\">Aucune connexion</td></tr>"
2190 tom.houday 561
fi
562
 
2209 tom.houday 563
# Create HTML document
3252 rexy 564
echo "<h3>Connexion à l'ALCASAR Control Center (ACC)</h3>" >> $HTML_REPORT
2209 tom.houday 565
echo "<table class=\"table table-striped\">" >> $HTML_REPORT
2267 richard 566
echo "<thead><tr><th>Date</th><th>Utilisateur</th><th>Adresse IP</th><th>Agent</th></tr></thead><tbody>" >> $HTML_REPORT
2688 lucas.echa 567
echo "$ROWS" >> $HTML_REPORT
2209 tom.houday 568
echo "</tbody></table>" >> $HTML_REPORT
2190 tom.houday 569
 
2209 tom.houday 570
 
2190 tom.houday 571
###################### ALCASAR : GLOBAL TRAFFIC ######################
572
echo "Get Global traffic of the last 30 days"
573
 
574
ROWS=""
2474 tom.houday 575
EXTIF=$(grep ^EXTIF= $CONF_FILE | cut -d'=' -f2)
2190 tom.houday 576
 
3249 rexy 577
while read -r entry; do
578
    year=$(echo "$entry" | sed -n 's/.*"year": \([0-9]*\).*/\1/p')
579
    month=$(echo "$entry" | sed -n 's/.*"month": \([0-9]*\).*/\1/p')
580
    day=$(echo "$entry" | sed -n 's/.*"day": \([0-9]*\).*/\1/p')
581
    rx=$(echo "$entry" | sed -n 's/.*"rx": \([0-9]*\).*/\1/p')
582
    tx=$(echo "$entry" | sed -n 's/.*"tx": \([0-9]*\).*/\1/p')
583
    if [ -n "$year" ]; then
584
        date_year=$year
585
    fi
586
    if [ -n "$month" ]; then
587
        if [ $month -le 9 ]; then
588
            date_month="0$month"
589
        else
590
            date_month=$month
591
        fi
592
    fi
593
    if [ -n "$day" ]; then
594
        if [ $day -le 9 ]; then
595
            date_day="0$day"
596
        else
597
            date_day=$day
598
        fi
599
    fi
600
    if [ -n "$rx" ]; then
601
        day_rx=$rx
602
    fi
603
    if [ -n "$tx" ]; then
604
        day_tx=$tx
605
    fi
606
    if [ -n "$date_year" ] && [ -n "$date_month" ] && [ -n "$date_day" ] && [ -n "$day_rx" ] && [ -n "$day_tx" ]; then
607
        day_date="$date_day$date_month$date_year"
608
        day_total=$((day_rx * day_tx))
609
        ROWS="$ROWS<tr><td>$day_date</td><td>$day_rx</td><td>$day_tx</td><td>$day_total</td></tr>"
610
        date_month=""
611
        date_day=""
612
        date_year=""
613
    fi
614
done < <(vnstat -i $EXIT_IF --json d -b $(date --date="7 days ago" "+%Y-%m-%d") | python -m json.tool | sed -n 's/.*"day": \([0-9]*\).*/\1/p')
2209 tom.houday 615
if [ -z "$ROWS" ]; then
616
	ROWS="<tr><td colspan=\"4\" style=\"text-align: center;\">Aucun jour capturé</td></tr>"
617
fi
618
 
2190 tom.houday 619
# Create html document
3252 rexy 620
echo "<h3>Trafic global</h3>" >> $HTML_REPORT
2190 tom.houday 621
echo "<table class=\"table table-striped\">" >> $HTML_REPORT
622
echo "<thead><tr><th>Date</th><th>Entrant</th><th>Sortant</th><th>Total</th></tr></thead><tbody>" >> $HTML_REPORT
2688 lucas.echa 623
echo "$ROWS" >> $HTML_REPORT
2190 tom.houday 624
echo "</tbody></table>" >> $HTML_REPORT
625
 
626
 
627
###################### ALCASAR : FAIL2BAN ######################
628
echo "Get fail2ban log of the week"
629
 
2209 tom.houday 630
ROWS=""
631
dateDaysAgo_formatted=$(date --date="$MAX_DAY_AGO days ago" +'%Y-%m-%d %H:%M:%S,%N' | rev | cut -c 7- | rev)
632
while read -r log ; do
2190 tom.houday 633
	log_datas=($log)
634
	log_date="${log_datas[0]} ${log_datas[1]}"
635
	log_type=${log_datas[4]:1:-1}
636
	log_ip=${log_datas[6]}
2209 tom.houday 637
	log_date_formatted=$(date -d "$log_date" +"%x %X")
2190 tom.houday 638
 
2209 tom.houday 639
	ROWS="$ROWS<tr><td>$log_date_formatted</td><td>$log_ip</td><td>$log_type</td></tr>"
640
done < <(grep " Ban " /var/log/fail2ban.log | sort -r | awk -v dateDaysAgo="$dateDaysAgo_formatted" '($1 " " $2) >= dateDaysAgo')
2190 tom.houday 641
 
2209 tom.houday 642
if [ -z "$ROWS" ]; then
643
	ROWS="<tr><td colspan=\"3\" style=\"text-align: center;\">Aucune adresse IP bloquée</td></tr>"
644
fi
645
 
3252 rexy 646
#Create html document
647
echo "<h3>Adresse(s) IP bloquée(s) (Fail2Ban)</h3>" >> $HTML_REPORT
2209 tom.houday 648
echo "<table class=\"table table-striped\">" >> $HTML_REPORT
649
echo "<thead><tr><th>Date</th><th>Adresse IP</th><th>Règle</th></tr></thead><tbody>" >> $HTML_REPORT
650
echo $ROWS >> $HTML_REPORT
2190 tom.houday 651
echo "</tbody></table>" >> $HTML_REPORT
652
 
653
 
3252 rexy 654
######################HTML END######################
2009 raphael.pi 655
 
656
#Execute our javascript function to print charts
657
echo "<script>window.onload = function() {" >> $HTML_REPORT
3252 rexy 658
#BL since installation
2009 raphael.pi 659
if [ $ENABLE_BL -eq "1" ]
660
then
661
	echo "var ctx_$NAME_BL_INSTALLATION = document.getElementById('$NAME_BL_INSTALLATION').getContext('2d');" >> $HTML_REPORT
662
	echo "var $NAME_BL_INSTALLATION = new Chart(ctx_$NAME_BL_INSTALLATION, $CONF_BL_INSTALLATION);" >> $HTML_REPORT
663
fi
3252 rexy 664
#BL week
2009 raphael.pi 665
if [ $ENABLE_BL_WEEK -eq "1" ]
666
then
667
	echo "var ctx_$NAME_BL = document.getElementById('$NAME_BL').getContext('2d');" >> $HTML_REPORT
668
	echo "var $NAME_BL = new Chart(ctx_$NAME_BL, $CONF_BL);" >> $HTML_REPORT
669
fi
3252 rexy 670
#malware Threat
2009 raphael.pi 671
if [ $ENABLE_AV -eq "1" ]
672
then
673
	echo "var ctx_$NAME_AV = document.getElementById('$NAME_AV').getContext('2d');" >> $HTML_REPORT
674
	echo "var $NAME_AV = new Chart(ctx_$NAME_AV, $CONF_AV);" >> $HTML_REPORT
675
fi
676
echo "};</script>" >> $HTML_REPORT
677
echo "</body>" >> $HTML_REPORT
678
echo "</html>" >> $HTML_REPORT
679
 
680
#convert html document to PDF
2688 lucas.echa 681
/usr/bin/wkhtmltopdf $HTML_REPORT "$(echo $HTML_REPORT | cut -d'.' -f1).pdf"
682
chown apache:apache "$(echo $HTML_REPORT | cut -d'.' -f1).pdf"
683
chmod 644 "$(echo $HTML_REPORT | cut -d'.' -f1).pdf"
684
mv "$(echo $HTML_REPORT | cut -d'.' -f1).pdf" /var/Save/activity_report/
2009 raphael.pi 685
 
2013 raphael.pi 686
#compress every logs, if they exist
2841 rexy 687
if [ "$(ls -1 /var/log/clamav/clamd.log.* 2>/dev/null | wc -l)" -ge 1 ]
2013 raphael.pi 688
then
2841 rexy 689
	gzip /var/log/clamav/clamd.log.*
2013 raphael.pi 690
fi
691
 
692
#compress every logs
2688 lucas.echa 693
if [ "$(ls -1 /var/log/unbound/unbound-blacklist.log.* 2>/dev/null | wc -l)" -ge 1 ]
2013 raphael.pi 694
then
2688 lucas.echa 695
	gzip /var/log/unbound/unbound-blacklist.log.*
2013 raphael.pi 696
fi
697
 
698
#remove our files
2141 richard 699
rm -f $TMP_BL
700
rm -f $TMP_BL_WEEK
701
rm -f $TMP_BL_WEEK_CAT
702
rm -f $TMP_STATS
703
rm -f $TMP_STATS_2
704
rm -f $HTML_REPORT