Here is a clever script concept that helps company managers notifying someone's unusual amount of file reading. That's typical behaviour for an employee who is intended to quit and try to steal all the files of that company. Such auditing softwares are on the market for several hundred or thousand bucks!
Luckily for you, I've written one in bash. OK that's not good news for ones who use only Windows. But it can be easily portable to any script language, for example, php so that it could be run directly in the Windows fileserver or DC by installing the proper runtime enviroment. (PHP, ruby, python, etc.)
Exploring that thought further, now I'm going to translate that for myself. ;) But for now, it's enough to get it work in bash.
The original idea is that we suppose that all the users open almost the same amount of files daily on their daily routines. This script always alerts when a statistical threshold percent reached per user.
In the following example you are going to see a nice solution for lab use in which I transfer the logfile from the Windows server to a Linux server to be able to run the bash script on it. You can find detailed comments inside the script.
Step-by-step installation:
1: Enable audit log policy on your Windows Server, assign it to the target folders and test it
(Note: in the above blog you can find an advanced example. In my case I look for event id 4663 because it just contains the information I need.) Set the audit rules according to your needs. The less eventrule the better. We need to trace file reads so the first rule is a must.
2: You need to export the specific events from the security log to a plain file. So create a getsec.ps1 file in c:\script\ with the following content:
Get-EventLog security -After (Get-Date).AddDays(-1) -InstanceId 4663 |select Message|ft -AutoSize -Wrap > c:\auditing\report.txt
3: Also, don't forget to create that c:\auditing folder and then put an empty file into it named: mounted
4: Schedule the script to run at the end of the working hours or at midnight. The command is to be: (e.g.) C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and the argument (e.g.): -executionpolicy bypass -file c:\scripts\getsec.ps1 2>&1 > C:\scripts\log.txt
5: Share c:\auditing folder with a dedicated user that is intended to be used only by the Linux server, e.g.: linuxsrv
6: On your linux box, install the following packages: cifs-utils dos2unix mutt iconv
7: Test your connection:
[ -f /mnt/mounted ] || mount.cifs //192.168.xx.xx/auditing/ /mnt/ -o username=linuxsrv,password=Sup3rS3cur3P4$$,domain=contoso
8: Create the base directories in, e.g.
mkdir /root/auditor && cd /root/auditor
mkdir archive average stat users; echo "0" > counter
Having succeeded, congratulations, now you are ready to track your file access activity and watch out for possible data stealing FOR FREE!
Here is the mighty script. See comments inline!
A következő címkéjű bejegyzések mutatása: shell. Összes bejegyzés megjelenítése
A következő címkéjű bejegyzések mutatása: shell. Összes bejegyzés megjelenítése
2016. június 27., hétfő
2014. november 25., kedd
Little dear ones of mine
How to take actions on a directory that contains hundreds of subdirectories, named like this...
...but just on the first some hundreds of them so their proper naming could be an issue. Solution:
It could be more solid but you know, always Keep It Simple&Stupid. :-)
Here is a more complex one. It's a cron driven script that checks your openvpn logfile and email you if an event (e.g. if certain user connects) found. It remembers its last run so that a logline never get processed twice. Also handles logrotate events.
0001
0002
....
0100
0101
...
0999
1000
...
...but just on the first some hundreds of them so their proper naming could be an issue. Solution:
#!/bin/bash
for i in $(seq 500);do
lngt=`expr length $i`
case $lngt in
1)
i=000$i
;;
2)
i=00$i
;;
3)
i=0$i
;;
esac
ls /home/samba/archive/$i -LRs >> /root/content.txt
done
It could be more solid but you know, always Keep It Simple&Stupid. :-)
Here is a more complex one. It's a cron driven script that checks your openvpn logfile and email you if an event (e.g. if certain user connects) found. It remembers its last run so that a logline never get processed twice. Also handles logrotate events.
#!/bin/bash
cd /var/log/openvpn
[ -e temp ] && rm temp
echo "" > connectionz
NOW=`cat openvpn.log|wc -l`
LAST=`cat last`
CHECK=$(($NOW-$LAST))
if [[ $CHECK -ge 0 ]]; then # change found
echo $NOW > last # if 0 then doesn't matter but no harmful
else
echo 0 > last # logrotation happened, nulling last
LAST=0
fi
tail -$CHECK openvpn.log|grep Initiated > connectionz
while read line
do
USER=`echo $line|cut -d '[' -f2|cut -d ']' -f1`
DATUM=`echo $line|cut -d ' ' -f2-5`
if [ $USER = "JohnSmith" ] || [ $USER = "PeteSmith" ] || [ $USER = "JaneSmith" ] ; then
echo "A user connected:"$USER" event time:"$DATUM >> temp
echo "" >> temp
fi
done < connetionz
[ -e temp ] && cat temp | mail -n -s "OPENVPN CONNETION initiated" myemail@mydomain.com,yourdomain@yourdomain.com
2013. március 5., kedd
my little handy backup from php
<?php
if ($_POST){
$nap=stripslashes($_POST['nap']);
exec ("find /var/www/ize -not -name \"backup_*\" -a -not -name \"backup.php\" -a -not -name \"*.zip\" -a -not -name \"error.log\" -a -not -path \"*/zend*\" -a -not -path \"*tmp/*\" -a -not -path \"*images/*\" -a -not -path \"*fotok*\" -type f -mtime -$nap > lista");
$fajl = "backup_".`date +%F_%H%M`.".tar.gz";
$output = `tar -cvzf /var/www/ize/backup_\`date +%F_%H%M\`.tar.gz --files-from lista`;
echo "DONE!";
echo "<a href=\"$fajl\">backup</a>";
echo "<pre>$output</pre>";
} else {
?>
<form action="backup.php" method="post">
file age limit to be backuped ?
<input type="text" name="nap" id="nap" cols="2" rows="1">
<button class="button" type="submit">GO</button>
</form>
<?php }; ?>
Feliratkozás:
Bejegyzések (Atom)
