Another simple script to block peers who try to connect to your server

There are 13 replies in this Thread which was already clicked 3,582 times. The last Post () by Mangazes.

  • I have been thinking about how you can block people on your server, without hassle.


    Below is a script I have "modded" all you have to do is copy and paste into the box.


    I have been running it for the past 3 weeks and it's rock steady.


    Script: Anything in below in BLUE is cut and paste.




    #!/bin/sh
    ###########################
    ## Auto Ban script ##
    ## Illegal CCcam Users ##
    ## Modified by Prioryman ##
    ## Thanks to Unlocker-AL ##
    ## for the idea and code ##
    ###########################
    workdir="/var/log" #This is there the debug info from CCcam is dumped
    firewall="/var/log" #The saved firewall rules
    logdir="/var/log" #The logfile directory
    EMAIL="" #Email adres to mail result to ( unless you know what you doing here, Only for PC server NOT Dreambox.
    SUBJECT="Illegal Userlist CCcam" #Subject title of the email
    EMAILMESSAGE="/emu/log/illegaluser.txt" #List that contains the Illegal users

    rm $workdir/debug.old
    cp $workdir/debug.txt $workdir/debug.old
    cp $logdir/iptables-save.new $logdir/iptables-save.old
    cat $workdir/debug.old|grep illegal > $logdir/illegaluser.log
    grep 'illegal' $logdir/illegaluser.log | awk -F" " '{print $5,$7}' > $logdir/illegaluser.txt
    grep 'illegal' $logdir/illegaluser.log | awk -F" " '{print $7}' | grep -o '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' > $logdir/badIP.out
    awk '
    {s[$0]++}
    END {
    for(i in s) {
    if(s[i]>1) {
    print i
    }
    }
    }' $logdir/badIP.out > $logdir/badIP.block
    while IFS= read -r EachLine
    do
    command="iptables -A INPUT -s "$EachLine" -j DROP"
    echo $command
    $command
    done < $logdir/badIP.block
    rm $logdir/badIP.block
    if [ -s $logdir/illegaluser.txt ] ; then
    iptables-save -c > $logdir/iptables-save.new
    /usr/bin/nail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    else
    echo "No Illegal Users found right now"
    fi
    sleep 1
    > $workdir/debug.txt
    exit 0




    The script above will scan you're debug file ( in Var/log ) for illegal users, sort out the user-name and IP and insert the IP in IP-tables.


    If for some reason the file locations are different on your server change them in script ie: /var/log/

    It will also email you the result, so you know what users are put in IP-tables. This only works on a PC server and is fiddly to set-up.


    Now you will need to edit crontab - please do NOT use command crontab -e


    Edit in folder Var using command Nano crontab to finish CTRL + X say Yes press enter/return


    Add below:


    */59 * * * * root /var/script/illegal.sh >> /var/cccamlog/illegalPrats.log



    This will every 59 minutes run the script and leave a text file in the folder Var/cccamlog


    If you do not want to install the above script there is a way to block users manually, however it's slow and a pain.


    Within putty or what ever you use to access your PC server or Dreambox server at the prompt type this:


    iptables -I INPUT -s xxxxxxxxxxxxxx -j DROP



    The xxxxxxxxxxxxxxxx is the IP address of the "pain in the ass" Peer, whom you want to block completely


    On the other hand you can allow access again ( say you want to allow them back ) by doing this:


    iptables -I INPUT -s xxxxxxxxxxxxxx -j ACCEPT


    Notice that the last word has changed - this unblocks him.


    I hope this helps you - It works for my I no longer even check the log files.


    My server is a little faster as I don't have old peers "hammering" my IP address trying to access the Server.

    Kindest regards, Prioryman


    Let your interests be as wide as possible, and let your reactions to the things and persons that interest you, be as far as possible friendly rather than hostile.

  • my server is ubuntu and oscam only.
    do i need to have cccam installed also?


    No, should work with oscam.
    But the folder will still be called cccamlog unless you change it to oscamlog.

    Quote


    */59 * * * * root /var/script/illegal.sh >> /var/oscamlog/illegalPrats.log


    Maybe change everything named CCcam to Oscam.

    Edited once, last by hacked ().

  • i cant get it to work on oscam, do i need to make any additional files for the logs?


    Try this:



    Telnet:

    Quote


    chmod +x /var/script/illegal.sh


    Now you will need to edit crontab - please do NOT use command crontab -e
    Edit in folder Var using command Nano crontab to finish CTRL + X say Yes press enter/return
    Add below:

    Code
    */59 * * * * root /var/script/illegal.sh >> /var/oscamlog/illegalPrats.log



    Not tested but should work.


    Testing script:

    Quote


    root@ubuntu-test:~# cd /var/script
    root@Ubuntu-test:/var/script# ./illegal.sh
    No Illegal Users found right now
    root@ubuntutest:/var/script#

    Edited 2 times, last by hacked ().

  • thanks for your help, i'm still wondering whether this does work on oscam alone as i've done the above but get the following errors


    root@Ubuntu:~# cd /var/script
    root@Ubuntu:/var/script# ./illegal.sh
    rm: cannot remove ‘/var/log/debug.old’: No such file or directory
    cp: cannot stat ‘/var/log/debug.txt’: No such file or directory
    cp: cannot stat ‘/var/log/iptables-save.new’: No such file or directory
    cat: /var/log/debug.old: No such file or directory
    No Illegal Users found right now
    root@Ubuntu:/var/script# root@Ubuntu:~# cd /var/script

  • master G

    Added the Label Article

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!