membuat custom rule pada regex.custom.pm di csf

0
1169

selamat pagi sobat sekolahlinux :D, kali ini saya akan menuliskan bagaimana cara membuat rule baru di csf yang mana nantinya berguna untuk pemblokiran ip yang melakukan serangan.

untuk tutorial install csf nya bisa dilihat disini

http://sekolahlinux.com/integrasi-csf-firewall-dengan-webmin-dan-virtualmin-pada-centos/

nah jika sudah sekarang waktunya membuat rule baru pada csf.

buka file csf.conf

vim /etc/csf/csf.conf

lalu edit letak custom file lognya, dan kali ini saya akan membuat feature rule untuk memblock ip yang gagal ketika melakukan SASL LOGIN, yang mana log errornya ada di /var/log/maillog

errornya kira2 seperti ini

May  2 10:10:30 server postfix/smtpd[24512]: warning: unknown[5.135.19.156]: SASL LOGIN authentication failed: authentication failure
May  2 10:10:30 server postfix/smtpd[24508]: warning: unknown[5.135.19.156]: SASL LOGIN authentication failed: authentication failure

nah lalu rubah letak file lognya menjadi seperti dibawah pada file csf.conf

CUSTOM1_LOG = "/var/log/maillog"
CUSTOM2_LOG = "/var/log/customlog"
CUSTOM3_LOG = "/var/log/customlog"
CUSTOM4_LOG = "/var/log/customlog"
CUSTOM5_LOG = "/var/log/customlog"
CUSTOM6_LOG = "/var/log/customlog"
CUSTOM7_LOG = "/var/log/customlog"
CUSTOM8_LOG = "/var/log/customlog"
CUSTOM9_LOG = "/var/log/customlog"

diatas saya merubah letak file CUSTOM1_LOG menjadi /var/log/maillog.

selanjutnya buka file vim /etc/csf/regex.custom.pm dan tambahkan rulenya menjadi seperti dibawah

#!/usr/bin/perl
###############################################################################
# Copyright 2006-2015, Way to the Web Limited
# URL: http://www.configserver.com
# Email: sales@waytotheweb.com
###############################################################################
sub custom_line {
	my $line = shift;
	my $lgfile = shift;

# Do not edit before this point
###############################################################################
#
# Custom regex matching can be added to this file without it being overwritten
# by csf upgrades. The format is slightly different to regex.pm to cater for
# additional parameters. You need to specify the log file that needs to be
# scanned for log line matches in csf.conf under CUSTOMx_LOG. You can scan up
# to 9 custom logs (CUSTOM1_LOG .. CUSTOM9_LOG)
#
# The regex matches in this file will supercede the matches in regex.pm
#
# Example:
#	if (($globlogs{CUSTOM1_LOG}{$lgfile}) and ($line =~ /^S+s+d+s+S+ S+ pure-ftpd: (?@(d+.d+.d+.d+)) [WARNING] Authentication failed for user/)) {
#		return ("Failed myftpmatch login from",$1,"myftpmatch","5","20,21","1");
#	}
#
# The return values from this example are as follows:
#
# "Failed myftpmatch login from" = text for custom failure message
# $1 = the offending IP address
# "myftpmatch" = a unique identifier for this custom rule, must be alphanumeric and have no spaces
# "5" = the trigger level for blocking
# "20,21" = the ports to block the IP from in a comma separated list, only used if LF_SELECT enabled. To specify the protocol use 53;udp,53;tcp
# "1" = n/temporary (n = number of seconds to temporarily block) or 1/permanant IP block, only used if LF_TRIGGER is disabled


	if (($lgfile eq $config{CUSTOM1_LOG}) and ($line =~ /^S+s+d+s+S+ S+ postfix/smtpd[d+]: warning:.*[(d+.d+.d+.d+)]: SASL [A-Z]* authentication failed/)) {
			return ("Failed SASL login from",$1,"mysaslmatch","1","25","1");
		}


#
# If the matches in this file are not syntactically correct for perl then lfd
# will fail with an error. You are responsible for the security of any regex
# expressions you use. Remember that log file spoofing can exploit poorly
# constructed regex's
###############################################################################
# Do not edit beyond this point

	return 0;
}

1;

nah pada script diatas kita perlu fokus pada script ini

if (($lgfile eq $config{CUSTOM1_LOG}) and ($line =~ /^S+s+d+s+S+ S+ postfix/smtpd[d+]: warning:.*[(d+.d+.d+.d+)]: SASL [A-Z]* authentication failed/)) {
			return ("Failed SASL login from",$1,"mysaslmatch","1","25","1");
		}

script diatas berfungsi untuk mencari keyword yang mirip berikut ini penjelasannya

postfix/smtpd[d+]: =>  backslash berwarna merah menandakan bahwa karakter setelah backslah itu sendiri tidak akan dieksekusi dan akan dibiarkan, begitupun dengan backslah2 lain yang ada di script diatas fungsinya sama.

d+ => d+ adalah (d = digit) dan (+ = lebih dari satu) jadi d+ adalah digit lebih dari satu, rule itu sendiri yang diterapkan oleh csf yang mana nantinya bisa membedakan mana ip dan mana yang bukan

 

berikut ini saya berikan script tambahan untuk bisa dipelajari, namun untuk case ini penyebab errornya adalah adanya email spam yang memaksa masuk dan kemudian terblock oleh spamassasin dengan amavis

berikut ini lognya

May  4 11:01:07 sekolahlinux amavis[25584]: (25584-05) Blocked SPAM {RejectedInbound}, [1.3.3.53]:11904 [1.3.3.53] <akbar@sekolahlinux.com> -> <akbar@sekolahlinux.com>, Message-ID: <002001d0864c$01e514f5$1f679d9e@aqhptwf>, mail_id: 3fdrZtgTe547, Hits: 145.048, size: 1584, Tests: [AXB_XMAILER_MIMEOLE_OL_024C2=0.001,DATE_IN_FUTURE_03_06=2.426,DNET_ATTACK_A2=110,DOS_OE_TO_MX=0.5,RCVD_IN_BRBL_LASTEXT=2.5,RCVD_IN_PBL=4.7,RDNS_NONE=0.5,SPF_SOFTFAIL=0.972,TO_EQ_FM_DIRECT_MX=0.449,URIBL_BLACK=8.9,URIBL_DBL_SPAM=2.5,URIBL_JP_SURBL=5.8,URIBL_WS_SURBL=5.8], 6990 ms

nah untuk memfilter ipnya cukup dengan script seperti dibawah tambahkan di regex.custom.pm

if (($lgfile eq $config{CUSTOM1_LOG}) and ($line =~ /^S+s+d+s+S+ S+ amavis[d+]:.*[(d+.d+.d+.d+)]:.*ms/)) {
                        return ("sekolahlinux Spam",$1,"sekolahlinux","1","25","1");
		}

dan berikut ini penjelasan untuk script code (“sekolahlinux Spam”,$1,”sekolahlinux”,”1″,”25″,”1″);

  • “sekolahlinux Spam” => deskripsi rule
  • $1 => ip yang terblock
  • “sekolahlinux” => nama rulenya
  • “1” => jumlah kesempatan sebelum di blockir
  • “25” => port yang akan di blockir pada ip tersebut
  • “1” => menandakan bahwa ip tersebut akan langsung terbaned secara permanent

jangan lupa untuk merestart service csf dan lfd nya

untuk melihat lognya apakah filter ipnya berhasil atau tidak bisa dengan melakukan tail pada lfd.log seperti dibawah ini

[root@server sekolahlinux]# tail -f /var/log/lfd.log

May  4 11:18:51 server lfd[17818]: (sekolahlinux) sekolahlinux Spam 1.3.3.53 (CN/China/-): 1 in the last 3600 secs - *Blocked in csf* [LF_CUSTOMTRIGGER]

ouu iya untuk kasus spam diatas paling tidak spam tersebut harus terblock dulu disisi spamassasin, baru setelah itu bisa kita proses dengan csf 😀

semoga bermanfaat 😀