Initial Commit

Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2025-01-19 09:35:11 +07:00
commit 7845272a33
5 changed files with 167 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.vscode
*.o

3
attach.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/env bash
/usr/sbin/ip link set dev enp11s0 xdp obj xdp_packet_filter.o sec packet_filter

5
build.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/env bash
rm xdp_packet_filter.o
clang -O2 -g -Wall -target bpf -c xdp_packet_filter.c -o xdp_packet_filter.o

3
detach.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/env bash
/usr/sbin/ip link set dev enp11s0 xdp off

154
xdp_packet_filter.c Normal file
View File

@@ -0,0 +1,154 @@
/**
* Packet Filter berbasis XDP
*
* Author : MOVZX
* Email : movzx@yahoo.com
* GitHub : https://github.com/MOVZX
*
* Version : 1.0
*
* File xdp_redm_packet_filter.c
*/
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/icmp.h>
#include <linux/udp.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#define bpf_htons(x) __builtin_bswap16(x)
#define bpf_htonl(x) __builtin_bswap32(x)
/**
* Daftar Port yang tidak terdaftar di Server
* Kalau terdeteksi ya berarti ada aktivitas Port/Vulnerability Scanner
* yang sedang mengintip/menganalisa Server, gak boleh!
*/
#define DAFTAR_BLOKIR \
{ \
20, 21, 22, 23, 25, 53, 67, 68, 69, 110, 111, 135, \
137, 138, 139, 143, 389, 445, 465, 546, 873, 993, 995, \
1080, 1433, 1435, 1900, 2049, 3306, 3389, 5353, 5432, \
5900, 5901, 5902, 6667 \
}
#define DEBUG 1 // 1 = Enable, 0 = Disable
/**
* Fungsi untuk memeriksa apakah port tujuan terdaftar
* dalam daftar port yang terblokir.
*
* @param port: port tujuan yang akan diperiksa.
*
* @return:
* 1 jika port tujuan terdaftar dalam daftar blokir,
* 0 jika port tujuan tidak terdaftar dalam daftar blokir.
*/
static inline int periksa_port_tujuan(__u16 port)
{
__u16 blocked_ports[] = DAFTAR_BLOKIR;
// Loop pemeriksaan port
for (int i = 0; i < sizeof(blocked_ports) / sizeof(blocked_ports[0]); i++)
{
if (bpf_htons(port) == blocked_ports[i])
return 1; // Blok
}
return 0; // Aman
}
/**
* Program filtrasi paket XDP untuk memblokir paket berdasarkan port tujuan.
*
* Fungsi ini memvalidasi dan memproses paket jaringan yang masuk pada level NIC, lalu memeriksa header Ethernet, IP,
* dan lapisan transport (TCP/UDP) serta menerapkan mekanisme filtrasi untuk memblokir lalu lintas yang ditujukan
* ke port tujuan tertentu yang telah ditentukan sebelumnya. Fungsi ini beroperasi sebagai berikut:
*
* - Validasi header Ethernet dan pastikan paket adalah tipe IPv4.
* - Validasi header IP dan periksa protokol (TCP atau UDP).
* - Untuk TCP, validasi header TCP dan periksa apakah port tujuan ada dalam daftar blokir. Jika ya, paket dihentikan.
* - Untuk UDP, validasi header UDP dan periksa apakah port tujuan ada dalam daftar blokir. Jika ya, paket dihentikan.
* - Jika paket melewati semua pemeriksaan dan tidak diblokir, maka paket diizinkan untuk melewati.
*
* Fungsi ini mencatat paket yang diblokir ketika flag/mode DEBUG diaktifkan.
*
* @param ctx: Pointer ke konteks XDP yang menyediakan akses ke data paket.
*
* @return:
* - XDP_ABORTED jika header tidak valid atau paket diblokir.
* - XDP_PASS jika paket diizinkan untuk melewati.
*/
SEC("packet_filter") int xdp_packet_filter(struct xdp_md *ctx)
{
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
// Validasi Ethernet Header
if (data + sizeof(struct ethhdr) > data_end)
return XDP_ABORTED;
struct ethhdr *eth = (struct ethhdr *)data;
// Lewati jika bukan IPv4
if (eth->h_proto != bpf_htons(ETH_P_IP))
return XDP_PASS;
// IP Header
struct iphdr *ip = (struct iphdr *)(eth + 1);
// Validasi IP Header
if ((void *)(ip + 1) > data_end || ip->ihl < 5)
return XDP_ABORTED;
__u32 ip_klien = ip->saddr;
__u32 ip_tujuan = ip->daddr;
// TCP
if (ip->protocol == IPPROTO_TCP)
{
// TCP Header
struct tcphdr *tcp = (struct tcphdr *)(ip + 1);
// Validasi TCP Header
if ((void *)(tcp + 1) > data_end)
return XDP_ABORTED;
// Cek daftar blokir port
if (periksa_port_tujuan(tcp->dest))
{
if (DEBUG)
bpf_printk("[BLOKIR][TCP]: %pI4:%d -> %pI4:%d", &ip_klien, bpf_htons(tcp->source), &ip_tujuan, bpf_htons(tcp->dest));
return XDP_ABORTED;
}
}
// UDP
else if (ip->protocol == IPPROTO_UDP)
{
// UDP Header
struct udphdr *udp = (struct udphdr *)(ip + 1);
// Validasi UDP Header
if ((void *)(udp + 1) > data_end)
return XDP_ABORTED;
// Cek daftar blokir port
if (periksa_port_tujuan(udp->dest))
{
if (DEBUG)
bpf_printk("[BLOKIR][UDP]: %pI4:%d -> %pI4:%d", &ip_klien, bpf_htons(udp->source), &ip_tujuan, bpf_htons(udp->dest));
return XDP_ABORTED;
}
}
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";