Skip to content
Snippets Groups Projects
Commit f128b0f8 authored by NGUYEN Do Duc Anh's avatar NGUYEN Do Duc Anh
Browse files

fix bug

parent ca8297b3
Branches
No related tags found
No related merge requests found
......@@ -4,8 +4,8 @@
#include <linux/udp.h>
#include <linux/tcp.h> // For struct tcphdr
#define SUBNET_BASE __constant_htonl(0x7B640100)
#define REPLACE_PROBABILITY 50
#define SUBNET_BASE __constant_htonl(0xAC103C00)
#define REPLACE_PROBABILITY 90
const __be32 ALTERNATE_IPS[] = {__constant_htonl(0x7B640202)};
#define NUM_ALTERNATE_IPS (sizeof(ALTERNATE_IPS) / sizeof(ALTERNATE_IPS[0]))
......@@ -17,7 +17,8 @@ BPF_HASH(ip_pool, __u32, __u32, 1024);
#define MY_OPTION_TYPE 31 // Custom option type
#define MAX_CHECKING 4
static inline __u16 csum_fold_helper(__u32 csum) {
static inline __u16 csum_fold_helper(__u32 csum)
{
// Add overflow (carry folding)
for (__u8 i = 0; csum >> 16 && i < MAX_CHECKING; i += 1)
{
......@@ -27,29 +28,34 @@ static inline __u16 csum_fold_helper(__u32 csum) {
return ~csum;
}
static __always_inline __u32 sum16(const void* data, __u32 size, const void* data_end) {
static __always_inline __u32 sum16(const void *data, __u32 size, const void *data_end)
{
__u32 s = 0;
for (__u8 i = 0; i < 30; i++) {
if (2*i >= size) {
for (__u8 i = 0; i < 30; i++)
{
if (2 * i >= size)
{
return s; /* normal exit */
}
if (data + 2*i + 1 + 1 > data_end) {
if (data + 2 * i + 1 + 1 > data_end)
{
return 0; /* should be unreachable */
}
s += ((const __u16 *)data)[i];
if (2*i + 1 == size) {
if (2 * i + 1 == size)
{
__u8 byte;
if (bpf_probe_read_kernel(&byte, sizeof(byte), data + (i * 2 + 1)))
return 0;
s += byte;
}
}
return s;
}
static inline __u16 tcp_checksum(struct iphdr *ip, struct tcphdr *tcp, void *data_end) {
static inline __u16 tcp_checksum(struct iphdr *ip, struct tcphdr *tcp, void *data_end)
{
tcp->check = 0;
__u32 csum = 0;
__u16 tcp_len = ntohs(ip->tot_len) - (ip->ihl * 4);
......@@ -274,7 +280,7 @@ int inter_op_ebpf(struct xdp_md *ctx)
ip->check = iph_csum(ip, data_end);
tcp->check = tcp_checksum(ip, tcp, data_end);
}
else
else if ((ip->saddr & SUBNET_MASK) == SUBNET_BASE && tcp->dest == DEST_PORT)
{
__u32 alt_ip_index_key = 0;
__u32 *index_ptr = alt_ip_index.lookup(&alt_ip_index_key);
......@@ -282,16 +288,14 @@ int inter_op_ebpf(struct xdp_md *ctx)
return XDP_PASS;
__u32 current_index = *index_ptr;
if (current_index < NUM_ALTERNATE_IPS && (ip->saddr & SUBNET_MASK) == SUBNET_BASE)
{
if (tcp->dest == DEST_PORT)
if (current_index < NUM_ALTERNATE_IPS)
{
__u32 rand_num = bpf_get_prandom_u32() % 100;
if (rand_num < REPLACE_PROBABILITY)
{
// Replace destination IP
__u32 original_ip = ip->daddr;
__u32 new_ip = ALTERNATE_IPS[0];
__u32 new_ip = ALTERNATE_IPS[current_index];
ip->daddr = new_ip;
// Optional: log the replacement
......@@ -307,6 +311,5 @@ int inter_op_ebpf(struct xdp_md *ctx)
}
}
}
}
return XDP_PASS;
}
import subprocess
from bcc import BPF
from scapy.all import *
from scapy.layers.inet import UDP, IP
......@@ -59,21 +57,21 @@ def update_ebpf_file(file_path, subnet_ip, probability, alternate_ips):
f.write(content)
def packet_handler(packet):
def packet_handler(pkt):
global stop_sniffing
if packet.haslayer(UDP) and packet[UDP].dport == 5000:
print(f"From {packet[IP].src}: {packet[UDP].payload}")
if pkt.haslayer(UDP) and pkt[UDP].dport == 5000:
print(f"From {pkt[IP].src}: {pkt[UDP].payload}")
udp_payload = bytes(packet[UDP].payload)
udp_payload = bytes(pkt[UDP].payload)
try:
json_array = json.loads(udp_payload.decode('utf-8'))
print(f"Intercepted from {packet[IP].src}:")
print(f"Intercepted from {pkt[IP].src}:")
print(json_array)
file_path = DIR + '/ubuntu_intermediate/inter_op_ebpf.c'
update_ebpf_file(file_path, packet[IP].dst, 75, json_array)
update_ebpf_file(file_path, pkt[IP].dst, 75, json_array)
stop_sniffing = True
except (UnicodeDecodeError, json.JSONDecodeError):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment