I have a little log-file, where i should find the bots.
File:
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user logged in| -
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user changed password| -
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user logged off| -
Mon, 22 Aug 2016 13:15:42 +0200|178.57.66.225|faaaaaa11111| - |user logged in| -
Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user logged in| -
Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user changed password| -
Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user logged off| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged in| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged in| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user changed password| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged off| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user logged in| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user changed password| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user changed profile| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user logged off| -
Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciulmla| - |user logged in| -
Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciulmla| - |user changed password| -
Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciulmla| - |user logged off| -
Mon, 22 Aug 2016 13:20:42 +0200|178.57.67.225|faaaa0a1111| - |user logged in| -
In this file, i have a lot ip, logins, usernames. first of all, i thought about uniq and count of ip. So, what i should do. I should use events only: user logged in, user changed off, user logged off. Next, i should show the users, that logged in the same time. First three lines, user has logged in, changed password, logged off. This time is 13:15:39. And user fxsciaqulmlk from ip 178.57.66.225 is a bot, because operation of event has made at the same time. My script:
log_file=/root/log
log_after=/root/after_log
temp_file=/root/temp
temp_file2=/root/temp2
uniq_file=/root/uniq
uniq_file2=/root/uniq2
result_uniq=/root/result_uniq
result_file=/root/result
cat /dev/null > $log_after
cat /dev/null > $temp_file
cat /dev/null > $temp_file2
cat /dev/null > $uniq_file
cat /dev/null > $uniq_file2
cat /dev/null > $result_file
grep "changed password\|logged in\|logged off" $log_file > $log_after
cat $log_after | awk '{print $6}' | awk -F "|" '{print $2,$3}' | tail -n 20 > $temp_file
cat $log_after | awk '{print $5}' | tail -n 20 > $temp_file2
uniq -c $temp_file | awk '{print $1}' > $uniq_file
uniq -c $temp_file2 | awk '{print $1}' > $uniq_file2
awk 'FNR==NR{a[$1]++;next}!a[$1]' $uniq_file $uniq_file2 > $result_uniq
if [ -s $result_uniq ] && [ -f $result_uniq ]; then
echo "File is not empty"
echo "Differences:"
cat $result_uniq
echo "Need to think"
exit 0
else
echo "File is empty"
echo "We can use one file from uniq"
fi
for i in `uniq -c $temp_file | awk '{print $1}'`; do
if [ $i -gt 2 ]; then
s=`uniq -c $temp_file | awk '$1 == '$i | awk '{print $3}'`
ss=`uniq -c $temp_file | awk '$1 == '$i | awk '{print $2}'`
echo "Tho boot is user $s with ip $ss"
fi
done
This question is an exact duplicate of:
Script to search bots from log file 1 answer
I have written this message before. So, i have done my task, but i have a little not correct output. So... I will start. I have a log file:
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user logged in| - Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user changed password| - Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user logged off| - Mon, 22 Aug 2016 13:15:42 +0200|178.57.66.225|faaaaaa11111| - |user logged in| - Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user logged in| - Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user changed password| - Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user logged off| - Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged in| - Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged in| - Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user changed password| - Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged off| - Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user logged in| - Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user changed password| - Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user changed profile| - Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user logged off| - Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciulmla| - |user logged in| - Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciulmla| - |user changed password| - Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciulmla| - |user logged off| - Mon, 22 Aug 2016 13:20:42 +0200|178.57.67.225|faaaa0a1111| - |user logged in| -
So, what i should do. I should use events only: user logged in, user changed off, user logged off. Next, i should show the users, that logged in the same time. First three lines, user has logged in, changed password, logged off. This time is 13:15:39. And user fxsciaqulmlk from ip 178.57.66.225 is a bot, because operation of event has made at the same time. My script:
#!/bin/bash
# you should add it script in crontab, like this
#*/2 * * * * /name_of_this_script.sh
# you should change variable way in $log_file to your own way
log_file=/root/log
log_after=/root/after_log
temp_file=/root/temp
temp_file2=/root/temp2
uniq_file=/root/uniq
uniq_file2=/root/uniq2
result_uniq=/root/result_uniq
result_file=/root/result
cat /dev/null > $log_after
cat /dev/null > $temp_file
cat /dev/null > $temp_file2
cat /dev/null > $uniq_file
cat /dev/null > $uniq_file2
cat /dev/null > $result_file
grep "changed password\|logged in\|logged off" $log_file > $log_after
cat $log_after | awk '{print $6}' | awk -F "|" '{print $2,$3}' | tail -n 20 > $temp_file
cat $log_after | awk '{print $5}' | tail -n 20 > $temp_file2
uniq -c $temp_file | awk '{print $1}' > $uniq_file
uniq -c $temp_file2 | awk '{print $1}' > $uniq_file2
awk 'FNR==NR{a[$1]++;next}!a[$1]' $uniq_file $uniq_file2 > $result_uniq
if [ -s $result_uniq ] && [ -f $result_uniq ]; then
echo "File is not empty"
echo "Differences:"
cat $result_uniq
echo "Need to think"
exit 0
else
echo "File is empty"
echo "We can use one file from uniq"
fi
for i in `uniq -c $temp_file | awk '{print $1}'`; do
if [ $i -gt 2 ]; then
s=`uniq -c $temp_file | awk '$1 == '$i | awk '{print $3}'`
ss=`uniq -c $temp_file | awk '$1 == '$i | awk '{print $2}'`
echo "Tho boot is user $s with ip $ss"
fi
done
Everything ok. But i have bad result:
Tho boot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
Tho boot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
Tho boot is user erdsfsdfsdf with ip 178.57.66.205
Tho boot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
Tho boot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
Where i have a mistake ? I can't understand, where i have a mistake in code ? I am so sorry for repeating question, but i have done this script, i need a little help.