<commit_before>#!/bin/bash

SCRIPT_NAME="DDOS CUT v.0.2 by MrMrh"
RELEASE="Release date: 2014-09-22 13:36 GMT"

# Tested on CentOS 6.5, CentOS 7
# Please check all variables before you run this script
# Put this script into your /usr/local/bin folder and make it executable: chmod +x /usr/local/bin/ddos-cut.sh
# Script using system resources and should be running from root or sudo user
# Crontab record you'll need (running every minute): * * * * *	/usr/local/bin/ddos-cut.sh 1> /dev/null 2> /dev/null
# Put this crontab record by: sudo crontab -e
#
# By default script will create:
#	temporary directory /tmp/ddos-cut (will contain scripts for IP unban after )
#	Log file /var/log/ddos-cut
#	IPTABLES chain ddos-cut

C_DATE=`date`
U_DATE=`date '+%s'`
CONNECTIONS_LIMIT=140 		# IP with connctions more than $CONNECTIONS_LIMIT will be banned
BAN_TIME=600 			# Num seconds
LOG_DIR="/var/log/"
LOG_FILE=$LOG_DIR"ddos-cut.log"
NETSTAT="/bin/netstat"
GREP="/bin/grep"
EGREP="/bin/egrep"
AWK="/bin/awk"
SED="/bin/sed"
CUT="/bin/cut"
SORT="/bin/sort"
UNIQ="/usr/bin/uniq"
LS="/bin/ls"
LOGGER="/bin/logger"
IPTABLES="/sbin/iptables"
CHAIN_NAME="ddos-cut"
MKDIR="/bin/mkdir"
TMP_DIR="/tmp/ddos-cut"
LOG_PREF=$C_DATE" [ddos-cut.sh]: "

# Do not edit below this line
# ---------------------------

create_tmp_dir() {
	if [[ ! -e $TMP_DIR ]]; then
		$MKDIR $TMP_DIR
		LOG_TEXT="$LOG_PREF $TMP_DIR created"
		$LOGGER -s "$LOG_TEXT" 2>> $LOG_FILE
	elif [[ ! -d $TMP_DIR ]]; then
	    	LOG_TEXT="$LOG_PREF $TMP_DIR already exists and this is not a directory"
	    	$LOGGER -s "$LOG_TEXT" 2>> $LOG_FILE
	fi
}

create_chain() {
	if [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -eq 0 ]; then
		$IPTABLES -N $CHAIN_NAME
		if [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -gt 0 ]; then
			echo "New chain "$CHAIN_NAME" created."
		fi
	fi
}

banned_list() {
	$IPTABLES -n -L $CHAIN_NAME | $GREP DROP | awk '{print $4}'
}

unban_all() {
	$IPTABLES -F $CHAIN_NAME
	rm -rf $TMP_DIR/* 1>/dev/null 2>/dev/null
	if [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -eq 2 ]; then
		if [ "$VERBOSE" == 1 ]; then
			echo "Chain cleaned"
		fi
	elif [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -eq 0 ]; then
		create_chain
		if [ "$VERBOSE" == 1 ]; then
			echo "Chain doesn't exist. New chain "$CHAIN_NAME" created."
		fi<commit_msg>Updated ddos-cut.sh<commit_after>#!/bin/bash

SCRIPT_NAME="DDOS CUT v.0.3 by MrMrh"
RELEASE="Release date: 2014-09-22 16:40 GMT"

# Tested on CentOS 6.5, CentOS 7
# Please check all the variables before you run this script
# Put this script into your /usr/local/bin folder and make it executable: chmod +x /usr/local/bin/ddos-cut.sh
# Script using system resources and should be running from root or use sudo
# Crontab record you'll need (running every minute): * * * * *	/usr/local/bin/ddos-cut.sh 1> /dev/null 2> /dev/null
# Put this crontab record by: sudo crontab -e
#
# By default script will create:
#	temporary directory /tmp/ddos-cut (will contain scripts for IP unban after )
#	Log file /var/log/ddos-cut
#	IPTABLES chain ddos-cut

C_DATE=`date`
U_DATE=`date '+%s'`
CONNECTIONS_LIMIT=140 		# IP with more or equal than $CONNECTIONS_LIMIT will be banned
BAN_TIME=600 			# sec
LOG_DIR="/var/log/"
LOG_FILE=$LOG_DIR"ddos-cut.log"
NETSTAT="/bin/netstat"
GREP="/bin/grep"
EGREP="/bin/egrep"
AWK="/bin/awk"
SED="/bin/sed"
CUT="/bin/cut"
SORT="/bin/sort"
UNIQ="/usr/bin/uniq"
LS="/bin/ls"
LOGGER="/bin/logger"
IPTABLES="/sbin/iptables"
CHAIN_NAME="ddos-cut"
MKDIR="/bin/mkdir"
TMP_DIR="/tmp/ddos-cut"
LOG_PREF=$C_DATE" [ddos-cut.sh]: "

# Do not edit below this line
# ---------------------------

create_tmp_dir() {
	if [[ ! -e $TMP_DIR ]]; then
		$MKDIR $TMP_DIR
		LOG_TEXT="$LOG_PREF $TMP_DIR created"
		$LOGGER -s "$LOG_TEXT" 2>> $LOG_FILE
	elif [[ ! -d $TMP_DIR ]]; then
	    	LOG_TEXT="$LOG_PREF $TMP_DIR already exists and this is not a directory"
	    	$LOGGER -s "$LOG_TEXT" 2>> $LOG_FILE
	fi
}

create_chain() {
	if [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -eq 0 ]; then
		$IPTABLES -N $CHAIN_NAME
		if [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -gt 0 ]; then
			echo "New chain "$CHAIN_NAME" created."
		fi
	fi
}

banned_list() {
	$IPTABLES -n -L $CHAIN_NAME | $GREP DROP | $AWK '{print $4}'
}

unban_all() {
	$IPTABLES -F $CHAIN_NAME
	rm -rf $TMP_DIR/* 1>/dev/null 2>/dev/null
	if [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -eq 2 ]; then
		if [ "$VERBOSE" == 1 ]; then
			echo "Chain cleaned"
		fi
	elif [ $($IPTABLES -L $CHAIN_NAME 2>/dev/null | wc -l) -eq 0 ]; then
		create_chain
		if [ "$VERBOSE" == 1 ]; then
			echo "Chain doesn't exist. New chain "$CHAIN_NAME" created."
		fi
{'fraction_non_alphanumeric': 0.1696852091418715, 'fraction_numerical': 0.017248814144027597, 'mean_word_length': 3.4648700673724737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697454', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1884, 'n_words': 523}