-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyze.sh
More file actions
executable file
·92 lines (80 loc) · 1.94 KB
/
analyze.sh
File metadata and controls
executable file
·92 lines (80 loc) · 1.94 KB
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
#!/bin/bash
#
# Author:
# Felipe Mariani Lopes <bolzin [at] gmail [dot] com>
#
# Program:
# analize.sh
#
# Usage:
# sudo ./analize.sh [--invalid [-i|-u]| --root]
#
# Description:
# This program examines /var/log/auth.log files looking for
# suspicious attempts to log into server. It examines root
# attempts, invalid users and output the source ips.
# ------------------------------------------------------------------------------
# Settings - You can change the settings according to your configuration
CURRENT="/var/log/auth.log /var/log/auth.log.1"
COMPACTED="/var/log/auth.log.[[:digit:]].gz"
# To show only the last attempts you can use this line
#AUTHLOG="$CURRENT"
AUTHLOG="$CURRENT $COMPACTED"
# Temporary files used during execution
TEMP=$(tempfile)
RESULTTEMP=$(tempfile)
function attempts()
{
# used to decide between cat and zcat
extension="${1##*.}"
case "$2" in
# list all ips that tried to log in as root
--root)
if [ "$extension" = 'gz' ]
then
zcat $1 |\
tr -s " " |\
grep "Failed password for root" |\
cut -d" " -f11 >> $RESULTTEMP
else
cat $1 |\
tr -s " " |\
grep "Failed password for root" |\
cut -d" " -f11 >> $RESULTTEMP
fi
;;
# list invalid users or invalid users' ip
--invalid)
if [ "$extension" = 'gz' ]
then
zcat $1 |\
tr -s " " |\
grep "Failed password for invalid user" |\
cut -d" " -f11,13 > "${TEMP}"
else
cat $1 |\
tr -s " " |\
grep "Failed password for invalid user" |\
cut -d" " -f11,13 > "${TEMP}"
fi
case "$3" in
-u)
cat $TEMP | cut -d" " -f1 >> "${RESULTTEMP}"
;;
-i)
cat $TEMP | cut -d" " -f2 >> "${RESULTTEMP}"
;;
esac
;;
esac
}
# Main
for file in $AUTHLOG;
do
attempts $file $1 $2
done
# I'm using sort twice here because it is not sorting correctly when called once
cat $RESULTTEMP | sort | uniq -c | tr -s " " | cut -c2- | sort -n -k1 -r
# Removing temporary files
rm $TEMP
rm $RESULTTEMP