LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2010, 08:16 PM   #16
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31

yes, I wrote 2 (one restarts proftp), sadly my ftp server has just this second stopped logging IP's though, talk about bad timing! I'll figure it out... Anyhoo

Here's the blacklisting file, this is set as a cronjob and runs 5 minutes before my logs rotate

Code:
TOPHITS=`awk -F"[][]" 'BEGIN {MAXALLOWED=50}
($0 ~ /FTP session closed/) {
if (a[$4] > MAXALLOWED) {next;}
if ($4 != OLD) {a[$4] = 1;OLD = $4;}
else {a[$4]++;}
}
END {for (i in a) {if (a[i] > MAXALLOWED){print i;}}
}' /var/log/messages`
if [[ -n $TOPHITS ]]; then
   for IP in $TOPHITS;
    do
      sed -i "78s/$/,$IP/" /opt/etc/proftpd.conf
    done
   /root/ftp restart
else
   echo "No IP's meeting that threshold"
fi
I know assigning the variable like that looks a little sloppy but it seems to work ok.

Here's my starter script:

Code:
runningpid=`ps -ef | grep proftp | grep -v grep | awk '{print $2}'`

case $1 in

start)
if [ -n "$runningpid" ]; then
   echo "Process running under pid: $runningpid"
else
   /opt/sbin/proftpd --config /opt/etc/proftpd.conf
fi
;;

stop)
if [ -n "$runningpid" ]; then
   echo "killing pid $runningpid"
   kill $runningpid
else
   echo "Process already stopped"
fi
;;

status)
if [ -n "$runningpid" ]; then
   echo "Process running under pid: $runningpid"
else
   echo "ProFTP is not running on the system"
fi
;;

restart)
if [ -n "$runningpid" ]; then
   echo "killing $runningpid"
   kill $runningpid
   echo "restarting process"
   /opt/sbin/proftpd --config /opt/etc/proftpd.conf
   echo "new pid is `ps -ef | grep proftp | grep -v grep | awk '{print $2}'`"
else
   echo "Starting process"
   /opt/sbin/proftpd --config /opt/etc/proftpd.conf
   echo "new pid is: $runningpid"
fi
;;

*)
echo "Syntax ftp start|stop|status|restart"

esac
It's basically a more advanced init script than the one that was provided with proftpd, I wrote it in like 15 minutes so althoguh I'm open to critism, you must realise this was someting wrote quickly (usually I'd have functions for start and stop for example and merge them into the restart and stop/start case statements).
 
Old 10-27-2010, 05:21 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
TOPHITS=$(awk -F"[][]" -vmax=50 '/FTP session closed/{a[$4]++}END{for(i in a)if(a[i] > max)printf(",%s",i)}' /var/log/messages)

if [[ $TOPHITS ]]
then
    sed -i "78s/$/$TOPHITS/" /opt/etc/proftpd.conf
    /root/ftp restart
else
    echo "No IP's meeting that threshold"
fi
Small improvement on startup script:
Code:
get_pid()
{
    ps -ef | awk '/proftp/ && !/awk/{printf("%s ", $2)}'
}

runningpid=$(get_pid)

# and in case do
restart)
if [[ $runningpid ]]; then
   echo "killing $runningpid"
   kill $runningpid
   echo "restarting process"
   /opt/sbin/proftpd --config /opt/etc/proftpd.conf
   echo "new pid is $(get_pid)"
else
   echo "Starting process"
   /opt/sbin/proftpd --config /opt/etc/proftpd.conf
   echo "new pid is: $runningpid"
fi
;;
 
1 members found this post helpful.
Old 10-27-2010, 06:32 AM   #18
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
@grail:
Code:
for(i in a)if(a[i] > max)printf(",%s",i)
That is what I was looking for to curb the need of the for-loop. I tried
Code:
ORS=",";for(i in a)if(a[i] > max)print i)
But this meant a slight change on the format from a preceding to a trailing comma.

However, your script does not provide the same functionality as the script in the initial post. Your script counts the total number of hits by an IP.
It corresponds to
Code:
awk -F"[][]" '/closed/{print $4;}' file.2 | sort | uniq -c
Since the orig. script 'sort's after the input has been 'uniq'ed
Code:
awk -F"[][]" '/closed/{print $4;}' file.2 | uniq -c | sort -nr
I think that the original intention was to count the number of consecutive hits.
Compare the different output with this sample data:
Code:
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session opened.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session opened.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.73]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session opened.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.167[60.217.226.167]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.167[60.217.226.167]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.167[60.217.226.167]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.167[60.217.226.167]) - FTP session closed.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.166[60.217.226.166]) - FTP session opened.
Oct 26 16:07:37 HDD2 proftpd[14137]: HDD2 (60.217.226.167[60.217.226.167]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session closed.
Oct 26 08:44:32 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session opened.
Oct 26 08:44:35 HDD2 proftpd[22081]: HDD2 (110.9.129.72[110.9.129.72]) - FTP session closed.
With MAXHITS=4 the IP 110.9.129.72 does _not_ belong into the blacklist.
 
1 members found this post helpful.
Old 10-27-2010, 08:17 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Thanks crts ... I missed the consecutive part
 
1 members found this post helpful.
Old 10-27-2010, 09:26 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is another slight variation to include not consecutive (just for fun):
Code:
BEGIN{
    FS="[][]"
    MAXALLOWED=50
}

/FTP session closed/{

    if ($4 != OLD && a[OLD] <= MAXALLOWED)
        delete a[OLD]

    a[$4]++
    OLD = $4
}

END{
    for (i in a)
        if(a[i] > MAXALLOWED)
            print i
 
1 members found this post helpful.
Old 10-27-2010, 09:46 AM   #21
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Thanks for both suggestions I'll have a play with everything on this page and get optimum functionality. Also grail, thanks for the improved init script - adding the getpid as a function works 'properly' when listing the new pid ~(obviously before I needed to exit out of the ifstatement to get a DIFFERENT pid, and thus had to specifiy the pid by hand, adding it as a function works properly).

Can either of you clarify what you mean by consecutive hits and total number of hits? The idea was to list the IP which had hits greater than 50, the script crts provided does that successfully, but I don't really see the difference between consecutive and the total? Would this effect functionality?

My understanding of consecutive hit is one hit after another after another, and my idea of total is the ammount of times an IP has hit the box. After processing the two number should be the same as the time in which the hit occured is irrelevant when displaying the number of hits. I'd noticed specific IP's attempting to FTP to the server with usernames beginning with A (anonymous, administrator etc) followed by passwords which were all dictionary based - these were consecutive hits occuring roughly once every 25 seconds (from memory here...). But if one of these connection attempts had a legitimate hit inbetween the connection being opened and the password being specified, this wouldn't effect the totals? Or would it?

Thanks once again
 
Old 10-27-2010, 10:23 AM   #22
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes there is a difference between consecutive and total. If you use crts' input from post #18, and look at the references to ip 110.9.129.72, it first appears and closes 4 times prior to a new
ip being hit. Then the same ip appears at the end of the file 2 more times.

So consecutively it 4 & 2, so if MAX=4 then neither would warrant being displayed.

However, as a total of 6 it would be displayed.

So for consecutive you can use crts' or my last solution.
If total, then my previous awk solution, from post #17, would be the way to go.
 
1 members found this post helpful.
Old 10-27-2010, 01:32 PM   #23
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Got it, cheers grail. I need to review the logs properly but I suspect consecutive hits would make more sense, as it stands this brute force attack only took place once so I need to see how the code functions with both the total hits and total consecutive hits and compare this to my logs (sparce at the moment).

Cheers for all your effort guys, this will become useful to vast ammounts of people I'm sure
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with sed statement Mr. Swillis Linux - Software 4 09-04-2010 09:49 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
sed with if statement Alkass Programming 7 04-30-2010 12:28 AM
Help with Sed Statement zcrxsir88 Programming 1 03-17-2009 04:19 PM
decoding a sed statement Steve Riley Linux - General 3 01-26-2005 11:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration