Skip to main content
  1. Posts/

Custom Regex Rules in CSF

·5 mins·
csf csf
Table of Contents

Custom Regex Rules di CSF memungkinkan Anda untuk menetapkan aturan khusus berdasarkan pola teks tertentu yang ingin Anda terapkan pada trafik masuk ke server. Ini sering digunakan untuk mengidentifikasi dan memblokir trafik yang mencurigakan, seperti serangan brute force, shell scanner, atau aktivitas mencurigakan lainnya.

Untuk contoh kasus pada panduan kali ini, kita akan mencoba membuat custom regex rule untuk memblokir berdasarkan access_log.

Test script
#

Sebelum implementasi, Anda dapat melakukan pengetesan regex yang telah dibuat menggunakan script perl berikut.

#!/usr/bin/perl
use strict;
use warnings;

# Sample Apache log line with 403 status code
my $log_entry = '192.168.100.30 - - [20/Jul/2024:13:47:18 +0700] "GET /admin HTTP/1.1" 403 199';

# Regular expression pattern to match Apache log entry with 403 status
my $pattern = qr/^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]*)" 403 (\d+)$/;

# Matching the log entry against the pattern
if ($log_entry =~ $pattern) {
    my $client_ip = $1;
    my $identity = $2;
    my $username = $3;
    my $timestamp = $4;
    my $request = $5;
    my $size = $6;
    my $response_time = $7;

    # Print the captured fields
    print "Client IP: $client_ip\n";
    print "Identity: $identity\n";
    print "Username: $username\n";
    print "Timestamp: $timestamp\n";
    print "Request: $request\n";
    print "Size: $size\n";
    print "Response Time: $response_time\n";
} else {
    print "No match found.\n";
}

Script tersebut akan mengembalikan output jika regex match dengan log yang dimasukan.

# perl test.pl
Client IP: 192.168.100.30
Identity: -
Username: -
Timestamp: 20/Jul/2024:13:47:18 +0700
Request: GET /admin HTTP/1.1
Size: 199
Use of uninitialized value $response_time in concatenation (.) or string at test.pl line 29.
Response Time:

Jika regex tidak match dengan seluruh variabel script, maka outputnya menjadi No match found.

regex.custom.pm
#

Pada bagian ini kita akan membuat custom regex rule berdasarkan access_log berikut.

192.168.162.79 - - [22/Jul/2024:06:25:07 +0000] "GET /admin/ HTTP/1.1" 403 199

Edit atau tambahkan CUSTOM1_LOG di /etc/csf/csf.conf, kemudian arahkan ke path access_log situs Anda.

LF_SELECT = "1"
CUSTOM1_LOG = "/var/log/httpd/web1-access_log"

Selanjutnya edit /etc/csf/regex.custom.pm lalu tambahkan baris berikut.

if (($globlogs{CUSTOM1_LOG}{$lgfile}) and ($line =~ /^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]*)" 403 (\d+)$/)) {
        return ("Apache Security rule triggered from",$1,"httpd_403","50","80,443","86400","0");
}

Penjelasan:

  • ($line =~ /^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]*)" 403 (\d+)$/)): Merupakan regex yang dibuat berdasarkan access_log.
  • Apache Security rule triggered from: Teks custom yang akan dicatat ke dalam file log lfd.log apabila mencapai HIT dari rule yang ditentukan.
  • $1: Menunjuk ke alamat IP yang akan diblokir.
  • httpd_403: Merupakan nama unik untuk mengidentifikasi custom rule.
  • 50: Untuk menentukan jumlah HIT.
  • 80,443: Untuk memblokir IP berdasarkan port HTTP dan HTTPS. Anda juga dapat menentukan protokol dari port yang ingin diblokir dengan cara berikut 53;udp,53;tcp
  • 86400: Durasi dalam detik untuk memblokir IP secara temporer.
  • 0: Untuk memblokir IP di Cloudflare jika CF_ENABLE diaktifkan.

Restart service CSF+LFD untuk menerapkan perubahan.

csf -ra

Untuk pengetesan, gunakan bantuan tool seperti wrk.

wrk --latency http://domain.com/admin/

Jika berhasil maka IP akan diblokir lalu akan dicatat pada file log /var/log/lfd.log

Jul 22 06:25:11 lamp lfd[12397]: (httpd_403) Apache Security rule triggered from 192.168.162.79: 50 in the last 3600 secs - *Blocked in csf* for 86400 secs [LF_CUSTOMTRIGGER]

LF_APACHE_404
#

Pada bagian ini kita akan membuat custom regex rule berdasarkan access_log berikut.

192.168.185.210 - - [22/Jul/2024:08:53:13 +0000] "GET /siteadmin HTTP/1.1" 404 196

Selanjutnya edit file /etc/csf/csf.conf seperti berikut.

LF_APACHE_404 = "50"
LF_APACHE_404_PERM = "3600"
HTACCESS_LOG = "/var/log/httpd/access_log"

Penjelasan:

  • LF_APACHE_404: Untuk menentukan jumlah HIT yang memicu pemblokiran IP.
  • LF_APACHE_404_PERM: Untuk menentukan durasi dalam detik untuk memblokir IP secara temporer.
  • HTACCESS_LOG: Untuk menentukan path dari access_log atau error_log yang akan di watching.

Kemudian edit regex pada fungsi loginline404 di file /usr/local/csf/lib/ConfigServer/RegexMain.pm.

# start loginline404
sub loginline404 {
        my $line = shift;
        if ($line =~ /^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]*)" 404 (\d+)$/) {
        my $ip = $1;
                $ip =~ s/^::ffff://;
                if ($config{LF_APACHE_ERRPORT} == 2 and $ip =~ /(.*):\d+$/) {$ip = $1}
                if (checkip(\$ip)) {return ($ip)} else {return}
        }
}
# end loginline404

Restart service CSF+LFD untuk menerapkan perubahan.

csf -ra

Lakukan pengetesan dengan bantuan tool wrk.

wrk --latency http://domain.com/dir404

Jika berhasil maka IP akan diblokir lalu akan dicatat pada file log /var/log/lfd.log.

Jul 22 16:18:13 lamp lfd[29830]: 192.168.162.79, more than 50 Apache 404 hits in the last 3600 secs - *Blocked in csf* for 3600 secs [LF_APACHE_404]
Jul 22 16:18:14 lamp lfd[29827]: (PERMBLOCK) 192.168.162.79 has had more than 4 temp blocks in the last 86400 secs - *Blocked in csf* [LF_APACHE_404]

Anda juga dapat melakukan hal yang serupa pada konfigurasi LF_APACHE_403 dan LF_APACHE_401.

Example regex
#

Contoh regex berdasarkan log.

192.168.131.50 - - [27/Jul/2024:22:41:09 +0700] "GET /images/wp-login.php HTTP/1.1" 404 1238 example.com "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0" WL:"0" "-" XFF:"-" CAPTCHA:"0" PEER:192.168.131.50
my $pattern = qr/^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]*)" \b404\b/;
192.168.152.226 - - [27/Jul/2024:23:44:40 +0700] "GET /skl/assets/bootstrap.min.js HTTP/1.1" 200 1608 example.com "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.2; +https://openai.com/gptbot)" WL:"0" "-" XFF:"-" CAPTCHA:"0" PEER:192.168.152.226
my $pattern = qr/^(\S+) \S+ \S+ \[([^]]+)\].* "([^"]*\bGPTBot\/\d+\.\d+;[^"]*)"/;
192.168.173.252 - - [30/Jul/2024:02:23:49 +0700] "POST /wp-login.php HTTP/1.1" 200 1242 example.com "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36" WL:"0" "-" XFF:"-" CAPTCHA:"0" PEER:192.168.173.252
my $pattern = qr/(\S+) -.*[GET|POST].*(wp-login.php)/;
192.168.153.54 - - [31/Jul/2024:15:31:09 +0700] "POST /administrator/index.php HTTP/1.1" 200 267348 example.com "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36" WL:"0" "-" XFF:"-" CAPTCHA:"0" PEER:192.168.153.54
my $pattern = qr/^(\S+) -.*[GET|POST].*(\/administrator\/index\.php)/;
192.168.240.32 - - [18/Aug/2024:16:26:35 +0700] "GET / HTTP/1.1" 200 24929 example.net "-" "python-requests/2.32.3" WL:"0" "-" XFF:"-" CAPTCHA:"0" PEER:192.168.240.32
my $pattern = qr/^(\S+).*?"python-requests\/([0-9\.]+)"/;
2024-09-04 19:43:51 60986 [Warning] Access denied for user 'root'@'192.168.240.242' (using password: YES)
my $pattern = qr/Access denied for user .*'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'/;

Referensi:

Related

How to Enable Directory Watching in CSF
·2 mins
csf csf
Install dan Konfigurasi CSF di cPanel
·3 mins
csf cpanel csf
Setting Country Allow Port CSF
·1 min
csf csf
Configuring Perl CPAN Mirror List
·2 mins
perl perl
Blocking URI paths using ModSecurity
·2 mins
modsec modsec apache
Cara Mengedit DNS Zone di cPanel
·2 mins
cpanel cpanel