-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sensitive traffic detection (#239)
- Loading branch information
1 parent
2832c74
commit e2cea47
Showing
13 changed files
with
157 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,7 @@ | |
# IDE | ||
.idea/ | ||
*.iml | ||
|
||
# BPF specific files | ||
*.o | ||
vmlinux.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
//go:build ignore | ||
|
||
// Common header for all eBPF programs | ||
#include "headers.h" | ||
#include "maps.h" | ||
#include "filters.h" | ||
#include "common.h" | ||
#include "include/headers.h" | ||
|
||
// Event logic | ||
#include "include/events/events.h" | ||
#include "include/events/events.c" | ||
|
||
// Filter logic | ||
#include "include/filters/pci.h" | ||
#include "include/filters/pci.c" | ||
|
||
#include "include/filters/filters.h" | ||
#include "include/filters/filters.c" | ||
|
||
// All eBPF programs | ||
#include "gotls.ebpf.c" | ||
#include "openssl.ebpf.c" | ||
#include "gotls/gotls.ebpf.c" | ||
#include "openssl/openssl.ebpf.c" | ||
|
||
char _license[] SEC("license") = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
|
||
#define HOST_HEADER_LEN 6 | ||
#define AUTH_HEADER_LEN 14 | ||
#define MAX_HEADER_LENGTH 255 | ||
|
||
#define HOST_HEADER "Host: " | ||
#define AUTH_HEADER "Authorization: " | ||
|
||
#define HOST_AWS "amazonaws.com" | ||
#define HOST_AWS_LEN 13 | ||
|
||
struct http_request_t { | ||
// Request headers | ||
__u32 host_len; | ||
char host[MAX_HEADER_LENGTH]; | ||
|
||
__u32 auth_len; | ||
char auth[MAX_HEADER_LENGTH]; | ||
|
||
// Internal state | ||
char cur_line[MAX_HEADER_LENGTH]; | ||
}; | ||
|
||
struct http_request_ctx_t { | ||
__u8 *data; | ||
int data_len; | ||
__u32 line_start; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); | ||
__type(key, __u32); | ||
__type(value, struct http_request_t); | ||
__uint(max_entries, 1); | ||
} http_request_map SEC(".maps"); | ||
|
||
|
||
// ####################################################################### // | ||
// Function declarations | ||
// ####################################################################### // | ||
|
||
static __inline bool should_send_event(struct ssl_event_t *event); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
#include "pci.h" | ||
|
||
// Check if the character is a digit | ||
static __inline bool is_digit(char c) { | ||
return c >= '0' && c <= '9'; | ||
} | ||
|
||
// Luhn check to validate a card number | ||
// Ref: https://en.wikipedia.org/wiki/Luhn_algorithm | ||
bool luhn_check(const char *card_number, int len) { | ||
__u32 sum = 0; | ||
bool even = true; // Start with alternating since the check digit is not doubled | ||
|
||
// Process all digits except the last one (check digit) | ||
for (int i = len - 2; i >= 0; i--) { | ||
__u32 digit = card_number[i] - '0'; // Convert character to integer | ||
if (digit < 0 || digit > 9) return false; // value may only contain digits | ||
if (even) digit *= 2; // double the value | ||
if (digit > 9) digit -= 9; | ||
|
||
even = !even; | ||
sum += digit; | ||
} | ||
|
||
// Add the check digit (last digit) to the sum | ||
__u32 checksum = card_number[len - 1] - '0'; | ||
sum += checksum; | ||
|
||
// If the total modulo 10 is 0, the number is valid | ||
return (sum % 10 == 0); | ||
} | ||
|
||
// Helper function to check for card-like sequences | ||
bool detect_card_number(const char *data, int data_len) { | ||
int digit_count = 0; | ||
char card_number[MAX_CARD_LEN]; // Store potential card number sequence | ||
|
||
for (int i = 0; i < data_len; i++) { | ||
if (is_digit(data[i])) { | ||
card_number[digit_count] = data[i]; // Store digit | ||
digit_count++; | ||
|
||
// Reset count if we exceed the maximum card number length | ||
if (digit_count > MAX_CARD_LEN) digit_count = 0; | ||
} else { | ||
// Check if we have a valid card number length and validate with Luhn check | ||
if (digit_count >= MIN_CARD_LEN && digit_count <= MAX_CARD_LEN) { | ||
if (luhn_check(card_number, digit_count)) return true; | ||
} | ||
|
||
digit_count = 0; // Reset count if non-digit found | ||
} | ||
} | ||
|
||
// Check again at the end in case the card number is at the end of the string | ||
if (digit_count >= MIN_CARD_LEN && digit_count <= MAX_CARD_LEN) { | ||
return luhn_check(card_number, digit_count); | ||
} | ||
|
||
return false; // No valid card number detected | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
#define MIN_CARD_LEN 13 | ||
#define MAX_CARD_LEN 19 | ||
|
||
bool detect_card_number(const char *data, int data_len); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters