Skip to content

Commit

Permalink
Add SSL/TLS sni filter
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Mar 16, 2024
1 parent 69959f0 commit 6f4f6ad
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/libnfdump/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,10 @@ static int RunExtendedFilter(const FilterEngine_t *engine, recordHandle_t *handl
char *str = (char *)data.dataPtr;
evaluate = str != NULL && (strcmp(inPtr, str) == 0 ? 1 : 0);
} break;
case CMP_SUBSTRING: {
char *str = (char *)data.dataPtr;
evaluate = str != NULL && (strstr(inPtr, str) != NULL ? 1 : 0);
} break;
case CMP_BINARY: {
void *dataPtr = data.dataPtr;
evaluate = dataPtr != NULL && memcmp(inPtr, dataPtr, length) == 0;
Expand Down
1 change: 1 addition & 0 deletions src/libnfdump/filter/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef enum {
CMP_IDENT,
CMP_FLAGS,
CMP_STRING,
CMP_SUBSTRING,
CMP_BINARY,
CMP_NET,
CMP_IPLIST,
Expand Down
8 changes: 7 additions & 1 deletion src/libnfdump/filter/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,6 @@ static int AddPayloadSSL(char *type, char *arg, char *opt) {
yyerror("String %s is not a valid SSL/TLS version", arg);
return -1;
}
printf("SSL/TLS version: %s\n", opt);
unsigned int major, minor;
if (sscanf(opt, "%1u.%1u", &major, &minor) != 2 || major > 3 || minor > 3 ) {
yyerror("String %s is not a valid SSL/TLS version", opt);
Expand Down Expand Up @@ -1242,6 +1241,13 @@ static int AddPayloadSSL(char *type, char *arg, char *opt) {
version = major << 8;
}
return NewElement(SSLindex, OFFsslVersion, SIZEsslVersion, version, CMP_EQ, FUNC_NONE, NULLPtr);
} else if (strcasecmp(arg, "sni") == 0) {
if ( opt == NULL || strlen(opt) > 64 ) {
yyerror("Invalid string %s for SSL/TLS sni name", opt != NULL ? opt : "");
return -1;
}
data_t data = {.dataPtr=strdup(opt)};
return NewElement(SSLindex, OFFsslSNI, SIZEsslSNI, 0, CMP_SUBSTRING, FUNC_NONE, data);
}
yyerror("String %s is not a valid SSL/TLS filter", arg);
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/libnfdump/ssl/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ typedef struct ssl_s {
char alpnName[ALPNmaxLen];
char sniName[256];
#define OFFsslSNI offsetof(ssl_t, sniName)
#define SIZEsslSNI MemberSize(ssl_t, sniName)
} ssl_t;

void sslPrint(ssl_t *ssl);
Expand Down
3 changes: 3 additions & 0 deletions src/test/nftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,10 @@ static void runTest(void) {
CheckFilter("payload ssl defined", recordHandle, 1);
CheckFilter("payload tls version 1.2", recordHandle, 1);
CheckFilter("payload tls version 1.3", recordHandle, 0);
CheckFilter("payload ssl sni example", recordHandle, 1);
CheckFilter("payload ssl sni nonexist", recordHandle, 0);
recordHandle->extensionList[SSLindex] = NULL;
CheckFilter("payload ssl sni example", recordHandle, 0);

// ja3
recordHandle->extensionList[JA3index] = "123456789abcdef0123456789abcdef0";
Expand Down

0 comments on commit 6f4f6ad

Please sign in to comment.