v0.0.3
This commit is contained in:
@@ -10,7 +10,6 @@ AS_IF([test "x$enable_debug" = xyes],
|
||||
[CFLAGS="-ggdb3 -O0 -fsanitize=address -fno-omit-frame-pointer"],
|
||||
[CFLAGS="-g -O2"])
|
||||
|
||||
|
||||
AC_PROG_CC
|
||||
AC_PROG_CPP
|
||||
AC_PROG_INSTALL
|
||||
|
||||
@@ -14,9 +14,10 @@
|
||||
|
||||
#include <pcap.h>
|
||||
|
||||
#include "libosfp.h"
|
||||
#include "libosfp_fingerprint.h"
|
||||
#include "libosfp_score_db.h"
|
||||
#include "osfp_common.h"
|
||||
#include "osfp.h"
|
||||
#include "osfp_fingerprint.h"
|
||||
#include "osfp_score_db.h"
|
||||
|
||||
#define DEFAULT_FP_FILE_PATH "./fp.json"
|
||||
|
||||
@@ -434,82 +435,36 @@ const char *PrintInet(int af, const void *src, char *dst, socklen_t size)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void example_detect(libosfp_context_t *libosfp_context, Packet *p)
|
||||
void example_detect(struct osfp_db *osfp_db, Packet *p)
|
||||
{
|
||||
int ret;
|
||||
char str_buf[1024];
|
||||
//unsigned char *iph = (unsigned char *)(p->iph != NULL ? (void *)p->iph : (void *)p->ip6h);
|
||||
struct tcphdr *tcph;
|
||||
unsigned int tcph_len;
|
||||
struct osfp_result *result;
|
||||
unsigned int os_class_flags = OSFP_OS_CLASS_FLAG_WINDOWS | OSFP_OS_CLASS_FLAG_LINUX | OSFP_OS_CLASS_FLAG_MAC_OS;
|
||||
|
||||
unsigned char *iph = (unsigned char *)(p->iph != NULL ? (void *)p->iph : (void *)p->ip6h);
|
||||
unsigned char *tcph = (unsigned char *)p->tcph;
|
||||
libosfp_result_t result;
|
||||
unsigned int os_class_flags = LIBOSFP_OS_CLASS_FLAG_WINDOWS | LIBOSFP_OS_CLASS_FLAG_LINUX | LIBOSFP_OS_CLASS_FLAG_MAC_OS;
|
||||
printf("Example ipv4 header detect: --------------------------\n");
|
||||
|
||||
printf("Example header detect: --------------------------\n");
|
||||
if (p->iph == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = libosfp_detect(libosfp_context, os_class_flags, iph, tcph, &result);
|
||||
if (ret != 0) {
|
||||
printf("libosfp header match failed, erro: %s\n", "?");
|
||||
tcph = (struct tcphdr *)p->tcph;
|
||||
tcph_len = tcph->doff << 2;
|
||||
|
||||
result = osfp_ipv4_identify(osfp_db, p->iph, tcph, tcph_len);
|
||||
if (result == NULL) {
|
||||
printf("osfp header match failed, erro: %s\n", "?");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf("Connection info: %s:%d -> %s:%d\n", p->srcip, p->sp, p->dstip, p->dp);
|
||||
printf("Most likely os class: %s\n", libosfp_result_likely_os_class_name_get(&result));
|
||||
printf("Likely score: %u/100\n", result.score.likely_score);
|
||||
printf("Most likely os class: %s\n", osfp_result_os_name_get(result));
|
||||
|
||||
printf("Details:\n");
|
||||
if (libosfp_result_to_buf(&result, str_buf, sizeof(str_buf))) {
|
||||
printf("%s", str_buf);
|
||||
}
|
||||
|
||||
exit:
|
||||
return;
|
||||
}
|
||||
|
||||
void example_detect_fingerprint(libosfp_context_t *libosfp_context, Packet *p)
|
||||
{
|
||||
int ret;
|
||||
char str_buf[1024];
|
||||
|
||||
unsigned char *iph = (unsigned char *)(p->iph != NULL ? (void *)p->iph : (void *)p->ip6h);
|
||||
unsigned char *tcph = (unsigned char *)p->tcph;
|
||||
libosfp_result_t result;
|
||||
libosfp_fingerprint_t fp;
|
||||
|
||||
// fingerprinting
|
||||
printf("Example fingerprint detect: --------------------------\n");
|
||||
memset(&fp, 0, sizeof(libosfp_fingerprint_t));
|
||||
ret = libosfp_fingerprinting(iph, tcph, &fp);
|
||||
if (ret != 0) {
|
||||
printf("libosfp fingerprinting failed\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
libosfp_fingerprint_to_json_buf(&fp, str_buf, sizeof(str_buf), 1);
|
||||
printf("%s\n", str_buf);
|
||||
|
||||
// output fingerprint with connection info line
|
||||
if (fingerprinting_output_fp) {
|
||||
fprintf(fingerprinting_output_fp, "Connection info: %s:%d -> %s:%d\n", p->srcip, p->sp, p->dstip, p->dp);
|
||||
fprintf(fingerprinting_output_fp, "%s\n", str_buf);
|
||||
fflush(fingerprinting_output_fp);
|
||||
}
|
||||
|
||||
// score
|
||||
memset(&result, 0, sizeof(libosfp_result_t));
|
||||
ret = libosfp_score_db_score(libosfp_context->score_db, 0, &fp, &result.score);
|
||||
if (ret != 0) {
|
||||
printf("libosfp fingerprint score failed, error: %d\n", ret);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf("Connection info: %s:%d -> %s:%d\n", p->srcip, p->sp, p->dstip, p->dp);
|
||||
printf("Most likely os class: %s\n", libosfp_result_likely_os_class_name_get(&result));
|
||||
printf("Likely score: %u/100\n", result.score.likely_score);
|
||||
|
||||
printf("Details:\n");
|
||||
if (libosfp_result_to_buf(&result, str_buf, sizeof(str_buf))) {
|
||||
printf("%s", str_buf);
|
||||
}
|
||||
printf("%s\n", osfp_result_score_detail_export(result));
|
||||
|
||||
exit:
|
||||
return;
|
||||
@@ -518,7 +473,7 @@ exit:
|
||||
void process_packet(char *user, struct pcap_pkthdr *h, u_char *pkt)
|
||||
{
|
||||
int ret;
|
||||
libosfp_context_t *libosfp_context = (libosfp_context_t *)user;
|
||||
struct osfp_db *osfp_db = (struct osfp_db *)user;
|
||||
Packet packet = {0}, *p = &packet;
|
||||
|
||||
// decode packet
|
||||
@@ -540,11 +495,8 @@ void process_packet(char *user, struct pcap_pkthdr *h, u_char *pkt)
|
||||
PrintInet(AF_INET6, (const void *)&(p->dst.address), p->dstip, sizeof(p->dstip));
|
||||
}
|
||||
|
||||
// fingerprint detect example for libosfp developer
|
||||
example_detect_fingerprint(libosfp_context, p);
|
||||
|
||||
// tcp/ip header detect example for user
|
||||
example_detect(libosfp_context, p);
|
||||
example_detect(osfp_db, p);
|
||||
|
||||
printf("--------------------------- processed packet count %d\n", ++processed_packet);
|
||||
|
||||
@@ -654,39 +606,31 @@ int main(int argc, char *argv[])
|
||||
// get link type
|
||||
link_type = pcap_datalink(pcap_handle);
|
||||
|
||||
// create libosfp context
|
||||
// create osfp db
|
||||
if (fp_file_path == NULL) {
|
||||
fp_file_path = DEFAULT_FP_FILE_PATH;
|
||||
}
|
||||
|
||||
//libosfp_context_t *libosfp_context = libosfp_context_create(fp_file_path);
|
||||
libosfp_context_t *libosfp_context = libosfp_context_create(NULL);
|
||||
if (libosfp_context == NULL) {
|
||||
printf("could not create libosfp context. fingerprints file: %s\n", fp_file_path);
|
||||
struct osfp_db *osfp_db = osfp_db_new(fp_file_path);
|
||||
if (osfp_db == NULL) {
|
||||
printf("could not create osfp context. fingerprints file: %s\n", fp_file_path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// setup libosfp context
|
||||
r = libosfp_context_setup(libosfp_context);
|
||||
if (r != LIBOSFP_NOERR) {
|
||||
printf("could not setup libosfp context. error: %d\n", LIBOSFP_NOERR);
|
||||
libosfp_context_destroy(libosfp_context);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
libosfp_score_db_debug_print(libosfp_context->score_db);
|
||||
osfp_score_db_debug_print(osfp_db->score_db);
|
||||
|
||||
// loop
|
||||
while (1) {
|
||||
int r = pcap_dispatch(pcap_handle, 0, (pcap_handler)process_packet, (void*)libosfp_context);
|
||||
int r = pcap_dispatch(pcap_handle, 0, (pcap_handler)process_packet, (void*)osfp_db);
|
||||
if (r < 0) {
|
||||
printf("error code: %d, error: %s\n", r, pcap_geterr(pcap_handle));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// destroy libosfp context
|
||||
libosfp_context_destroy(libosfp_context);
|
||||
// destroy osfp db
|
||||
osfp_db_free(osfp_db);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
38
gen_c.sh
38
gen_c.sh
@@ -1,38 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
which jq >& /dev/null || (echo "error: ${0} require system command \"jq\"." && exit)
|
||||
|
||||
DEFAULT_FINGERPRINTS=$1
|
||||
if [[ ${DEFAULT_FINGERPRINTS} == "" ]]; then
|
||||
DEFAULT_FINGERPRINTS="./fp.json"
|
||||
fi
|
||||
|
||||
C_FILE_PATH=$2
|
||||
if [[ ${DEFAULT_FILE_PATH} == "" ]]; then
|
||||
C_FILE_PATH=src
|
||||
fi
|
||||
|
||||
C_INCLUDE_FILE="${C_FILE_PATH}/libosfp_default_fingerprints.h"
|
||||
C_SOURCE_FILE="${C_FILE_PATH}/libosfp_default_fingerprints.c"
|
||||
|
||||
|
||||
|
||||
cat > ${C_INCLUDE_FILE} <<EOF
|
||||
// File generated by gen_c.sh
|
||||
#ifndef _LIBOSFP_DEFAULT_FINGERPRINTS_H__
|
||||
#define _LIBOSFP_DEFAULT_FINGERPRINTS_H__
|
||||
extern const char *g_default_fingerprints;
|
||||
#endif
|
||||
EOF
|
||||
|
||||
|
||||
cat > ${C_SOURCE_FILE} <<EOF
|
||||
// File generated by gen_c.sh
|
||||
const char *g_default_fingerprints =
|
||||
EOF
|
||||
|
||||
jq -c . ${DEFAULT_FINGERPRINTS} | jq -R >> ${C_SOURCE_FILE}
|
||||
|
||||
cat >> ${C_SOURCE_FILE} <<EOF
|
||||
;
|
||||
EOF
|
||||
BIN
pcap/test.pcap
Normal file
BIN
pcap/test.pcap
Normal file
Binary file not shown.
@@ -9,17 +9,15 @@ libosfp_la_SOURCES = \
|
||||
utstring.h \
|
||||
cJSON.h \
|
||||
cJSON.c \
|
||||
libosfp_common.h \
|
||||
libosfp_common.c \
|
||||
libosfp_default_fingerprints.h \
|
||||
libosfp_default_fingerprints.c \
|
||||
libosfp.h \
|
||||
libosfp.c \
|
||||
libosfp_fingerprint.h \
|
||||
libosfp_fingerprint.c \
|
||||
libosfp_log.h \
|
||||
libosfp_log.c \
|
||||
libosfp_score_db.h \
|
||||
libosfp_score_db.c
|
||||
osfp_common.h \
|
||||
osfp_common.c \
|
||||
osfp.h \
|
||||
osfp.c \
|
||||
osfp_fingerprint.h \
|
||||
osfp_fingerprint.c \
|
||||
osfp_log.h \
|
||||
osfp_log.c \
|
||||
osfp_score_db.h \
|
||||
osfp_score_db.c
|
||||
|
||||
pkginclude_HEADERS = libosfp.h libosfp_fingerprint.h
|
||||
pkginclude_HEADERS = osfp.h osfp_fingerprint.h osfp_score_db.h osfp_common.h
|
||||
|
||||
135
src/libosfp.c
135
src/libosfp.c
@@ -1,135 +0,0 @@
|
||||
#include "libosfp.h"
|
||||
#include "libosfp_fingerprint.h"
|
||||
#include "libosfp_score_db.h"
|
||||
#include "libosfp_log.h"
|
||||
|
||||
const char *libosfp_result_likely_os_class_name_get(libosfp_result_t *result)
|
||||
{
|
||||
libosfp_os_class_id_t os_class;
|
||||
|
||||
if (result == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
os_class = result->score.likely_os_class;
|
||||
if (os_class < 0 || os_class >= LIBOSFP_OS_CLASS_MAX) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return libosfp_os_class_id_to_name(os_class);
|
||||
}
|
||||
|
||||
int libosfp_result_to_buf(libosfp_result_t *result, char *strbuf, unsigned int buf_len)
|
||||
{
|
||||
int ret, offset = 0, i;
|
||||
libosfp_os_class_id_t likely_os_class;
|
||||
|
||||
if (result == NULL || strbuf == NULL || buf_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
likely_os_class = result->score.likely_os_class;
|
||||
if (likely_os_class < 0 || likely_os_class >= LIBOSFP_OS_CLASS_MAX) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
LIBOSFP_WRITE_STRING_TO_BUF(ret, strbuf, buf_len, offset,
|
||||
"Most likely os class: %s\nLikely score: %u/100\n",
|
||||
libosfp_os_class_id_to_name(likely_os_class), result->score.likely_score);
|
||||
|
||||
for (i = 0; i < LIBOSFP_OS_CLASS_MAX; i++) {
|
||||
LIBOSFP_WRITE_STRING_TO_BUF(ret, strbuf, buf_len, offset,"%s score: %u\n",
|
||||
libosfp_os_class_id_to_name(i), result->score.os_class_score[i]);
|
||||
}
|
||||
|
||||
exit:
|
||||
return offset;
|
||||
}
|
||||
|
||||
libosfp_error_code_t libosfp_detect(libosfp_context_t *libosfp_context, unsigned int flags, unsigned char *ip_hdr, unsigned char *tcp_hdr, libosfp_result_t *result)
|
||||
{
|
||||
int ret = LIBOSFP_EINVAL;
|
||||
libosfp_fingerprint_t fp;
|
||||
|
||||
if (libosfp_context == NULL || ip_hdr == NULL || tcp_hdr == NULL || result == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = libosfp_fingerprinting(ip_hdr, tcp_hdr, &fp);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = libosfp_score_db_score(libosfp_context->score_db, flags, &fp, &result->score);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return LIBOSFP_NOERR;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
libosfp_error_code_t libosfp_context_setup(libosfp_context_t *libosfp_context)
|
||||
{
|
||||
int ret = LIBOSFP_EINVAL;
|
||||
|
||||
if (libosfp_context == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = libosfp_score_db_load((libosfp_score_db_t *)libosfp_context->score_db, libosfp_context->fp_file);
|
||||
if (ret != LIBOSFP_NOERR) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return LIBOSFP_NOERR;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
libosfp_context_t *libosfp_context_create(char *fp_file)
|
||||
{
|
||||
libosfp_context_t *libosfp_context;
|
||||
|
||||
libosfp_context = calloc(1, sizeof(libosfp_context_t));
|
||||
if (libosfp_context == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (fp_file != NULL) {
|
||||
if (0 != access(fp_file, R_OK)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
libosfp_context->fp_file = strdup((const char*)fp_file);
|
||||
if (libosfp_context->fp_file == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
libosfp_context->score_db = (void *)libosfp_score_db_create();
|
||||
if (libosfp_context->score_db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return libosfp_context;
|
||||
exit:
|
||||
if (libosfp_context) {
|
||||
libosfp_context_destroy(libosfp_context);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void libosfp_context_destroy(libosfp_context_t *libosfp_context)
|
||||
{
|
||||
if (libosfp_context) {
|
||||
if (libosfp_context->fp_file) {
|
||||
free(libosfp_context->fp_file);
|
||||
}
|
||||
if (libosfp_context->score_db) {
|
||||
libosfp_score_db_destroy(libosfp_context->score_db);
|
||||
}
|
||||
free(libosfp_context);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef __LIBOSFP_H__
|
||||
#define __LIBOSFP_H__
|
||||
|
||||
#include "libosfp_common.h"
|
||||
|
||||
typedef struct libosfp_result {
|
||||
libosfp_score_t score;
|
||||
} libosfp_result_t;
|
||||
|
||||
typedef struct libosfp_context {
|
||||
char *fp_file;
|
||||
void *score_db;
|
||||
} libosfp_context_t;
|
||||
|
||||
int libosfp_result_to_buf(libosfp_result_t *result, char *strbuf, unsigned int buf_len);
|
||||
const char *libosfp_result_likely_os_class_name_get(libosfp_result_t *result);
|
||||
|
||||
libosfp_error_code_t libosfp_detect(libosfp_context_t *libosfp_context, unsigned int flags, unsigned char *ip_hdr, unsigned char *tcp_hdr, libosfp_result_t *result);
|
||||
libosfp_error_code_t libosfp_context_setup(libosfp_context_t *libosfp_context);
|
||||
libosfp_context_t *libosfp_context_create(char *fp_file);
|
||||
void libosfp_context_destroy(libosfp_context_t *libosfp_context);
|
||||
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
#include "libosfp_common.h"
|
||||
|
||||
const char *os_class_name[LIBOSFP_OS_CLASS_MAX] = {
|
||||
LIBOSFP_OS_CLASS_NAME_WINDOWS,
|
||||
LIBOSFP_OS_CLASS_NAME_LINUX,
|
||||
LIBOSFP_OS_CLASS_NAME_MAC_OS,
|
||||
LIBOSFP_OS_CLASS_NAME_IOS,
|
||||
LIBOSFP_OS_CLASS_NAME_ANDROID
|
||||
};
|
||||
|
||||
libosfp_os_class_id_t libosfp_os_class_name_to_id(char *name)
|
||||
{
|
||||
libosfp_os_class_id_t os_class;
|
||||
|
||||
if (0 == strncmp(name, LIBOSFP_OS_CLASS_NAME_WINDOWS, strlen(LIBOSFP_OS_CLASS_NAME_WINDOWS))) {
|
||||
os_class = LIBOSFP_OS_CLASS_WINDOWS;
|
||||
} else if (0 == strncmp(name, LIBOSFP_OS_CLASS_NAME_LINUX, strlen(LIBOSFP_OS_CLASS_NAME_LINUX))) {
|
||||
os_class = LIBOSFP_OS_CLASS_LINUX;
|
||||
} else if (0 == strncmp(name, LIBOSFP_OS_CLASS_NAME_MAC_OS, strlen(LIBOSFP_OS_CLASS_NAME_MAC_OS))) {
|
||||
os_class = LIBOSFP_OS_CLASS_MAC_OS;
|
||||
} else if (0 == strncmp(name, LIBOSFP_OS_CLASS_NAME_IOS, strlen(LIBOSFP_OS_CLASS_NAME_IOS))) {
|
||||
os_class = LIBOSFP_OS_CLASS_IOS;
|
||||
} else if (0 == strncmp(name, LIBOSFP_OS_CLASS_NAME_ANDROID, strlen(LIBOSFP_OS_CLASS_NAME_ANDROID))) {
|
||||
os_class = LIBOSFP_OS_CLASS_ANDROID;
|
||||
} else {
|
||||
os_class = LIBOSFP_OS_CLASS_MAX;
|
||||
}
|
||||
|
||||
return os_class;
|
||||
}
|
||||
|
||||
const char *libosfp_os_class_id_to_name(libosfp_os_class_id_t os_class)
|
||||
{
|
||||
return os_class_name[os_class];
|
||||
}
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
#ifndef __LIBOSFP_COMMON_H__
|
||||
#define __LIBOSFP_COMMON_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <malloc.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <linux/in.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/ipv6.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "utarray.h"
|
||||
#include "uthash.h"
|
||||
#include "utlist.h"
|
||||
#include "utringbuffer.h"
|
||||
#include "utstack.h"
|
||||
#include "utstring.h"
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
#define LIBOSFP_OS_CLASS_NAME_WINDOWS "Windows"
|
||||
#define LIBOSFP_OS_CLASS_NAME_LINUX "Linux"
|
||||
#define LIBOSFP_OS_CLASS_NAME_MAC_OS "Mac OS"
|
||||
#define LIBOSFP_OS_CLASS_NAME_IOS "iOS"
|
||||
#define LIBOSFP_OS_CLASS_NAME_ANDROID "Android"
|
||||
|
||||
#define LIBOSFP_TCP_OPTLENMAX 64
|
||||
#define LIBOSFP_TCP_OPTMAX 20
|
||||
|
||||
#define LIBOSFP_ETHERNET_HEADER_LEN 14
|
||||
#define LIBOSFP_VLAN_HEADER_LEN 4
|
||||
#define LIBOSFP_IPV4_HEADER_LEN 20
|
||||
#define LIBOSFP_IPV6_HEADER_LEN 40
|
||||
#define LIBOSFP_TCP_HEADER_LEN 20
|
||||
|
||||
|
||||
//# TCP Options (opt_type) - http://www.iana.org/assignments/tcp-parameters
|
||||
#define LIBOSFP_TCP_OPT_EOL 0 //# end of option list
|
||||
#define LIBOSFP_TCP_OPT_NOP 1 //# no operation
|
||||
#define LIBOSFP_TCP_OPT_MSS 2 //# maximum segment size
|
||||
#define LIBOSFP_TCP_OPT_WSCALE 3 //# window scale factor, RFC 1072
|
||||
#define LIBOSFP_TCP_OPT_SACKOK 4 //# SACK permitted, RFC 2018
|
||||
#define LIBOSFP_TCP_OPT_SACK 5 //# SACK, RFC 2018
|
||||
#define LIBOSFP_TCP_OPT_ECHO 6 //# echo (obsolete), RFC 1072
|
||||
#define LIBOSFP_TCP_OPT_ECHOREPLY 7 //# echo reply (obsolete), RFC 1072
|
||||
#define LIBOSFP_TCP_OPT_TIMESTAMP 8 //# timestamps, RFC 1323
|
||||
#define LIBOSFP_TCP_OPT_POCONN 9 //# partial order conn, RFC 1693
|
||||
#define LIBOSFP_TCP_OPT_POSVC 10 //# partial order service, RFC 1693
|
||||
#define LIBOSFP_TCP_OPT_CC 11 //# connection count, RFC 1644
|
||||
#define LIBOSFP_TCP_OPT_CCNEW 12 //# CC.NEW, RFC 1644
|
||||
#define LIBOSFP_TCP_OPT_CCECHO 13 //# CC.ECHO, RFC 1644
|
||||
#define LIBOSFP_TCP_OPT_ALTSUM 14 //# alt checksum request, RFC 1146
|
||||
#define LIBOSFP_TCP_OPT_ALTSUMDATA 15 //# alt checksum data, RFC 1146
|
||||
#define LIBOSFP_TCP_OPT_SKEETER 16 //# Skeeter
|
||||
#define LIBOSFP_TCP_OPT_BUBBA 17 //# Bubba
|
||||
#define LIBOSFP_TCP_OPT_TRAILSUM 18 //# trailer checksum
|
||||
#define LIBOSFP_TCP_OPT_MD5 19 //# MD5 signature, RFC 2385
|
||||
#define LIBOSFP_TCP_OPT_SCPS 20 //# SCPS capabilities
|
||||
#define LIBOSFP_TCP_OPT_SNACK 21 //# selective negative acks
|
||||
#define LIBOSFP_TCP_OPT_REC 22 //# record boundaries
|
||||
#define LIBOSFP_TCP_OPT_CORRUPT 23 //# corruption experienced
|
||||
#define LIBOSFP_TCP_OPT_SNAP 24 //# SNAP
|
||||
#define LIBOSFP_TCP_OPT_TCPCOMP 26 //# TCP compression filter
|
||||
#define LIBOSFP_TCP_OPT_MAX 27 //# Quick-Start Response
|
||||
#define LIBOSFP_TCP_OPT_USRTO 28 //# User Timeout Option (also, other known unauthorized use) [***][1] [RFC5482]
|
||||
#define LIBOSFP_TCP_OPT_AUTH 29 //# TCP Authentication Option (TCP-AO) [RFC5925]
|
||||
#define LIBOSFP_TCP_OPT_MULTIPATH 30 //# Multipath TCP (MPTCP)
|
||||
#define LIBOSFP_TCP_OPT_FASTOPEN 34 //# TCP Fast Open Cookie [RFC7413]
|
||||
#define LIBOSFP_TCP_OPY_ENCNEG 69 //# Encryption Negotiation (TCP-ENO) [RFC8547]
|
||||
#define LIBOSFP_TCP_OPT_EXP1 253 //# RFC3692-style Experiment 1 (also improperly used for shipping products)
|
||||
#define LIBOSFP_TCP_OPT_EXP2 254 //# RFC3692-style Experiment 2 (also improperly used for shipping products)
|
||||
|
||||
#define LIBOSFP_TCP_OPT_SACKOK_LEN 2
|
||||
#define LIBOSFP_TCP_OPT_WS_LEN 3
|
||||
#define LIBOSFP_TCP_OPT_TS_LEN 10
|
||||
#define LIBOSFP_TCP_OPT_MSS_LEN 4
|
||||
#define LIBOSFP_TCP_OPT_SACK_MIN_LEN 10 /* hdr 2, 1 pair 8 = 10 */
|
||||
#define LIBOSFP_TCP_OPT_SACK_MAX_LEN 34 /* hdr 2, 4 pair 32= 34 */
|
||||
#define LIBOSFP_TCP_OPT_TFO_MIN_LEN 4 /* kind, len, 2 bytes cookie: 4 */
|
||||
#define LIBOSFP_TCP_OPT_TFO_MAX_LEN 18 /* kind, len, 18 */
|
||||
|
||||
|
||||
#define LIBOSFP_WRITE_STRING_TO_BUF(ret, buf, size, off, ...) do { \
|
||||
ret = snprintf((char *)buf + off, \
|
||||
size - off, \
|
||||
__VA_ARGS__); \
|
||||
if (ret >= 0) { \
|
||||
if ( (off + ret) >= size) { \
|
||||
off = size - 1; \
|
||||
} else { \
|
||||
off += ret; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static inline unsigned long long libosfp_rdtsc(void)
|
||||
{
|
||||
union {
|
||||
unsigned long long tsc_64;
|
||||
struct {
|
||||
unsigned int lo_32;
|
||||
unsigned int hi_32;
|
||||
};
|
||||
} tsc;
|
||||
|
||||
asm volatile("rdtsc" :
|
||||
"=a" (tsc.lo_32),
|
||||
"=d" (tsc.hi_32));
|
||||
return tsc.tsc_64;
|
||||
}
|
||||
|
||||
#define libosfp_profile_cycle(x) volatile unsigned long long x = 0
|
||||
#define libosfp_profile_get_cycle(x) do { \
|
||||
x = libosfp_rdtsc(); \
|
||||
} while(0)
|
||||
|
||||
#define LIBOSFP_BIT_U32(n) (1UL << (n))
|
||||
|
||||
typedef enum libosfp_error_code {
|
||||
LIBOSFP_NOERR,
|
||||
LIBOSFP_EINVAL,
|
||||
LIBOSFP_ENOMEM,
|
||||
LIBOSFP_ERR_SCORE_DB_READ_FILE,
|
||||
LIBOSFP_ERR_SCORE_DB_PARSE_FILE,
|
||||
LIBOSFP_ERR_SCORE_DB_UNSUPPORTED,
|
||||
|
||||
LIBOSFP_ERR_FINGERPRINTING_UNSUPPORTED,
|
||||
|
||||
} libosfp_error_code_t;
|
||||
|
||||
typedef enum libosfp_os_class_id {
|
||||
LIBOSFP_OS_CLASS_WINDOWS,
|
||||
LIBOSFP_OS_CLASS_LINUX,
|
||||
LIBOSFP_OS_CLASS_MAC_OS,
|
||||
LIBOSFP_OS_CLASS_IOS,
|
||||
LIBOSFP_OS_CLASS_ANDROID,
|
||||
LIBOSFP_OS_CLASS_MAX,
|
||||
} libosfp_os_class_id_t;
|
||||
|
||||
#define LIBOSFP_OS_CLASS_FLAG_WINDOWS LIBOSFP_BIT_U32(LIBOSFP_OS_CLASS_WINDOWS)
|
||||
#define LIBOSFP_OS_CLASS_FLAG_LINUX LIBOSFP_BIT_U32(LIBOSFP_OS_CLASS_LINUX)
|
||||
#define LIBOSFP_OS_CLASS_FLAG_MAC_OS LIBOSFP_BIT_U32(LIBOSFP_OS_CLASS_MAC_OS)
|
||||
#define LIBOSFP_OS_CLASS_FLAG_IOS LIBOSFP_BIT_U32(LIBOSFP_OS_CLASS_IOS)
|
||||
#define LIBOSFP_OS_CLASS_FLAG_ANDROID LIBOSFP_BIT_U32(LIBOSFP_OS_CLASS_ANDROID)
|
||||
|
||||
typedef struct libosfp_score {
|
||||
unsigned int likely_os_class;
|
||||
unsigned int likely_score;
|
||||
unsigned int os_class_score[LIBOSFP_OS_CLASS_MAX];
|
||||
} libosfp_score_t;
|
||||
|
||||
libosfp_os_class_id_t libosfp_os_class_name_to_id(char *name);
|
||||
const char *libosfp_os_class_id_to_name(libosfp_os_class_id_t os_class);
|
||||
|
||||
#endif
|
||||
File diff suppressed because one or more lines are too long
@@ -1,5 +0,0 @@
|
||||
// File generated by gen_c.sh
|
||||
#ifndef _LIBOSFP_DEFAULT_FINGERPRINTS_H__
|
||||
#define _LIBOSFP_DEFAULT_FINGERPRINTS_H__
|
||||
extern const char *g_default_fingerprints;
|
||||
#endif
|
||||
@@ -1,441 +0,0 @@
|
||||
#include "libosfp_common.h"
|
||||
#include "libosfp_fingerprint.h"
|
||||
#include "libosfp_log.h"
|
||||
|
||||
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_IP_ID "ip_id"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_IP_TOS "ip_tos"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_IP_TOTAL_LENGHT "ip_total_length"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_IP_TTL "ip_ttl"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_OFF "tcp_off"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP "tcp_timestamp"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP_ECHO_REPLY "tcp_timestamp_echo_reply"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_WCALING "tcp_window_scaling"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_SIZE "tcp_window_size"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_FLAGS "tcp_flags"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_MSS "tcp_mss"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS "tcp_options"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS_ORDERED "tcp_options_ordered"
|
||||
#define LIBOSFP_FINGERPRINT_FIELD_NAME_OS "os"
|
||||
|
||||
#define LIBOSFP_FINGERPRINT_DEFAULT_OS_CLASS_NAME "LIBOSFP_UNKNOWN"
|
||||
|
||||
typedef struct libosfp_tcp_opt {
|
||||
unsigned char type;
|
||||
unsigned char len;
|
||||
const unsigned char *data;
|
||||
} libosfp_tcp_opt_t;
|
||||
|
||||
libosfp_fingerprint_field_t fp_fields[LIBOSFP_FIELD_MAX] = {
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_IP_ID, 1, LIBOSFP_FIELD_TYPE_UINT, 150, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_IP_TOS, 1, LIBOSFP_FIELD_TYPE_UINT, 25, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_IP_TOTAL_LENGHT, 1, LIBOSFP_FIELD_TYPE_UINT, 250, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_IP_TTL, 1, LIBOSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_OFF, 1, LIBOSFP_FIELD_TYPE_UINT, 250, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP, 0, LIBOSFP_FIELD_TYPE_UINT, 0, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP_ECHO_REPLY,1, LIBOSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_WCALING, 1, LIBOSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_SIZE, 1, LIBOSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_FLAGS, 1, LIBOSFP_FIELD_TYPE_UINT, 25, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_MSS, 1, LIBOSFP_FIELD_TYPE_UINT, 150, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS, 1, LIBOSFP_FIELD_TYPE_STRING, 400, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS_ORDERED, 1, LIBOSFP_FIELD_TYPE_STRING, 250, NULL, 0},
|
||||
{LIBOSFP_FINGERPRINT_FIELD_NAME_OS, 0, LIBOSFP_FIELD_TYPE_STRING, 0, NULL, 0},
|
||||
};
|
||||
|
||||
static char option_to_ascii(unsigned char type)
|
||||
{
|
||||
switch (type) {
|
||||
case LIBOSFP_TCP_OPT_EOL:
|
||||
return 'E';
|
||||
case LIBOSFP_TCP_OPT_NOP:
|
||||
return 'N';
|
||||
case LIBOSFP_TCP_OPT_MSS:
|
||||
return 'M';
|
||||
case LIBOSFP_TCP_OPT_WSCALE:
|
||||
return 'W';
|
||||
case LIBOSFP_TCP_OPT_SACKOK:
|
||||
return 'S';
|
||||
case LIBOSFP_TCP_OPT_SACK:
|
||||
return 'K';
|
||||
case LIBOSFP_TCP_OPT_ECHO:
|
||||
return 'J';
|
||||
case LIBOSFP_TCP_OPT_ECHOREPLY:
|
||||
return 'F';
|
||||
case LIBOSFP_TCP_OPT_TIMESTAMP:
|
||||
return 'T';
|
||||
case LIBOSFP_TCP_OPT_POCONN:
|
||||
return 'P';
|
||||
case LIBOSFP_TCP_OPT_POSVC:
|
||||
return 'R';
|
||||
default:
|
||||
return 'U';
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int compute_ip_ttl(unsigned int ip_ttl)
|
||||
{
|
||||
if (ip_ttl >= 0 && ip_ttl <= 32) {
|
||||
ip_ttl = 32;
|
||||
} else if (ip_ttl > 32 && ip_ttl <= 64) {
|
||||
ip_ttl = 64;
|
||||
} else if (ip_ttl > 64 && ip_ttl <= 128) {
|
||||
ip_ttl = 128;
|
||||
} else {
|
||||
ip_ttl = 255;
|
||||
}
|
||||
return ip_ttl;
|
||||
}
|
||||
|
||||
static unsigned int decode_tcp_options(libosfp_tcp_opt_t *tcp_opts, unsigned int max_opt_cnt, unsigned char *data, unsigned int len)
|
||||
{
|
||||
unsigned int offset = 0;
|
||||
unsigned int tcp_opt_cnt = 0;
|
||||
|
||||
unsigned char type;
|
||||
unsigned char olen;
|
||||
unsigned char *odata;
|
||||
|
||||
while (offset < len && tcp_opt_cnt < max_opt_cnt) {
|
||||
type = *(data + offset);
|
||||
|
||||
if (type == LIBOSFP_TCP_OPT_EOL || type == LIBOSFP_TCP_OPT_NOP) {
|
||||
olen = 1;
|
||||
} else {
|
||||
olen = *(data + offset + 1);
|
||||
if (olen < 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset + olen > len) {
|
||||
break;
|
||||
}
|
||||
|
||||
odata = (olen > 2) ? (data + offset + 2) : NULL;
|
||||
|
||||
tcp_opts[tcp_opt_cnt].type = type;
|
||||
tcp_opts[tcp_opt_cnt].len = olen;
|
||||
tcp_opts[tcp_opt_cnt].data = odata;
|
||||
|
||||
offset += olen;
|
||||
tcp_opt_cnt++;
|
||||
}
|
||||
return tcp_opt_cnt;
|
||||
}
|
||||
|
||||
int libosfp_fingerprint_to_json_buf(libosfp_fingerprint_t *fp, char *strbuf, unsigned int buf_len, unsigned int format)
|
||||
{
|
||||
int rlen = 0, ret, i;
|
||||
cJSON *root;
|
||||
|
||||
if (fp == NULL || strbuf == NULL || buf_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_FIELD_MAX; i++) {
|
||||
if (fp->fields[i].enabled) {
|
||||
switch (fp_fields[i].type) {
|
||||
case LIBOSFP_FIELD_TYPE_UINT:
|
||||
cJSON_AddNumberToObject(root, fp_fields[i].name, *(unsigned int *)fp->fields[i].value);
|
||||
break;
|
||||
case LIBOSFP_FIELD_TYPE_STRING:
|
||||
cJSON_AddStringToObject(root, fp_fields[i].name, (char *)fp->fields[i].value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
cJSON_AddNullToObject(root, fp_fields[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cJSON_PrintPreallocated(root, strbuf, buf_len, format)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return strlen(strbuf) + 1;
|
||||
}
|
||||
|
||||
unsigned int libosfp_fingerprint_get_field_enabled(libosfp_field_id_t field_id)
|
||||
{
|
||||
return fp_fields[field_id].enabled;
|
||||
}
|
||||
|
||||
unsigned int libosfp_fingerprint_get_field_importance(libosfp_field_id_t field_id)
|
||||
{
|
||||
return fp_fields[field_id].importance;
|
||||
}
|
||||
|
||||
char *libosfp_fingerprint_get_field_name(libosfp_field_id_t field_id)
|
||||
{
|
||||
return fp_fields[field_id].name;
|
||||
}
|
||||
|
||||
unsigned int libosfp_fingerprint_get_field_type(libosfp_field_id_t field_id)
|
||||
{
|
||||
return fp_fields[field_id].type;
|
||||
}
|
||||
|
||||
void libosfp_fingerprint_setup_field(libosfp_fingerprint_t *fp, libosfp_field_id_t field_id, void *value, unsigned int len)
|
||||
{
|
||||
fp->fields[field_id].name = libosfp_fingerprint_get_field_name(field_id);
|
||||
fp->fields[field_id].enabled = 1;
|
||||
|
||||
if (fp->value_buffer_used + len <= sizeof(fp->value_buffer)) {
|
||||
memcpy(fp->value_buffer + fp->value_buffer_used, value, len);
|
||||
fp->fields[field_id].value = fp->value_buffer + fp->value_buffer_used;
|
||||
fp->fields[field_id].value_len = len;
|
||||
fp->value_buffer_used += len;
|
||||
} else {
|
||||
fp->fields[field_id].value = NULL;
|
||||
fp->fields[field_id].value_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void libosfp_fingerprinting_tcp_option(unsigned char *opt_data, unsigned int opt_len, libosfp_fingerprint_t *fp)
|
||||
{
|
||||
int ret,i;
|
||||
|
||||
unsigned int tcp_mss;
|
||||
unsigned int tcp_ws;
|
||||
unsigned int tcp_ter;
|
||||
unsigned int tcp_opt_cnt;
|
||||
libosfp_tcp_opt_t tcp_opts[LIBOSFP_TCP_OPTMAX];
|
||||
|
||||
char options[LIBOSFP_TCP_OPTLENMAX];
|
||||
char options_ordered[LIBOSFP_TCP_OPTLENMAX];
|
||||
unsigned int offset = 0;
|
||||
unsigned int maxoffset = sizeof(options) - 3; //for shortest "E,"
|
||||
unsigned int ordered_offset = 0;
|
||||
unsigned int ordered_maxoffset = sizeof(options_ordered) - 1;
|
||||
|
||||
tcp_opt_cnt = decode_tcp_options(tcp_opts, LIBOSFP_TCP_OPTMAX, opt_data, opt_len);
|
||||
|
||||
for (i = 0; i < tcp_opt_cnt && offset < maxoffset && ordered_offset < ordered_maxoffset; i++) {
|
||||
libosfp_tcp_opt_t *opt = &tcp_opts[i];
|
||||
|
||||
char letter = option_to_ascii(opt->type);
|
||||
options[offset++] = letter;
|
||||
options_ordered[ordered_offset++] = letter;
|
||||
|
||||
switch (opt->type) {
|
||||
case LIBOSFP_TCP_OPT_EOL:
|
||||
case LIBOSFP_TCP_OPT_NOP:
|
||||
break;
|
||||
case LIBOSFP_TCP_OPT_MSS:
|
||||
if (opt->len != LIBOSFP_TCP_OPT_MSS_LEN) {
|
||||
break;
|
||||
}
|
||||
tcp_mss = (unsigned int)ntohs(*(unsigned short *)opt->data);
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_MSS, &tcp_mss, sizeof(tcp_mss));
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", tcp_mss);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
break;
|
||||
case LIBOSFP_TCP_OPT_WSCALE:
|
||||
if (opt->len != LIBOSFP_TCP_OPT_WS_LEN) {
|
||||
break;
|
||||
}
|
||||
tcp_ws = (unsigned int)*(unsigned char *)opt->data;
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_WINDOW_SCALING, &tcp_ws, sizeof(tcp_ws));
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", tcp_ws);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
break;
|
||||
case LIBOSFP_TCP_OPT_TIMESTAMP:
|
||||
if (opt->len != LIBOSFP_TCP_OPT_TS_LEN) {
|
||||
break;
|
||||
}
|
||||
tcp_ter = ntohl(*(unsigned int *)(opt->data + 4));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_TIMESTAMP_ECHO_REPLY, &tcp_ter, sizeof(tcp_ter));
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", tcp_ter);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
break;
|
||||
case LIBOSFP_TCP_OPT_SACKOK:
|
||||
case LIBOSFP_TCP_OPT_SACK:
|
||||
case LIBOSFP_TCP_OPT_ECHO:
|
||||
case LIBOSFP_TCP_OPT_ECHOREPLY:
|
||||
case LIBOSFP_TCP_OPT_POCONN:
|
||||
case LIBOSFP_TCP_OPT_POSVC:
|
||||
break;
|
||||
default:
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", opt->type);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
}
|
||||
|
||||
options[offset++] = ',';
|
||||
options[offset] = 0;
|
||||
options_ordered[ordered_offset] = 0;
|
||||
}
|
||||
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_OPTIONS, options, strlen(options) + 1);
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_OPTIONS_ORDERED, options_ordered, strlen(options_ordered) + 1);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int libosfp_fingerprinting_tcp(struct tcphdr *tcph, libosfp_fingerprint_t *fp)
|
||||
{
|
||||
if (tcph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
unsigned int tcp_off = tcph->doff << 2;
|
||||
unsigned int tcp_window_size = ntohs(tcph->window);
|
||||
unsigned int tcp_flags = *((unsigned char *)&tcph->window - 1);
|
||||
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_OFF, &tcp_off, sizeof(tcp_off));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_WINDOW_SIZE, &tcp_window_size, sizeof(tcp_window_size));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_TCP_FLAGS, &tcp_flags, sizeof(tcp_flags));
|
||||
|
||||
// tcp options
|
||||
if (tcp_off > LIBOSFP_TCP_HEADER_LEN) {
|
||||
libosfp_fingerprinting_tcp_option((unsigned char *)tcph + LIBOSFP_TCP_HEADER_LEN, tcp_off - LIBOSFP_TCP_HEADER_LEN, fp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int libosfp_fingerprinting_ipv4(struct iphdr *iph, libosfp_fingerprint_t *fp)
|
||||
{
|
||||
if (iph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
unsigned int ip_id = !!iph->id;
|
||||
unsigned int ip_tos = iph->tos;
|
||||
unsigned int ip_total_length = ntohs(iph->tot_len);
|
||||
unsigned int ip_ttl = compute_ip_ttl(iph->ttl);
|
||||
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_ID, &ip_id, sizeof(ip_id));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_TOS, &ip_tos, sizeof(ip_tos));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_TOTAL_LENGTH, &ip_total_length, sizeof(ip_total_length));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_TTL, &ip_ttl, sizeof(ip_ttl));
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int libosfp_fingerprinting_ipv6(struct ipv6hdr *iph, libosfp_fingerprint_t *fp)
|
||||
{
|
||||
if (iph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
//unsigned int ip_id = 0;
|
||||
//unsigned int ip_tos = 0;
|
||||
unsigned int ip_total_length = LIBOSFP_IPV6_HEADER_LEN + ntohs(iph->payload_len);
|
||||
unsigned int ip_ttl = compute_ip_ttl(iph->hop_limit);
|
||||
|
||||
//libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_ID, &ip_id, sizeof(ip_id));
|
||||
//libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_TOS, &ip_tos, sizeof(ip_tos));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_TOTAL_LENGTH, &ip_total_length, sizeof(ip_total_length));
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_IP_TTL, &ip_ttl, sizeof(ip_ttl));
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int libosfp_fingerprinting(unsigned char *iph, unsigned char *tcph, libosfp_fingerprint_t *fp)
|
||||
{
|
||||
int ret = LIBOSFP_EINVAL;
|
||||
int ip_version;
|
||||
|
||||
if (iph == NULL || tcph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memset(fp, 0, sizeof(libosfp_fingerprint_t));
|
||||
|
||||
ip_version = ((iph)[0] & 0xf0) >> 4;
|
||||
|
||||
switch (ip_version) {
|
||||
case 4:
|
||||
ret = libosfp_fingerprinting_ipv4((struct iphdr *)iph, fp);
|
||||
break;
|
||||
case 6:
|
||||
ret = libosfp_fingerprinting_ipv6((struct ipv6hdr *)iph, fp);
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = libosfp_fingerprinting_tcp((struct tcphdr *)tcph, fp);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
libosfp_fingerprint_setup_field(fp, LIBOSFP_FIELD_OS, LIBOSFP_FINGERPRINT_DEFAULT_OS_CLASS_NAME, strlen(LIBOSFP_FINGERPRINT_DEFAULT_OS_CLASS_NAME) + 1);
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef UNITTEST
|
||||
int test_libosfp_fingerprinting(void)
|
||||
{
|
||||
int ret;
|
||||
char iph[] = {
|
||||
0x45, 0x00, 0x00, 0x34, 0x51, 0xc4, 0x40, 0x00,
|
||||
0x80, 0x06, 0xe7, 0x27, 0xc0, 0xa8, 0x73, 0x08,
|
||||
0x6a, 0xb9, 0x23, 0x6e
|
||||
};
|
||||
|
||||
char tcph[] = {
|
||||
0xc1, 0xbd, 0x00, 0x50, 0x3d, 0x58, 0x51, 0x60,
|
||||
0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x20, 0x00,
|
||||
0x3d, 0x3a, 0x00, 0x00, 0x02, 0x04, 0x04, 0xec,
|
||||
0x01, 0x03, 0x03, 0x08, 0x01, 0x01, 0x04, 0x02
|
||||
};
|
||||
|
||||
char str_buf[2048] = "";
|
||||
const char *target_buf = "{\"ip_id\":1,\"ip_tos\":0,\"ip_total_length\":52,\"ip_ttl\":128,\"tcp_off\":32,\"tcp_timestamp\":null,\"tcp_timestamp_echo_reply\":null,\"tcp_window_scaling\":8,\"tcp_window_size\":8192,\"tcp_flags\":2,\"tcp_mss\":1260,\"tcp_options\":\"M1260,N,W8,N,N,S,\",\"tcp_options_ordered\":\"MNWNNS\",\"os\":\"LIBOSFP_UNKNOWN\"}";
|
||||
libosfp_fingerprint_t fp = {0};
|
||||
|
||||
ret = libosfp_fingerprinting(iph, tcph, &fp);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = libosfp_fingerprint_to_json_buf(&fp, str_buf, 2048, 0);
|
||||
if (ret <= 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (0 != memcmp(str_buf, target_buf, strlen(target_buf))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
@@ -1,63 +0,0 @@
|
||||
#ifndef __LIBOSFP_FINGERPRINT_H__
|
||||
#define __LIBOSFP_FINGERPRINT_H__
|
||||
|
||||
#define LIBOSFP_FINGERPRINT_VALUE_BUFFER_MAX 128
|
||||
|
||||
typedef enum libosfp_field_id {
|
||||
LIBOSFP_FIELD_IP_ID,
|
||||
LIBOSFP_FIELD_IP_TOS,
|
||||
LIBOSFP_FIELD_IP_TOTAL_LENGTH,
|
||||
LIBOSFP_FIELD_IP_TTL,
|
||||
LIBOSFP_FIELD_TCP_OFF,
|
||||
LIBOSFP_FIELD_TCP_TIMESTAMP,
|
||||
LIBOSFP_FIELD_TCP_TIMESTAMP_ECHO_REPLY,
|
||||
LIBOSFP_FIELD_TCP_WINDOW_SCALING,
|
||||
LIBOSFP_FIELD_TCP_WINDOW_SIZE,
|
||||
LIBOSFP_FIELD_TCP_FLAGS,
|
||||
LIBOSFP_FIELD_TCP_MSS,
|
||||
LIBOSFP_FIELD_TCP_OPTIONS,
|
||||
LIBOSFP_FIELD_TCP_OPTIONS_ORDERED,
|
||||
LIBOSFP_FIELD_OS,
|
||||
LIBOSFP_FIELD_MAX,
|
||||
} libosfp_field_id_t;
|
||||
|
||||
typedef enum libosfp_field_type {
|
||||
LIBOSFP_FIELD_TYPE_UNKNOWN,
|
||||
LIBOSFP_FIELD_TYPE_UINT,
|
||||
LIBOSFP_FIELD_TYPE_STRING,
|
||||
LIBOSFP_FIELD_TYPE_MAX
|
||||
} libosfp_field_type_t;
|
||||
|
||||
typedef struct libosfp_fingerprint_field {
|
||||
char *name;
|
||||
unsigned int enabled;
|
||||
unsigned int type;
|
||||
unsigned int importance;
|
||||
void *value;
|
||||
unsigned int value_len;
|
||||
} libosfp_fingerprint_field_t;
|
||||
|
||||
typedef struct libosfp_fingerprint {
|
||||
libosfp_fingerprint_field_t fields[LIBOSFP_FIELD_MAX];
|
||||
char value_buffer[LIBOSFP_FINGERPRINT_VALUE_BUFFER_MAX];
|
||||
unsigned int value_buffer_used;
|
||||
} libosfp_fingerprint_t;
|
||||
|
||||
|
||||
char *libosfp_fingerprint_get_field_name(libosfp_field_id_t field_id);
|
||||
unsigned int libosfp_fingerprint_get_field_enabled(libosfp_field_id_t field_id);
|
||||
unsigned int libosfp_fingerprint_get_field_importance(libosfp_field_id_t field_id);
|
||||
unsigned int libosfp_fingerprint_get_field_type(libosfp_field_id_t field_id);
|
||||
int libosfp_fingerprint_to_json_buf(libosfp_fingerprint_t *fp, char *strbuf, unsigned int buf_len, unsigned int format);
|
||||
void libosfp_fingerprint_setup_field(libosfp_fingerprint_t *fp, libosfp_field_id_t field_id, void *value, unsigned int len);
|
||||
|
||||
void libosfp_fingerprinting_tcp_option(unsigned char *pkt, unsigned int pktlen, libosfp_fingerprint_t *fp);
|
||||
int libosfp_fingerprinting_tcp(struct tcphdr *tcph, libosfp_fingerprint_t *fp);
|
||||
int libosfp_fingerprinting_ipv4(struct iphdr *iph, libosfp_fingerprint_t *fp);
|
||||
int libosfp_fingerprinting_ipv6(struct ipv6hdr *iph, libosfp_fingerprint_t *fp);
|
||||
int libosfp_fingerprinting(unsigned char *iphdr, unsigned char *tcphdr, libosfp_fingerprint_t *fp);
|
||||
|
||||
#ifdef UNITTEST
|
||||
int test_libosfp_fingerprinting(void);
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef __LIBOSFP_LOG_H__
|
||||
#define __LIBOSFP_LOG_H__
|
||||
|
||||
typedef enum libosfp_log_level {
|
||||
LIBOSFP_LOG_LEVEL_DEBUG,
|
||||
LIBOSFP_LOG_LEVEL_INFO,
|
||||
LIBOSFP_LOG_LEVEL_WARNING,
|
||||
LIBOSFP_LOG_LEVEL_ERROR
|
||||
} libosfp_log_level_t;
|
||||
|
||||
#ifndef DEBUG
|
||||
#define libosfp_log_debug(...) do { } while (0)
|
||||
#else
|
||||
#define libosfp_log_debug(...) libosfp_log(LIBOSFP_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#endif
|
||||
#define libosfp_log_info(...) libosfp_log(LIBOSFP_LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define libosfp_log_warning(...) libosfp_log(LIBOSFP_LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define libosfp_log_error(...) libosfp_log(LIBOSFP_LOG_LEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__,__VA_ARGS__)
|
||||
|
||||
|
||||
void libosfp_log_level_set(libosfp_log_level_t level);
|
||||
void libosfp_log(unsigned int x, const char *file, const int line, const char *func, const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
@@ -1,575 +0,0 @@
|
||||
#include "libosfp_common.h"
|
||||
|
||||
#include "libosfp_fingerprint.h"
|
||||
#include "libosfp_default_fingerprints.h"
|
||||
#include "libosfp_score_db.h"
|
||||
#include "libosfp_log.h"
|
||||
|
||||
#define LIBOSFP_PERCENTILE 100
|
||||
|
||||
#define LIBOSFP_SCORE_DB_FIELD_UINT_VALUE_MAX 65536
|
||||
|
||||
typedef struct libosfp_score_db_array_data {
|
||||
libosfp_score_t *array_head;
|
||||
unsigned int array_len;
|
||||
} libosfp_score_db_array_data_t;
|
||||
|
||||
typedef struct libosfp_score_db_hash_element {
|
||||
char *key;
|
||||
unsigned int keylen;
|
||||
libosfp_score_t *score;
|
||||
UT_hash_handle hh;
|
||||
} libosfp_score_db_hash_element_t;
|
||||
|
||||
typedef struct libosfp_score_db_hash_data {
|
||||
libosfp_score_db_hash_element_t *hash_head;
|
||||
} libosfp_score_db_hash_data_t;
|
||||
|
||||
|
||||
int libosfp_score_db_array_add(void *data, libosfp_score_t *score, void *value, unsigned int len)
|
||||
{
|
||||
int ret = -1, i;
|
||||
unsigned int index;
|
||||
libosfp_score_db_array_data_t *array_data = (libosfp_score_db_array_data_t *)data;
|
||||
|
||||
if (array_data == NULL || score == NULL || value == NULL || len != sizeof(unsigned int)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (array_data->array_head == NULL || array_data->array_len == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
index = *(unsigned int *)value;
|
||||
|
||||
if (index >= array_data->array_len) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_OS_CLASS_MAX; i++) {
|
||||
array_data->array_head[index].os_class_score[i] += score->os_class_score[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
libosfp_score_t *libosfp_score_db_array_match(void *data, void *value, unsigned int len)
|
||||
{
|
||||
unsigned int index;
|
||||
libosfp_score_db_array_data_t *array_data = (libosfp_score_db_array_data_t *)data;
|
||||
|
||||
if (array_data == NULL || value == NULL || len != sizeof(unsigned int)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (array_data->array_head == NULL || array_data->array_len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
index = *(unsigned int *)value;
|
||||
|
||||
if (index >= array_data->array_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &((array_data->array_head)[index]);
|
||||
}
|
||||
|
||||
void *libosfp_score_db_array_create(void)
|
||||
{
|
||||
libosfp_score_db_array_data_t *array_data = calloc(1, sizeof(libosfp_score_db_array_data_t));
|
||||
if (array_data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
array_data->array_head = calloc(LIBOSFP_SCORE_DB_FIELD_UINT_VALUE_MAX, sizeof(libosfp_score_t));
|
||||
if (array_data->array_head == NULL) {
|
||||
free(array_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
array_data->array_len = LIBOSFP_SCORE_DB_FIELD_UINT_VALUE_MAX;
|
||||
|
||||
return (void *)array_data;
|
||||
}
|
||||
|
||||
void libosfp_score_db_array_destroy(void *data) {
|
||||
libosfp_score_db_array_data_t *array_data = (libosfp_score_db_array_data_t *)data;
|
||||
|
||||
if (array_data) {
|
||||
if (array_data->array_head) {
|
||||
free(array_data->array_head);
|
||||
}
|
||||
free(array_data);
|
||||
}
|
||||
}
|
||||
|
||||
int libosfp_score_db_hash_add(void *data, libosfp_score_t *score, void *value, unsigned int len)
|
||||
{
|
||||
int ret = -1, i;
|
||||
libosfp_score_db_hash_data_t *hash_data = (libosfp_score_db_hash_data_t *)data;
|
||||
libosfp_score_db_hash_element_t *element = NULL;
|
||||
|
||||
if (hash_data == NULL || score == NULL || value == NULL || len == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
HASH_FIND(hh, hash_data->hash_head, value, len, element);
|
||||
if (element == NULL) {
|
||||
element = (libosfp_score_db_hash_element_t *)calloc(1, sizeof(libosfp_score_db_hash_element_t));
|
||||
if (element == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
element->key = strdup(value);
|
||||
element->keylen = len;
|
||||
element->score = (libosfp_score_t *)calloc(1, sizeof(libosfp_score_t));
|
||||
if (element->score == NULL) {
|
||||
free(element);
|
||||
element = NULL;
|
||||
goto exit;
|
||||
}
|
||||
HASH_ADD_KEYPTR(hh, hash_data->hash_head, element->key, element->keylen, element);
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_OS_CLASS_MAX; i++) {
|
||||
element->score->os_class_score[i] += score->os_class_score[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
libosfp_score_t *libosfp_score_db_hash_match(void *data, void *value, unsigned int len)
|
||||
{
|
||||
libosfp_score_db_hash_data_t *hash_data = (libosfp_score_db_hash_data_t *)data;
|
||||
libosfp_score_db_hash_element_t *element = NULL;
|
||||
|
||||
if (data == NULL || value == NULL || len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (hash_data->hash_head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HASH_FIND(hh, hash_data->hash_head, value, len, element);
|
||||
if (element == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return element->score;
|
||||
}
|
||||
|
||||
void *libosfp_score_db_hash_create(void)
|
||||
{
|
||||
return (void*)calloc(1, sizeof(libosfp_score_db_hash_data_t));
|
||||
}
|
||||
|
||||
void libosfp_score_db_hash_destroy(void *data) {
|
||||
libosfp_score_db_hash_data_t *hash_data = (libosfp_score_db_hash_data_t *)data;
|
||||
libosfp_score_db_hash_element_t *element = NULL;
|
||||
libosfp_score_db_hash_element_t *tmp = NULL;
|
||||
|
||||
if (hash_data) {
|
||||
if (hash_data->hash_head) {
|
||||
HASH_ITER(hh, hash_data->hash_head, element, tmp) {
|
||||
HASH_DELETE(hh, hash_data->hash_head, element);
|
||||
if (element) {
|
||||
if (element->key) {
|
||||
free(element->key);
|
||||
}
|
||||
if (element->score) {
|
||||
free(element->score);
|
||||
}
|
||||
free(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(hash_data);
|
||||
}
|
||||
}
|
||||
|
||||
libosfp_score_t *libosfp_score_db_filed_match(libosfp_field_score_db_t *db, void *value, unsigned int len)
|
||||
{
|
||||
if (db == NULL || value == NULL || len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return db->match(db->data, value, len);
|
||||
}
|
||||
|
||||
char *libosfp_score_db_read_file(char *fp_file)
|
||||
{
|
||||
int ret = -1;
|
||||
char *file_buffer = NULL;
|
||||
unsigned int file_len = 0;
|
||||
FILE *fp = NULL;
|
||||
struct stat st;
|
||||
|
||||
if (0 > stat(fp_file, &st)) {
|
||||
printf("stat() on '%s' failed.\n", fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (st.st_size == 0) {
|
||||
printf("Empty file: %s.\n", fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
file_len = (unsigned int)st.st_size;
|
||||
file_buffer = malloc(file_len);
|
||||
if (file_buffer == NULL) {
|
||||
printf("Not enough memory for file buffer. file name: %s\n",fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fp = fopen(fp_file, "r");
|
||||
if (fp == NULL) {
|
||||
printf("Cannot open '%s' for reading.\n", fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = fread(file_buffer, 1, file_len, fp);
|
||||
if (ret < 0) {
|
||||
free(file_buffer);
|
||||
fclose(fp);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return file_buffer;
|
||||
exit:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int libosfp_score_db_load_field(libosfp_field_score_db_t *db, cJSON *field, libosfp_os_class_id_t os_class)
|
||||
{
|
||||
int ret = -1;
|
||||
libosfp_score_t score = {0};
|
||||
|
||||
void *value_ptr;
|
||||
unsigned int value_len;
|
||||
|
||||
switch (field->type) {
|
||||
case cJSON_Number:
|
||||
value_ptr = (void *)&field->valueint;
|
||||
value_len = sizeof(field->valueint);
|
||||
break;
|
||||
case cJSON_String:
|
||||
value_ptr = (void *)field->valuestring;
|
||||
value_len = strlen(field->valuestring) + 1;
|
||||
break;
|
||||
case cJSON_NULL:
|
||||
ret = 0;
|
||||
goto exit;
|
||||
default:
|
||||
goto exit;
|
||||
}
|
||||
|
||||
score.os_class_score[os_class] = 1;
|
||||
|
||||
ret = db->add(db->data, &score, value_ptr, value_len);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
db->entry_count++;
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int libosfp_score_db_load_entry(libosfp_score_db_t *score_db, cJSON *entry)
|
||||
{
|
||||
int ret = -1, i;
|
||||
cJSON *field = NULL;
|
||||
|
||||
if (score_db == NULL || entry == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
field = cJSON_GetObjectItem(entry, libosfp_fingerprint_get_field_name(LIBOSFP_FIELD_OS));
|
||||
if (field == NULL || field->valuestring == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
libosfp_os_class_id_t os_class = libosfp_os_class_name_to_id(field->valuestring);
|
||||
if (os_class >= LIBOSFP_OS_CLASS_MAX) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_FIELD_OS; i++) {
|
||||
libosfp_field_score_db_t *db = &score_db->field_score_dbs[i];
|
||||
if (db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (!db->enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
field = cJSON_GetObjectItem(entry, libosfp_fingerprint_get_field_name(i));
|
||||
if (field == NULL) {
|
||||
printf("json entry missing field: %s\n%s\n",
|
||||
libosfp_fingerprint_get_field_name(i), cJSON_Print(entry));
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = libosfp_score_db_load_field(db, field, os_class);
|
||||
if (ret != 0) {
|
||||
printf("json entry field load failed. field: %s\n%s\n",
|
||||
libosfp_fingerprint_get_field_name(i), cJSON_Print(entry));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
score_db->entry_count++;
|
||||
score_db->os_class_entry_count[os_class]++;
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int libosfp_score_db_load(libosfp_score_db_t *score_db, char *fp_file)
|
||||
{
|
||||
int ret = LIBOSFP_EINVAL, i;
|
||||
const char *file_buffer;
|
||||
libosfp_field_score_db_t *field_score_db;
|
||||
|
||||
cJSON *root = NULL;
|
||||
cJSON *entry = NULL;
|
||||
|
||||
if (score_db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (fp_file == NULL) {
|
||||
file_buffer = g_default_fingerprints;
|
||||
} else {
|
||||
file_buffer = (const char *)libosfp_score_db_read_file(fp_file);
|
||||
if (file_buffer == NULL) {
|
||||
ret = LIBOSFP_ERR_SCORE_DB_READ_FILE;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
root = cJSON_Parse(file_buffer);
|
||||
|
||||
if (file_buffer != g_default_fingerprints) {
|
||||
free((char*)file_buffer);
|
||||
}
|
||||
|
||||
if (root == NULL) {
|
||||
ret = LIBOSFP_ERR_SCORE_DB_PARSE_FILE;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
score_db->entry_count = cJSON_GetArraySize(root);
|
||||
|
||||
for (i = 0; i < LIBOSFP_FIELD_MAX; i++) {
|
||||
field_score_db = &score_db->field_score_dbs[i];
|
||||
if (field_score_db->enabled) {
|
||||
score_db->perfect_score += libosfp_fingerprint_get_field_importance(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < score_db->entry_count; i++) {
|
||||
entry = cJSON_GetArrayItem(root, i);
|
||||
if (entry) {
|
||||
ret = libosfp_score_db_load_entry(score_db, entry);
|
||||
if (ret != 0) {
|
||||
libosfp_log_debug("json entry load failed.\n%s\n", cJSON_Print(entry));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return LIBOSFP_NOERR;
|
||||
exit:
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int libosfp_score_db_score(libosfp_score_db_t *score_db, unsigned int flags, libosfp_fingerprint_t *fp, libosfp_score_t *result_score)
|
||||
{
|
||||
int ret = LIBOSFP_EINVAL, i, j;
|
||||
unsigned int os_class_score;
|
||||
unsigned int likely_score;
|
||||
unsigned int perfect_score;
|
||||
unsigned int entry_count;
|
||||
unsigned int importance;
|
||||
libosfp_score_t *matched_score;
|
||||
libosfp_os_class_id_t likely_os_class;
|
||||
libosfp_fingerprint_field_t *field;
|
||||
libosfp_field_score_db_t *field_score_db;
|
||||
|
||||
if (score_db == NULL || fp == NULL || result_score == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memset(result_score, 0, sizeof(libosfp_score_t));
|
||||
|
||||
perfect_score = score_db->perfect_score;
|
||||
if (perfect_score == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_FIELD_MAX; i++) {
|
||||
field_score_db = &score_db->field_score_dbs[i];
|
||||
if (!field_score_db->enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
field = &fp->fields[i];
|
||||
if (!field->enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
matched_score = libosfp_score_db_filed_match(field_score_db, field->value, field->value_len);
|
||||
if (matched_score == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
importance = libosfp_fingerprint_get_field_importance(i);
|
||||
|
||||
for (j = 0; j < LIBOSFP_OS_CLASS_MAX; j++) {
|
||||
if (0 == flags || flags & LIBOSFP_BIT_U32(j)) {
|
||||
result_score->os_class_score[j] += matched_score->os_class_score[j] * importance / perfect_score;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == LIBOSFP_FIELD_TCP_OPTIONS) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
likely_score = result_score->likely_score;
|
||||
likely_os_class = result_score->likely_os_class;
|
||||
|
||||
for (i = 0; i < LIBOSFP_OS_CLASS_MAX; i++) {
|
||||
entry_count = score_db->os_class_entry_count[i];
|
||||
os_class_score = result_score->os_class_score[i];
|
||||
|
||||
if (entry_count == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// calc percentile score
|
||||
os_class_score = (LIBOSFP_PERCENTILE * os_class_score) / entry_count;
|
||||
|
||||
// calc likely os class and score
|
||||
if (likely_score < os_class_score) {
|
||||
likely_score = os_class_score;
|
||||
likely_os_class = i;
|
||||
}
|
||||
|
||||
result_score->os_class_score[i] = os_class_score;
|
||||
}
|
||||
|
||||
result_score->likely_score = likely_score;
|
||||
result_score->likely_os_class = likely_os_class;
|
||||
|
||||
return LIBOSFP_NOERR;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
void libosfp_score_db_debug_print(libosfp_score_db_t *score_db)
|
||||
{
|
||||
int i;
|
||||
printf("score_db:\n");
|
||||
printf("entry_count: %u\n", score_db->entry_count);
|
||||
printf("perfect_score: %u\n", score_db->perfect_score);
|
||||
|
||||
for (i = 0; i < LIBOSFP_OS_CLASS_MAX; i++) {
|
||||
const char *name = libosfp_os_class_id_to_name(i);
|
||||
printf("os class %s ", name);
|
||||
|
||||
printf("entry_count: %u\n", score_db->os_class_entry_count[i]);
|
||||
printf("os class %s entry_count: %u\n", libosfp_os_class_id_to_name(i), score_db->os_class_entry_count[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_FIELD_MAX; i++) {
|
||||
printf("field %s enabled: %u\n", libosfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].enabled);
|
||||
printf("field %s type: %u\n", libosfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].type);
|
||||
printf("field %s entry_count: %u\n", libosfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].entry_count);
|
||||
printf("field %s enabled: %p\n", libosfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].data);
|
||||
}
|
||||
}
|
||||
|
||||
libosfp_score_db_t *libosfp_score_db_create(void)
|
||||
{
|
||||
int i;
|
||||
libosfp_score_db_t *score_db;
|
||||
libosfp_field_score_db_t *db;
|
||||
|
||||
score_db = calloc(1, sizeof(libosfp_score_db_t));
|
||||
if (score_db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < LIBOSFP_FIELD_MAX; i++) {
|
||||
db = &score_db->field_score_dbs[i];
|
||||
|
||||
db->enabled = libosfp_fingerprint_get_field_enabled(i);
|
||||
if (!db->enabled) {
|
||||
libosfp_log_warning("field disabled: %s", "");
|
||||
continue;
|
||||
}
|
||||
|
||||
db->type = libosfp_fingerprint_get_field_type(i);
|
||||
switch (db->type) {
|
||||
case LIBOSFP_FIELD_TYPE_UINT:
|
||||
db->create = libosfp_score_db_array_create;
|
||||
db->destroy = libosfp_score_db_array_destroy;
|
||||
db->add = libosfp_score_db_array_add;
|
||||
db->match = libosfp_score_db_array_match;
|
||||
break;
|
||||
case LIBOSFP_FIELD_TYPE_STRING:
|
||||
db->create = libosfp_score_db_hash_create;
|
||||
db->destroy = libosfp_score_db_hash_destroy;
|
||||
db->add = libosfp_score_db_hash_add;
|
||||
db->match = libosfp_score_db_hash_match;
|
||||
break;
|
||||
default:
|
||||
libosfp_log_debug("unsupported type: %u", db->type);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
db->data = db->create();
|
||||
if (db->data == NULL) {
|
||||
libosfp_log_debug("create failed. field: %s", libosfp_fingerprint_get_field_name(i));
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
return score_db;
|
||||
exit:
|
||||
if (score_db) {
|
||||
libosfp_score_db_destroy(score_db);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void libosfp_score_db_destroy(libosfp_score_db_t *score_db)
|
||||
{
|
||||
int i;
|
||||
libosfp_field_score_db_t *db;
|
||||
|
||||
if (score_db) {
|
||||
for (i = 0; i < LIBOSFP_FIELD_MAX; i++) {
|
||||
db = &score_db->field_score_dbs[i];
|
||||
db->destroy(db->data);
|
||||
db->data = NULL;
|
||||
}
|
||||
free(score_db);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
#ifndef __LIBOSFP_SCORE_DB_H__
|
||||
#define __LIBOSFP_SCORE_DB_H__
|
||||
|
||||
#include "libosfp_common.h"
|
||||
#include "libosfp_fingerprint.h"
|
||||
|
||||
typedef struct libosfp_field_score_db {
|
||||
unsigned int enabled;
|
||||
unsigned int type;
|
||||
unsigned int entry_count;
|
||||
|
||||
void *data;
|
||||
|
||||
void *(*create)(void);
|
||||
void (*destroy)(void *);
|
||||
int (*add)(void *data, libosfp_score_t *, void *, unsigned int);
|
||||
libosfp_score_t *(*match)(void *, void *, unsigned int);
|
||||
} libosfp_field_score_db_t;
|
||||
|
||||
typedef struct libosfp_score_db {
|
||||
unsigned int entry_count;
|
||||
unsigned int perfect_score;
|
||||
unsigned int os_class_entry_count[LIBOSFP_OS_CLASS_MAX];
|
||||
libosfp_field_score_db_t field_score_dbs[LIBOSFP_FIELD_MAX];
|
||||
} libosfp_score_db_t;
|
||||
|
||||
int libosfp_score_db_load(libosfp_score_db_t *score_db, char *fp_file);
|
||||
int libosfp_score_db_score(libosfp_score_db_t *score_db, unsigned int flags, libosfp_fingerprint_t *fp, libosfp_score_t *result);
|
||||
|
||||
void libosfp_score_db_debug_print(libosfp_score_db_t *score_db);
|
||||
libosfp_score_db_t *libosfp_score_db_create(void);
|
||||
void libosfp_score_db_destroy(libosfp_score_db_t *score_db);
|
||||
|
||||
#endif
|
||||
258
src/osfp.c
Normal file
258
src/osfp.c
Normal file
@@ -0,0 +1,258 @@
|
||||
#include "osfp_common.h"
|
||||
|
||||
#include "osfp.h"
|
||||
#include "osfp_fingerprint.h"
|
||||
#include "osfp_score_db.h"
|
||||
#include "osfp_log.h"
|
||||
|
||||
#define OSFP_DEFAULT_RESULT_BUFLEN_MAX 512
|
||||
|
||||
static struct osfp_result *osfp_result_build(struct osfp_os_class_score *os_class_score)
|
||||
{
|
||||
int i;
|
||||
unsigned int tmp_score;
|
||||
unsigned int likely_score;
|
||||
enum osfp_os_class_id likely_os_class;
|
||||
struct osfp_result *result;
|
||||
|
||||
result = calloc(1, sizeof(struct osfp_result));
|
||||
if (result == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
likely_score = 0;
|
||||
likely_os_class = OSFP_OS_CLASS_OTHERS;
|
||||
|
||||
for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
|
||||
tmp_score = os_class_score->scores[i];
|
||||
|
||||
if (likely_score < tmp_score) {
|
||||
likely_score = tmp_score;
|
||||
likely_os_class = i;
|
||||
}
|
||||
result->detail.scores[i] = tmp_score;
|
||||
}
|
||||
|
||||
result->likely_score = likely_score;
|
||||
result->likely_os_class = likely_os_class;
|
||||
|
||||
if (likely_score < 10) {
|
||||
result->likely_os_class = OSFP_OS_CLASS_OTHERS;
|
||||
result->likely_score = 0;
|
||||
} else {
|
||||
for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
|
||||
if (likely_os_class == i) {
|
||||
continue;
|
||||
}
|
||||
if (likely_score == os_class_score->scores[i]) {
|
||||
if (likely_os_class == OSFP_OS_CLASS_LINUX && os_class_score->scores[i] == OSFP_OS_CLASS_ANDROID) {
|
||||
continue;
|
||||
} else if (likely_os_class == OSFP_OS_CLASS_MAC_OS && os_class_score->scores[i] == OSFP_OS_CLASS_IOS) {
|
||||
continue;
|
||||
} else {
|
||||
result->likely_os_class = OSFP_OS_CLASS_UNKNOWN;
|
||||
result->likely_score = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
exit:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *osfp_result_os_name_get(struct osfp_result *result)
|
||||
{
|
||||
enum osfp_os_class_id os_class;
|
||||
|
||||
if (result == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
os_class = result->likely_os_class;
|
||||
if (os_class < 0 || os_class >= OSFP_OS_CLASS_MAX) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return osfp_os_class_id_to_name(os_class);
|
||||
}
|
||||
|
||||
char *osfp_result_score_detail_export(struct osfp_result *result)
|
||||
{
|
||||
int i;
|
||||
cJSON *root = NULL;
|
||||
cJSON *array;
|
||||
cJSON *os_score;
|
||||
|
||||
if (result == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (result->json_str != NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
os_score = cJSON_AddObjectToObject(root, "likely");
|
||||
if (os_score) {
|
||||
cJSON_AddStringToObject(os_score, "name", osfp_os_class_id_to_name(result->likely_os_class));
|
||||
cJSON_AddNumberToObject(os_score, "score", result->likely_score);
|
||||
}
|
||||
|
||||
array = cJSON_AddArrayToObject(root, "detail");
|
||||
if (array) {
|
||||
for (i = OSFP_OS_CLASS_WINDOWS; i < OSFP_OS_CLASS_OTHERS; i++) {
|
||||
os_score = cJSON_CreateObject();
|
||||
if (os_score) {
|
||||
cJSON_AddStringToObject(os_score, "name", osfp_os_class_id_to_name(i));
|
||||
cJSON_AddNumberToObject(os_score, "score", result->detail.scores[i]);
|
||||
cJSON_AddItemToArray(array, os_score);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result->json_str = malloc(OSFP_DEFAULT_RESULT_BUFLEN_MAX);
|
||||
if (result->json_str == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (!cJSON_PrintPreallocated(root, result->json_str, OSFP_DEFAULT_RESULT_BUFLEN_MAX, 1)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
if (root) {
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
return result->json_str;
|
||||
}
|
||||
|
||||
void osfp_result_free(struct osfp_result *result)
|
||||
{
|
||||
if (result) {
|
||||
if (result->json_str) {
|
||||
free(result->json_str);
|
||||
}
|
||||
free(result);
|
||||
}
|
||||
}
|
||||
|
||||
struct osfp_result *osfp_ipv4_identify(struct osfp_db *db, struct iphdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len)
|
||||
{
|
||||
int ret = OSFP_EINVAL;
|
||||
struct osfp_fingerprint fp;
|
||||
struct osfp_os_class_score os_class_score;
|
||||
struct osfp_result *result;
|
||||
|
||||
if (db == NULL || l3_hdr == NULL || l4_hdr == NULL || l4_hdr == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_fingerprinting((unsigned char *)l3_hdr, (unsigned char *)l4_hdr, (unsigned int)l4_hdr_len, &fp, 4);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_score_db_score(db->score_db, 0, &fp, &os_class_score);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
result = osfp_result_build(&os_class_score);
|
||||
if (result == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return result;
|
||||
exit:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct osfp_result *osfp_ipv6_identify(struct osfp_db *db, struct ipv6hdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len)
|
||||
{
|
||||
int ret = OSFP_EINVAL;
|
||||
struct osfp_fingerprint fp;
|
||||
struct osfp_os_class_score os_class_score;
|
||||
struct osfp_result *result;
|
||||
|
||||
if (db == NULL || l3_hdr == NULL || l4_hdr == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_fingerprinting((unsigned char *)l3_hdr, (unsigned char *)l4_hdr, (unsigned int)l4_hdr_len, &fp, 6);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_score_db_score(db->score_db, 0, &fp, &os_class_score);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
result = osfp_result_build(&os_class_score);
|
||||
if (result == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return result;
|
||||
exit:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct osfp_db *osfp_db_new(const char *db_json_file)
|
||||
{
|
||||
int ret;
|
||||
struct osfp_db *db;
|
||||
|
||||
db = calloc(1, sizeof(struct osfp_db));
|
||||
if (db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (db_json_file != NULL) {
|
||||
if (0 != access(db_json_file, R_OK)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
db->db_json_path = strdup((const char*)db_json_file);
|
||||
if (db->db_json_path == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
db->score_db = (void *)osfp_score_db_create();
|
||||
if (db->score_db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_score_db_load((struct osfp_score_db *)db->score_db, db->db_json_path);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return db;
|
||||
exit:
|
||||
if (db) {
|
||||
osfp_db_free(db);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void osfp_db_free(struct osfp_db *db)
|
||||
{
|
||||
if (db) {
|
||||
if (db->db_json_path) {
|
||||
free(db->db_json_path);
|
||||
}
|
||||
if (db->score_db) {
|
||||
osfp_score_db_destroy(db->score_db);
|
||||
}
|
||||
free(db);
|
||||
}
|
||||
}
|
||||
58
src/osfp.h
Normal file
58
src/osfp.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef __OSFP_H__
|
||||
#define __OSFP_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <linux/in.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/ipv6.h>
|
||||
#include <linux/tcp.h>
|
||||
|
||||
#define OSFP_OS_CLASS_NAME_UNKNOWN "Unknown"
|
||||
#define OSFP_OS_CLASS_NAME_WINDOWS "Windows"
|
||||
#define OSFP_OS_CLASS_NAME_LINUX "Linux"
|
||||
#define OSFP_OS_CLASS_NAME_MAC_OS "Mac OS"
|
||||
#define OSFP_OS_CLASS_NAME_IOS "iOS"
|
||||
#define OSFP_OS_CLASS_NAME_ANDROID "Android"
|
||||
#define OSFP_OS_CLASS_NAME_OTHERS "Others"
|
||||
|
||||
enum osfp_os_class_id {
|
||||
OSFP_OS_CLASS_UNKNOWN,
|
||||
OSFP_OS_CLASS_WINDOWS,
|
||||
OSFP_OS_CLASS_LINUX,
|
||||
OSFP_OS_CLASS_MAC_OS,
|
||||
OSFP_OS_CLASS_IOS,
|
||||
OSFP_OS_CLASS_ANDROID,
|
||||
OSFP_OS_CLASS_OTHERS,
|
||||
OSFP_OS_CLASS_MAX,
|
||||
};
|
||||
|
||||
struct osfp_os_class_score {
|
||||
unsigned int scores[OSFP_OS_CLASS_MAX];
|
||||
};
|
||||
|
||||
struct osfp_result {
|
||||
char *json_str;
|
||||
|
||||
enum osfp_os_class_id likely_os_class;
|
||||
unsigned int likely_score;
|
||||
|
||||
struct osfp_os_class_score detail;
|
||||
};
|
||||
|
||||
struct osfp_db {
|
||||
char *db_json_path;
|
||||
void *score_db;
|
||||
};
|
||||
|
||||
struct osfp_db *osfp_db_new(const char *db_json_path);
|
||||
void osfp_db_free(struct osfp_db *db);
|
||||
|
||||
struct osfp_result *osfp_ipv4_identify(struct osfp_db *db, struct iphdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len);
|
||||
struct osfp_result *osfp_ipv6_identify(struct osfp_db *db, struct ipv6hdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len);
|
||||
|
||||
const char *osfp_result_os_name_get(struct osfp_result *result);
|
||||
char *osfp_result_score_detail_export(struct osfp_result *result);
|
||||
void osfp_result_free(struct osfp_result *result);
|
||||
|
||||
#endif
|
||||
40
src/osfp_common.c
Normal file
40
src/osfp_common.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "osfp_common.h"
|
||||
|
||||
#include "osfp.h"
|
||||
|
||||
const char *os_class_name[OSFP_OS_CLASS_MAX] = {
|
||||
OSFP_OS_CLASS_NAME_UNKNOWN,
|
||||
OSFP_OS_CLASS_NAME_WINDOWS,
|
||||
OSFP_OS_CLASS_NAME_LINUX,
|
||||
OSFP_OS_CLASS_NAME_MAC_OS,
|
||||
OSFP_OS_CLASS_NAME_IOS,
|
||||
OSFP_OS_CLASS_NAME_ANDROID,
|
||||
OSFP_OS_CLASS_NAME_OTHERS
|
||||
};
|
||||
|
||||
enum osfp_os_class_id osfp_os_class_name_to_id(char *name)
|
||||
{
|
||||
enum osfp_os_class_id os_class;
|
||||
|
||||
if (0 == strncmp(name, OSFP_OS_CLASS_NAME_WINDOWS, strlen(OSFP_OS_CLASS_NAME_WINDOWS))) {
|
||||
os_class = OSFP_OS_CLASS_WINDOWS;
|
||||
} else if (0 == strncmp(name, OSFP_OS_CLASS_NAME_LINUX, strlen(OSFP_OS_CLASS_NAME_LINUX))) {
|
||||
os_class = OSFP_OS_CLASS_LINUX;
|
||||
} else if (0 == strncmp(name, OSFP_OS_CLASS_NAME_MAC_OS, strlen(OSFP_OS_CLASS_NAME_MAC_OS))) {
|
||||
os_class = OSFP_OS_CLASS_MAC_OS;
|
||||
} else if (0 == strncmp(name, OSFP_OS_CLASS_NAME_IOS, strlen(OSFP_OS_CLASS_NAME_IOS))) {
|
||||
os_class = OSFP_OS_CLASS_IOS;
|
||||
} else if (0 == strncmp(name, OSFP_OS_CLASS_NAME_ANDROID, strlen(OSFP_OS_CLASS_NAME_ANDROID))) {
|
||||
os_class = OSFP_OS_CLASS_ANDROID;
|
||||
} else {
|
||||
os_class = OSFP_OS_CLASS_MAX;
|
||||
}
|
||||
|
||||
return os_class;
|
||||
}
|
||||
|
||||
const char *osfp_os_class_id_to_name(enum osfp_os_class_id os_class)
|
||||
{
|
||||
return os_class_name[os_class];
|
||||
}
|
||||
|
||||
146
src/osfp_common.h
Normal file
146
src/osfp_common.h
Normal file
@@ -0,0 +1,146 @@
|
||||
#ifndef __OSFP_COMMON_H__
|
||||
#define __OSFP_COMMON_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <malloc.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <linux/in.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/ipv6.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "utarray.h"
|
||||
#include "uthash.h"
|
||||
#include "utlist.h"
|
||||
#include "utringbuffer.h"
|
||||
#include "utstack.h"
|
||||
#include "utstring.h"
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
#include "osfp.h"
|
||||
|
||||
#define OSFP_TCP_OPTLENMAX 64
|
||||
#define OSFP_TCP_OPTMAX 20
|
||||
|
||||
#define OSFP_ETHERNET_HEADER_LEN 14
|
||||
#define OSFP_VLAN_HEADER_LEN 4
|
||||
#define OSFP_IPV4_HEADER_LEN 20
|
||||
#define OSFP_IPV6_HEADER_LEN 40
|
||||
#define OSFP_TCP_HEADER_LEN 20
|
||||
#define OSFP_TCP_DATA_OFF_MAX 60
|
||||
|
||||
|
||||
//# TCP Options (opt_type) - http://www.iana.org/assignments/tcp-parameters
|
||||
#define OSFP_TCP_OPT_EOL 0 //# end of option list
|
||||
#define OSFP_TCP_OPT_NOP 1 //# no operation
|
||||
#define OSFP_TCP_OPT_MSS 2 //# maximum segment size
|
||||
#define OSFP_TCP_OPT_WSCALE 3 //# window scale factor, RFC 1072
|
||||
#define OSFP_TCP_OPT_SACKOK 4 //# SACK permitted, RFC 2018
|
||||
#define OSFP_TCP_OPT_SACK 5 //# SACK, RFC 2018
|
||||
#define OSFP_TCP_OPT_ECHO 6 //# echo (obsolete), RFC 1072
|
||||
#define OSFP_TCP_OPT_ECHOREPLY 7 //# echo reply (obsolete), RFC 1072
|
||||
#define OSFP_TCP_OPT_TIMESTAMP 8 //# timestamps, RFC 1323
|
||||
#define OSFP_TCP_OPT_POCONN 9 //# partial order conn, RFC 1693
|
||||
#define OSFP_TCP_OPT_POSVC 10 //# partial order service, RFC 1693
|
||||
#define OSFP_TCP_OPT_CC 11 //# connection count, RFC 1644
|
||||
#define OSFP_TCP_OPT_CCNEW 12 //# CC.NEW, RFC 1644
|
||||
#define OSFP_TCP_OPT_CCECHO 13 //# CC.ECHO, RFC 1644
|
||||
#define OSFP_TCP_OPT_ALTSUM 14 //# alt checksum request, RFC 1146
|
||||
#define OSFP_TCP_OPT_ALTSUMDATA 15 //# alt checksum data, RFC 1146
|
||||
#define OSFP_TCP_OPT_SKEETER 16 //# Skeeter
|
||||
#define OSFP_TCP_OPT_BUBBA 17 //# Bubba
|
||||
#define OSFP_TCP_OPT_TRAILSUM 18 //# trailer checksum
|
||||
#define OSFP_TCP_OPT_MD5 19 //# MD5 signature, RFC 2385
|
||||
#define OSFP_TCP_OPT_SCPS 20 //# SCPS capabilities
|
||||
#define OSFP_TCP_OPT_SNACK 21 //# selective negative acks
|
||||
#define OSFP_TCP_OPT_REC 22 //# record boundaries
|
||||
#define OSFP_TCP_OPT_CORRUPT 23 //# corruption experienced
|
||||
#define OSFP_TCP_OPT_SNAP 24 //# SNAP
|
||||
#define OSFP_TCP_OPT_TCPCOMP 26 //# TCP compression filter
|
||||
#define OSFP_TCP_OPT_MAX 27 //# Quick-Start Response
|
||||
#define OSFP_TCP_OPT_USRTO 28 //# User Timeout Option (also, other known unauthorized use) [***][1] [RFC5482]
|
||||
#define OSFP_TCP_OPT_AUTH 29 //# TCP Authentication Option (TCP-AO) [RFC5925]
|
||||
#define OSFP_TCP_OPT_MULTIPATH 30 //# Multipath TCP (MPTCP)
|
||||
#define OSFP_TCP_OPT_FASTOPEN 34 //# TCP Fast Open Cookie [RFC7413]
|
||||
#define OSFP_TCP_OPY_ENCNEG 69 //# Encryption Negotiation (TCP-ENO) [RFC8547]
|
||||
#define OSFP_TCP_OPT_EXP1 253 //# RFC3692-style Experiment 1 (also improperly used for shipping products)
|
||||
#define OSFP_TCP_OPT_EXP2 254 //# RFC3692-style Experiment 2 (also improperly used for shipping products)
|
||||
|
||||
#define OSFP_TCP_OPT_SACKOK_LEN 2
|
||||
#define OSFP_TCP_OPT_WS_LEN 3
|
||||
#define OSFP_TCP_OPT_TS_LEN 10
|
||||
#define OSFP_TCP_OPT_MSS_LEN 4
|
||||
#define OSFP_TCP_OPT_SACK_MIN_LEN 10 /* hdr 2, 1 pair 8 = 10 */
|
||||
#define OSFP_TCP_OPT_SACK_MAX_LEN 34 /* hdr 2, 4 pair 32= 34 */
|
||||
#define OSFP_TCP_OPT_TFO_MIN_LEN 4 /* kind, len, 2 bytes cookie: 4 */
|
||||
#define OSFP_TCP_OPT_TFO_MAX_LEN 18 /* kind, len, 18 */
|
||||
|
||||
|
||||
#define OSFP_WRITE_STRING_TO_BUF(ret, buf, size, off, ...) do { \
|
||||
ret = snprintf((char *)buf + off, \
|
||||
size - off, \
|
||||
__VA_ARGS__); \
|
||||
if (ret >= 0) { \
|
||||
if ( (off + ret) >= size) { \
|
||||
off = size - 1; \
|
||||
} else { \
|
||||
off += ret; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static inline unsigned long long osfp_rdtsc(void)
|
||||
{
|
||||
union {
|
||||
unsigned long long tsc_64;
|
||||
struct {
|
||||
unsigned int lo_32;
|
||||
unsigned int hi_32;
|
||||
};
|
||||
} tsc;
|
||||
|
||||
asm volatile("rdtsc" :
|
||||
"=a" (tsc.lo_32),
|
||||
"=d" (tsc.hi_32));
|
||||
return tsc.tsc_64;
|
||||
}
|
||||
|
||||
#define osfp_profile_cycle(x) volatile unsigned long long x = 0
|
||||
#define osfp_profile_get_cycle(x) do { \
|
||||
x = osfp_rdtsc(); \
|
||||
} while(0)
|
||||
|
||||
#define OSFP_BIT_U32(n) (1UL << (n))
|
||||
|
||||
enum osfp_error_code {
|
||||
OSFP_NOERR,
|
||||
OSFP_EINVAL,
|
||||
OSFP_ENOMEM,
|
||||
OSFP_ERR_SCORE_DB_READ_FILE,
|
||||
OSFP_ERR_SCORE_DB_PARSE_FILE,
|
||||
OSFP_ERR_SCORE_DB_UNSUPPORTED,
|
||||
|
||||
OSFP_ERR_FINGERPRINTING_UNSUPPORTED,
|
||||
|
||||
};
|
||||
|
||||
#define OSFP_OS_CLASS_FLAG_WINDOWS OSFP_BIT_U32(OSFP_OS_CLASS_WINDOWS)
|
||||
#define OSFP_OS_CLASS_FLAG_LINUX OSFP_BIT_U32(OSFP_OS_CLASS_LINUX)
|
||||
#define OSFP_OS_CLASS_FLAG_MAC_OS OSFP_BIT_U32(OSFP_OS_CLASS_MAC_OS)
|
||||
#define OSFP_OS_CLASS_FLAG_IOS OSFP_BIT_U32(OSFP_OS_CLASS_IOS)
|
||||
#define OSFP_OS_CLASS_FLAG_ANDROID OSFP_BIT_U32(OSFP_OS_CLASS_ANDROID)
|
||||
|
||||
|
||||
const char *osfp_os_class_id_to_name(enum osfp_os_class_id os_class);
|
||||
enum osfp_os_class_id osfp_os_class_name_to_id(char *name);
|
||||
|
||||
#endif
|
||||
447
src/osfp_fingerprint.c
Normal file
447
src/osfp_fingerprint.c
Normal file
@@ -0,0 +1,447 @@
|
||||
#include "osfp_common.h"
|
||||
|
||||
#include "osfp.h"
|
||||
#include "osfp_fingerprint.h"
|
||||
#include "osfp_log.h"
|
||||
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_IP_ID "ip_id"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_IP_TOS "ip_tos"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_IP_TOTAL_LENGHT "ip_total_length"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_IP_TTL "ip_ttl"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_OFF "tcp_off"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP "tcp_timestamp"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP_ECHO_REPLY "tcp_timestamp_echo_reply"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_WCALING "tcp_window_scaling"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_SIZE "tcp_window_size"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_FLAGS "tcp_flags"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_MSS "tcp_mss"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS "tcp_options"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS_ORDERED "tcp_options_ordered"
|
||||
#define OSFP_FINGERPRINT_FIELD_NAME_OS "os"
|
||||
|
||||
#define OSFP_FINGERPRINT_DEFAULT_OS_CLASS_NAME "OSFP_UNKNOWN"
|
||||
|
||||
struct osfp_tcp_opt {
|
||||
unsigned char type;
|
||||
unsigned char len;
|
||||
const unsigned char *data;
|
||||
} osfp_tcp_opt;
|
||||
|
||||
struct osfp_fingerprint_field fp_fields[OSFP_FIELD_MAX] = {
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_IP_ID, 1, OSFP_FIELD_TYPE_UINT, 150, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_IP_TOS, 1, OSFP_FIELD_TYPE_UINT, 25, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_IP_TOTAL_LENGHT, 1, OSFP_FIELD_TYPE_UINT, 250, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_IP_TTL, 1, OSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_OFF, 1, OSFP_FIELD_TYPE_UINT, 250, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP, 0, OSFP_FIELD_TYPE_UINT, 0, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_TIMESTAMP_ECHO_REPLY,1, OSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_WCALING, 1, OSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_WINDOW_SIZE, 1, OSFP_FIELD_TYPE_UINT, 200, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_FLAGS, 1, OSFP_FIELD_TYPE_UINT, 25, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_MSS, 1, OSFP_FIELD_TYPE_UINT, 150, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS, 1, OSFP_FIELD_TYPE_STRING, 400, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_TCP_OPTIONS_ORDERED, 1, OSFP_FIELD_TYPE_STRING, 250, NULL, 0},
|
||||
{OSFP_FINGERPRINT_FIELD_NAME_OS, 0, OSFP_FIELD_TYPE_STRING, 0, NULL, 0},
|
||||
};
|
||||
|
||||
static char option_to_ascii(unsigned char type)
|
||||
{
|
||||
switch (type) {
|
||||
case OSFP_TCP_OPT_EOL:
|
||||
return 'E';
|
||||
case OSFP_TCP_OPT_NOP:
|
||||
return 'N';
|
||||
case OSFP_TCP_OPT_MSS:
|
||||
return 'M';
|
||||
case OSFP_TCP_OPT_WSCALE:
|
||||
return 'W';
|
||||
case OSFP_TCP_OPT_SACKOK:
|
||||
return 'S';
|
||||
case OSFP_TCP_OPT_SACK:
|
||||
return 'K';
|
||||
case OSFP_TCP_OPT_ECHO:
|
||||
return 'J';
|
||||
case OSFP_TCP_OPT_ECHOREPLY:
|
||||
return 'F';
|
||||
case OSFP_TCP_OPT_TIMESTAMP:
|
||||
return 'T';
|
||||
case OSFP_TCP_OPT_POCONN:
|
||||
return 'P';
|
||||
case OSFP_TCP_OPT_POSVC:
|
||||
return 'R';
|
||||
default:
|
||||
return 'U';
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int compute_ip_ttl(unsigned int ip_ttl)
|
||||
{
|
||||
if (ip_ttl >= 0 && ip_ttl <= 32) {
|
||||
ip_ttl = 32;
|
||||
} else if (ip_ttl > 32 && ip_ttl <= 64) {
|
||||
ip_ttl = 64;
|
||||
} else if (ip_ttl > 64 && ip_ttl <= 128) {
|
||||
ip_ttl = 128;
|
||||
} else {
|
||||
ip_ttl = 255;
|
||||
}
|
||||
return ip_ttl;
|
||||
}
|
||||
|
||||
static unsigned int decode_tcp_options(struct osfp_tcp_opt *tcp_opts, unsigned int max_opt_cnt, unsigned char *data, unsigned int len)
|
||||
{
|
||||
unsigned int offset = 0;
|
||||
unsigned int tcp_opt_cnt = 0;
|
||||
|
||||
unsigned char type;
|
||||
unsigned char olen;
|
||||
unsigned char *odata;
|
||||
|
||||
while (offset < len && tcp_opt_cnt < max_opt_cnt) {
|
||||
type = *(data + offset);
|
||||
|
||||
if (type == OSFP_TCP_OPT_EOL || type == OSFP_TCP_OPT_NOP) {
|
||||
olen = 1;
|
||||
} else {
|
||||
olen = *(data + offset + 1);
|
||||
if (olen < 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset + olen > len) {
|
||||
break;
|
||||
}
|
||||
|
||||
odata = (olen > 2) ? (data + offset + 2) : NULL;
|
||||
|
||||
tcp_opts[tcp_opt_cnt].type = type;
|
||||
tcp_opts[tcp_opt_cnt].len = olen;
|
||||
tcp_opts[tcp_opt_cnt].data = odata;
|
||||
|
||||
offset += olen;
|
||||
tcp_opt_cnt++;
|
||||
}
|
||||
return tcp_opt_cnt;
|
||||
}
|
||||
|
||||
int osfp_fingerprint_to_json_buf(struct osfp_fingerprint *fp, char *strbuf, unsigned int buf_len, unsigned int format)
|
||||
{
|
||||
int rlen = 0, ret, i;
|
||||
cJSON *root;
|
||||
|
||||
if (fp == NULL || strbuf == NULL || buf_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_FIELD_MAX; i++) {
|
||||
if (fp->fields[i].enabled) {
|
||||
switch (fp_fields[i].type) {
|
||||
case OSFP_FIELD_TYPE_UINT:
|
||||
cJSON_AddNumberToObject(root, fp_fields[i].name, *(unsigned int *)fp->fields[i].value);
|
||||
break;
|
||||
case OSFP_FIELD_TYPE_STRING:
|
||||
cJSON_AddStringToObject(root, fp_fields[i].name, (char *)fp->fields[i].value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
cJSON_AddNullToObject(root, fp_fields[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cJSON_PrintPreallocated(root, strbuf, buf_len, format)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return strlen(strbuf) + 1;
|
||||
}
|
||||
|
||||
unsigned int osfp_fingerprint_get_field_enabled(enum osfp_field_id field_id)
|
||||
{
|
||||
return fp_fields[field_id].enabled;
|
||||
}
|
||||
|
||||
unsigned int osfp_fingerprint_get_field_importance(enum osfp_field_id field_id)
|
||||
{
|
||||
return fp_fields[field_id].importance;
|
||||
}
|
||||
|
||||
char *osfp_fingerprint_get_field_name(enum osfp_field_id field_id)
|
||||
{
|
||||
return fp_fields[field_id].name;
|
||||
}
|
||||
|
||||
unsigned int osfp_fingerprint_get_field_type(enum osfp_field_id field_id)
|
||||
{
|
||||
return fp_fields[field_id].type;
|
||||
}
|
||||
|
||||
void osfp_fingerprint_setup_field(struct osfp_fingerprint *fp, enum osfp_field_id field_id, void *value, unsigned int len)
|
||||
{
|
||||
fp->fields[field_id].name = osfp_fingerprint_get_field_name(field_id);
|
||||
fp->fields[field_id].enabled = 1;
|
||||
|
||||
if (fp->value_buffer_used + len <= sizeof(fp->value_buffer)) {
|
||||
memcpy(fp->value_buffer + fp->value_buffer_used, value, len);
|
||||
fp->fields[field_id].value = fp->value_buffer + fp->value_buffer_used;
|
||||
fp->fields[field_id].value_len = len;
|
||||
fp->value_buffer_used += len;
|
||||
} else {
|
||||
fp->fields[field_id].value = NULL;
|
||||
fp->fields[field_id].value_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void osfp_fingerprinting_tcp_option(unsigned char *opt_data, unsigned int opt_len, struct osfp_fingerprint *fp)
|
||||
{
|
||||
int ret,i;
|
||||
|
||||
unsigned int tcp_mss;
|
||||
unsigned int tcp_ws;
|
||||
unsigned int tcp_ter;
|
||||
unsigned int tcp_opt_cnt;
|
||||
struct osfp_tcp_opt tcp_opts[OSFP_TCP_OPTMAX];
|
||||
|
||||
char options[OSFP_TCP_OPTLENMAX];
|
||||
char options_ordered[OSFP_TCP_OPTLENMAX];
|
||||
unsigned int offset = 0;
|
||||
unsigned int maxoffset = sizeof(options) - 3; //for shortest "E,"
|
||||
unsigned int ordered_offset = 0;
|
||||
unsigned int ordered_maxoffset = sizeof(options_ordered) - 1;
|
||||
|
||||
tcp_opt_cnt = decode_tcp_options(tcp_opts, OSFP_TCP_OPTMAX, opt_data, opt_len);
|
||||
|
||||
for (i = 0; i < tcp_opt_cnt && offset < maxoffset && ordered_offset < ordered_maxoffset; i++) {
|
||||
struct osfp_tcp_opt *opt = &tcp_opts[i];
|
||||
|
||||
char letter = option_to_ascii(opt->type);
|
||||
options[offset++] = letter;
|
||||
options_ordered[ordered_offset++] = letter;
|
||||
|
||||
switch (opt->type) {
|
||||
case OSFP_TCP_OPT_EOL:
|
||||
case OSFP_TCP_OPT_NOP:
|
||||
break;
|
||||
case OSFP_TCP_OPT_MSS:
|
||||
if (opt->len != OSFP_TCP_OPT_MSS_LEN) {
|
||||
break;
|
||||
}
|
||||
tcp_mss = (unsigned int)ntohs(*(unsigned short *)opt->data);
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_MSS, &tcp_mss, sizeof(tcp_mss));
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", tcp_mss);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
break;
|
||||
case OSFP_TCP_OPT_WSCALE:
|
||||
if (opt->len != OSFP_TCP_OPT_WS_LEN) {
|
||||
break;
|
||||
}
|
||||
tcp_ws = (unsigned int)*(unsigned char *)opt->data;
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_WINDOW_SCALING, &tcp_ws, sizeof(tcp_ws));
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", tcp_ws);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
break;
|
||||
case OSFP_TCP_OPT_TIMESTAMP:
|
||||
if (opt->len != OSFP_TCP_OPT_TS_LEN) {
|
||||
break;
|
||||
}
|
||||
tcp_ter = ntohl(*(unsigned int *)(opt->data + 4));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_TIMESTAMP_ECHO_REPLY, &tcp_ter, sizeof(tcp_ter));
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", tcp_ter);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
break;
|
||||
case OSFP_TCP_OPT_SACKOK:
|
||||
case OSFP_TCP_OPT_SACK:
|
||||
case OSFP_TCP_OPT_ECHO:
|
||||
case OSFP_TCP_OPT_ECHOREPLY:
|
||||
case OSFP_TCP_OPT_POCONN:
|
||||
case OSFP_TCP_OPT_POSVC:
|
||||
break;
|
||||
default:
|
||||
ret = snprintf(options + offset, sizeof(options), "%u", opt->type);
|
||||
if (ret < 0 || offset + ret > maxoffset) {
|
||||
break;
|
||||
}
|
||||
offset += ret;
|
||||
}
|
||||
|
||||
options[offset++] = ',';
|
||||
options[offset] = 0;
|
||||
options_ordered[ordered_offset] = 0;
|
||||
}
|
||||
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_OPTIONS, options, strlen(options) + 1);
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_OPTIONS_ORDERED, options_ordered, strlen(options_ordered) + 1);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int osfp_fingerprinting_tcp(struct tcphdr *tcph, unsigned int tcph_len, struct osfp_fingerprint *fp)
|
||||
{
|
||||
unsigned int tcp_off;
|
||||
unsigned int tcp_window_size;
|
||||
unsigned int tcp_flags;
|
||||
|
||||
if (tcph == NULL || tcph_len > OSFP_TCP_DATA_OFF_MAX || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
tcp_off = tcph->doff << 2;
|
||||
tcp_window_size = ntohs(tcph->window);
|
||||
tcp_flags = *((unsigned char *)&tcph->window - 1);
|
||||
|
||||
if (tcp_off != tcph_len) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_OFF, &tcp_off, sizeof(tcp_off));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_WINDOW_SIZE, &tcp_window_size, sizeof(tcp_window_size));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_TCP_FLAGS, &tcp_flags, sizeof(tcp_flags));
|
||||
|
||||
// tcp options
|
||||
if (tcp_off > OSFP_TCP_HEADER_LEN) {
|
||||
osfp_fingerprinting_tcp_option((unsigned char *)tcph + OSFP_TCP_HEADER_LEN, tcp_off - OSFP_TCP_HEADER_LEN, fp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int osfp_fingerprinting_ipv4(struct iphdr *iph, struct osfp_fingerprint *fp)
|
||||
{
|
||||
if (iph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
unsigned int ip_id = !!iph->id;
|
||||
unsigned int ip_tos = iph->tos;
|
||||
unsigned int ip_total_length = ntohs(iph->tot_len);
|
||||
unsigned int ip_ttl = compute_ip_ttl(iph->ttl);
|
||||
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_ID, &ip_id, sizeof(ip_id));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_TOS, &ip_tos, sizeof(ip_tos));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_TOTAL_LENGTH, &ip_total_length, sizeof(ip_total_length));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_TTL, &ip_ttl, sizeof(ip_ttl));
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int osfp_fingerprinting_ipv6(struct ipv6hdr *iph, struct osfp_fingerprint *fp)
|
||||
{
|
||||
if (iph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
//unsigned int ip_id = 0;
|
||||
//unsigned int ip_tos = 0;
|
||||
unsigned int ip_total_length = OSFP_IPV6_HEADER_LEN + ntohs(iph->payload_len);
|
||||
unsigned int ip_ttl = compute_ip_ttl(iph->hop_limit);
|
||||
|
||||
//osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_ID, &ip_id, sizeof(ip_id));
|
||||
//osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_TOS, &ip_tos, sizeof(ip_tos));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_TOTAL_LENGTH, &ip_total_length, sizeof(ip_total_length));
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_IP_TTL, &ip_ttl, sizeof(ip_ttl));
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int osfp_fingerprinting(unsigned char *iph, unsigned char *tcph, unsigned int tcph_len, struct osfp_fingerprint *fp, unsigned int ip_version)
|
||||
{
|
||||
int ret = OSFP_EINVAL;
|
||||
|
||||
if (iph == NULL || tcph == NULL || fp == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memset(fp, 0, sizeof(struct osfp_fingerprint));
|
||||
|
||||
switch (ip_version) {
|
||||
case 4:
|
||||
ret = osfp_fingerprinting_ipv4((struct iphdr *)iph, fp);
|
||||
break;
|
||||
case 6:
|
||||
ret = osfp_fingerprinting_ipv6((struct ipv6hdr *)iph, fp);
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_fingerprinting_tcp((struct tcphdr *)tcph, tcph_len, fp);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
osfp_fingerprint_setup_field(fp, OSFP_FIELD_OS, OSFP_FINGERPRINT_DEFAULT_OS_CLASS_NAME, strlen(OSFP_FINGERPRINT_DEFAULT_OS_CLASS_NAME) + 1);
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef UNITTEST
|
||||
int test_osfp_fingerprinting(void)
|
||||
{
|
||||
int ret;
|
||||
char iph[] = {
|
||||
0x45, 0x00, 0x00, 0x34, 0x51, 0xc4, 0x40, 0x00,
|
||||
0x80, 0x06, 0xe7, 0x27, 0xc0, 0xa8, 0x73, 0x08,
|
||||
0x6a, 0xb9, 0x23, 0x6e
|
||||
};
|
||||
|
||||
char tcph[] = {
|
||||
0xc1, 0xbd, 0x00, 0x50, 0x3d, 0x58, 0x51, 0x60,
|
||||
0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x20, 0x00,
|
||||
0x3d, 0x3a, 0x00, 0x00, 0x02, 0x04, 0x04, 0xec,
|
||||
0x01, 0x03, 0x03, 0x08, 0x01, 0x01, 0x04, 0x02
|
||||
};
|
||||
|
||||
char str_buf[2048] = "";
|
||||
const char *target_buf = "{\"ip_id\":1,\"ip_tos\":0,\"ip_total_length\":52,\"ip_ttl\":128,\"tcp_off\":32,\"tcp_timestamp\":null,\"tcp_timestamp_echo_reply\":null,\"tcp_window_scaling\":8,\"tcp_window_size\":8192,\"tcp_flags\":2,\"tcp_mss\":1260,\"tcp_options\":\"M1260,N,W8,N,N,S,\",\"tcp_options_ordered\":\"MNWNNS\",\"os\":\"OSFP_UNKNOWN\"}";
|
||||
struct osfp_fingerprint fp = {0};
|
||||
|
||||
ret = osfp_fingerprinting(iph, tcph, &fp);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = osfp_fingerprint_to_json_buf(&fp, str_buf, 2048, 0);
|
||||
if (ret <= 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (0 != memcmp(str_buf, target_buf, strlen(target_buf))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
63
src/osfp_fingerprint.h
Normal file
63
src/osfp_fingerprint.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef __OSFP_FINGERPRINT_H__
|
||||
#define __OSFP_FINGERPRINT_H__
|
||||
|
||||
#define OSFP_FINGERPRINT_VALUE_BUFFER_MAX 128
|
||||
|
||||
enum osfp_field_id {
|
||||
OSFP_FIELD_IP_ID,
|
||||
OSFP_FIELD_IP_TOS,
|
||||
OSFP_FIELD_IP_TOTAL_LENGTH,
|
||||
OSFP_FIELD_IP_TTL,
|
||||
OSFP_FIELD_TCP_OFF,
|
||||
OSFP_FIELD_TCP_TIMESTAMP,
|
||||
OSFP_FIELD_TCP_TIMESTAMP_ECHO_REPLY,
|
||||
OSFP_FIELD_TCP_WINDOW_SCALING,
|
||||
OSFP_FIELD_TCP_WINDOW_SIZE,
|
||||
OSFP_FIELD_TCP_FLAGS,
|
||||
OSFP_FIELD_TCP_MSS,
|
||||
OSFP_FIELD_TCP_OPTIONS,
|
||||
OSFP_FIELD_TCP_OPTIONS_ORDERED,
|
||||
OSFP_FIELD_OS,
|
||||
OSFP_FIELD_MAX,
|
||||
};
|
||||
|
||||
enum osfp_field_type {
|
||||
OSFP_FIELD_TYPE_UNKNOWN,
|
||||
OSFP_FIELD_TYPE_UINT,
|
||||
OSFP_FIELD_TYPE_STRING,
|
||||
OSFP_FIELD_TYPE_MAX
|
||||
};
|
||||
|
||||
struct osfp_fingerprint_field {
|
||||
char *name;
|
||||
unsigned int enabled;
|
||||
unsigned int type;
|
||||
unsigned int importance;
|
||||
void *value;
|
||||
unsigned int value_len;
|
||||
};
|
||||
|
||||
struct osfp_fingerprint {
|
||||
struct osfp_fingerprint_field fields[OSFP_FIELD_MAX];
|
||||
char value_buffer[OSFP_FINGERPRINT_VALUE_BUFFER_MAX];
|
||||
unsigned int value_buffer_used;
|
||||
};
|
||||
|
||||
char *osfp_fingerprint_get_field_name(enum osfp_field_id field_id);
|
||||
unsigned int osfp_fingerprint_get_field_enabled(enum osfp_field_id field_id);
|
||||
unsigned int osfp_fingerprint_get_field_importance(enum osfp_field_id field_id);
|
||||
unsigned int osfp_fingerprint_get_field_type(enum osfp_field_id field_id);
|
||||
|
||||
int osfp_fingerprint_to_json_buf(struct osfp_fingerprint *fp, char *strbuf, unsigned int buf_len, unsigned int format);
|
||||
void osfp_fingerprint_setup_field(struct osfp_fingerprint *fp, enum osfp_field_id field_id, void *value, unsigned int len);
|
||||
|
||||
void osfp_fingerprinting_tcp_option(unsigned char *pkt, unsigned int pktlen, struct osfp_fingerprint *fp);
|
||||
int osfp_fingerprinting_tcp(struct tcphdr *tcph, unsigned int tcph_len, struct osfp_fingerprint *fp);
|
||||
int osfp_fingerprinting_ipv4(struct iphdr *iph, struct osfp_fingerprint *fp);
|
||||
int osfp_fingerprinting_ipv6(struct ipv6hdr *iph, struct osfp_fingerprint *fp);
|
||||
int osfp_fingerprinting(unsigned char *iph, unsigned char *tcph, unsigned int tcph_len, struct osfp_fingerprint *fp, unsigned int ip_version);
|
||||
|
||||
#ifdef UNITTEST
|
||||
int test_osfp_fingerprinting(void);
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,14 +1,14 @@
|
||||
#include "libosfp_common.h"
|
||||
#include "libosfp_log.h"
|
||||
#include "osfp_common.h"
|
||||
#include "osfp_log.h"
|
||||
|
||||
/* The maximum length of the log message */
|
||||
#define LIBOSFP_MAX_LOG_MSG_LEN 2048
|
||||
#define OSFP_MAX_LOG_MSG_LEN 2048
|
||||
|
||||
unsigned int libosfp_log_level = LIBOSFP_LOG_LEVEL_INFO;
|
||||
unsigned int osfp_log_level = OSFP_LOG_LEVEL_INFO;
|
||||
|
||||
void libosfp_log_message(unsigned int x, const char *file, const int line, const char *func, const char *msg)
|
||||
void osfp_log_message(unsigned int x, const char *file, const int line, const char *func, const char *msg)
|
||||
{
|
||||
char buffer[LIBOSFP_MAX_LOG_MSG_LEN] = "";
|
||||
char buffer[OSFP_MAX_LOG_MSG_LEN] = "";
|
||||
char log_time_buf[128];
|
||||
time_t now;
|
||||
struct tm tm_now;
|
||||
@@ -18,36 +18,36 @@ void libosfp_log_message(unsigned int x, const char *file, const int line, const
|
||||
strftime(log_time_buf, sizeof(log_time_buf), "%Y-%m-%d %T", &tm_now);
|
||||
|
||||
switch (x) {
|
||||
case LIBOSFP_LOG_LEVEL_DEBUG:
|
||||
case OSFP_LOG_LEVEL_DEBUG:
|
||||
snprintf(buffer, sizeof(buffer), "[%s][DEBUG][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
|
||||
break;
|
||||
case LIBOSFP_LOG_LEVEL_INFO:
|
||||
case OSFP_LOG_LEVEL_INFO:
|
||||
snprintf(buffer, sizeof(buffer), "[%s][INFO][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
|
||||
break;
|
||||
case LIBOSFP_LOG_LEVEL_WARNING:
|
||||
case OSFP_LOG_LEVEL_WARNING:
|
||||
snprintf(buffer, sizeof(buffer), "[%s][WARN][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
|
||||
break;
|
||||
case LIBOSFP_LOG_LEVEL_ERROR:
|
||||
case OSFP_LOG_LEVEL_ERROR:
|
||||
snprintf(buffer, sizeof(buffer), "[%s][ERROR][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void libosfp_log(unsigned int x, const char *file, const int line, const char *func, const char *fmt, ...)
|
||||
void osfp_log(unsigned int x, const char *file, const int line, const char *func, const char *fmt, ...)
|
||||
{
|
||||
if (libosfp_log_level >= x ) {
|
||||
char msg[LIBOSFP_MAX_LOG_MSG_LEN];
|
||||
if (osfp_log_level >= x ) {
|
||||
char msg[OSFP_MAX_LOG_MSG_LEN];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(msg, sizeof(msg), fmt, ap);
|
||||
va_end(ap);
|
||||
libosfp_log_message(x, file, line, func, msg);
|
||||
osfp_log_message(x, file, line, func, msg);
|
||||
}
|
||||
}
|
||||
|
||||
void libosfp_log_level_set(libosfp_log_level_t level)
|
||||
void osfp_log_level_set(osfp_log_level_t level)
|
||||
{
|
||||
libosfp_log_level = level;
|
||||
osfp_log_level = level;
|
||||
}
|
||||
|
||||
24
src/osfp_log.h
Normal file
24
src/osfp_log.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef __OSFP_LOG_H__
|
||||
#define __OSFP_LOG_H__
|
||||
|
||||
typedef enum osfp_log_level {
|
||||
OSFP_LOG_LEVEL_DEBUG,
|
||||
OSFP_LOG_LEVEL_INFO,
|
||||
OSFP_LOG_LEVEL_WARNING,
|
||||
OSFP_LOG_LEVEL_ERROR
|
||||
} osfp_log_level_t;
|
||||
|
||||
#ifndef DEBUG
|
||||
#define osfp_log_debug(...) do { } while (0)
|
||||
#else
|
||||
#define osfp_log_debug(...) osfp_log(OSFP_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#endif
|
||||
#define osfp_log_info(...) osfp_log(OSFP_LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define osfp_log_warning(...) osfp_log(OSFP_LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define osfp_log_error(...) osfp_log(OSFP_LOG_LEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__,__VA_ARGS__)
|
||||
|
||||
|
||||
void osfp_log_level_set(osfp_log_level_t level);
|
||||
void osfp_log(unsigned int x, const char *file, const int line, const char *func, const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
553
src/osfp_score_db.c
Normal file
553
src/osfp_score_db.c
Normal file
@@ -0,0 +1,553 @@
|
||||
#include "osfp_common.h"
|
||||
|
||||
#include "osfp.h"
|
||||
#include "osfp_fingerprint.h"
|
||||
#include "osfp_score_db.h"
|
||||
#include "osfp_log.h"
|
||||
|
||||
#define OSFP_PERCENTILE 100
|
||||
|
||||
#define OSFP_SCORE_DB_FIELD_UINT_VALUE_MAX 65536
|
||||
|
||||
struct osfp_score_db_array_data {
|
||||
struct osfp_os_class_score *array_head;
|
||||
unsigned int array_len;
|
||||
};
|
||||
|
||||
struct osfp_score_db_hash_element {
|
||||
char *key;
|
||||
unsigned int keylen;
|
||||
struct osfp_os_class_score *score;
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
struct osfp_score_db_hash_data {
|
||||
struct osfp_score_db_hash_element *hash_head;
|
||||
};
|
||||
|
||||
int osfp_score_db_array_add(void *data, struct osfp_os_class_score *os_class_score, void *value, unsigned int len)
|
||||
{
|
||||
int ret = -1, i;
|
||||
unsigned int index;
|
||||
struct osfp_score_db_array_data *array_data = (struct osfp_score_db_array_data *)data;
|
||||
|
||||
if (array_data == NULL || os_class_score == NULL || value == NULL || len != sizeof(unsigned int)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (array_data->array_head == NULL || array_data->array_len == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
index = *(unsigned int *)value;
|
||||
|
||||
if (index >= array_data->array_len) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
|
||||
array_data->array_head[index].scores[i] += os_class_score->scores[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct osfp_os_class_score *osfp_score_db_array_match(void *data, void *value, unsigned int len)
|
||||
{
|
||||
unsigned int index;
|
||||
struct osfp_score_db_array_data *array_data = (struct osfp_score_db_array_data *)data;
|
||||
|
||||
if (array_data == NULL || value == NULL || len != sizeof(unsigned int)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (array_data->array_head == NULL || array_data->array_len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
index = *(unsigned int *)value;
|
||||
|
||||
if (index >= array_data->array_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &((array_data->array_head)[index]);
|
||||
}
|
||||
|
||||
void *osfp_score_db_array_create(void)
|
||||
{
|
||||
struct osfp_score_db_array_data *array_data = calloc(1, sizeof(struct osfp_score_db_array_data));
|
||||
if (array_data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
array_data->array_head = calloc(OSFP_SCORE_DB_FIELD_UINT_VALUE_MAX, sizeof(struct osfp_os_class_score));
|
||||
if (array_data->array_head == NULL) {
|
||||
free(array_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
array_data->array_len = OSFP_SCORE_DB_FIELD_UINT_VALUE_MAX;
|
||||
|
||||
return (void *)array_data;
|
||||
}
|
||||
|
||||
void osfp_score_db_array_destroy(void *data) {
|
||||
struct osfp_score_db_array_data *array_data = (struct osfp_score_db_array_data *)data;
|
||||
|
||||
if (array_data) {
|
||||
if (array_data->array_head) {
|
||||
free(array_data->array_head);
|
||||
}
|
||||
free(array_data);
|
||||
}
|
||||
}
|
||||
|
||||
int osfp_score_db_hash_add(void *data, struct osfp_os_class_score *os_class_score, void *value, unsigned int len)
|
||||
{
|
||||
int ret = -1, i;
|
||||
struct osfp_score_db_hash_data *hash_data = (struct osfp_score_db_hash_data *)data;
|
||||
struct osfp_score_db_hash_element *element = NULL;
|
||||
|
||||
if (hash_data == NULL || os_class_score == NULL || value == NULL || len == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
HASH_FIND(hh, hash_data->hash_head, value, len, element);
|
||||
if (element == NULL) {
|
||||
element = (struct osfp_score_db_hash_element *)calloc(1, sizeof(struct osfp_score_db_hash_element));
|
||||
if (element == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
element->key = strdup(value);
|
||||
element->keylen = len;
|
||||
element->score = (struct osfp_os_class_score *)calloc(1, sizeof(struct osfp_os_class_score));
|
||||
if (element->score == NULL) {
|
||||
free(element);
|
||||
element = NULL;
|
||||
goto exit;
|
||||
}
|
||||
HASH_ADD_KEYPTR(hh, hash_data->hash_head, element->key, element->keylen, element);
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
|
||||
element->score->scores[i] += os_class_score->scores[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct osfp_os_class_score *osfp_score_db_hash_match(void *data, void *value, unsigned int len)
|
||||
{
|
||||
struct osfp_score_db_hash_data *hash_data = (struct osfp_score_db_hash_data *)data;
|
||||
struct osfp_score_db_hash_element *element = NULL;
|
||||
|
||||
if (data == NULL || value == NULL || len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (hash_data->hash_head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HASH_FIND(hh, hash_data->hash_head, value, len, element);
|
||||
if (element == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return element->score;
|
||||
}
|
||||
|
||||
void *osfp_score_db_hash_create(void)
|
||||
{
|
||||
return (void*)calloc(1, sizeof(struct osfp_score_db_hash_data));
|
||||
}
|
||||
|
||||
void osfp_score_db_hash_destroy(void *data) {
|
||||
struct osfp_score_db_hash_data *hash_data = (struct osfp_score_db_hash_data *)data;
|
||||
struct osfp_score_db_hash_element *element = NULL;
|
||||
struct osfp_score_db_hash_element *tmp = NULL;
|
||||
|
||||
if (hash_data) {
|
||||
if (hash_data->hash_head) {
|
||||
HASH_ITER(hh, hash_data->hash_head, element, tmp) {
|
||||
HASH_DELETE(hh, hash_data->hash_head, element);
|
||||
if (element) {
|
||||
if (element->key) {
|
||||
free(element->key);
|
||||
}
|
||||
if (element->score) {
|
||||
free(element->score);
|
||||
}
|
||||
free(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(hash_data);
|
||||
}
|
||||
}
|
||||
|
||||
struct osfp_os_class_score *osfp_score_db_filed_match(struct osfp_field_score_db *db, void *value, unsigned int len)
|
||||
{
|
||||
if (db == NULL || value == NULL || len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return db->match(db->data, value, len);
|
||||
}
|
||||
|
||||
char *osfp_score_db_read_file(char *fp_file)
|
||||
{
|
||||
int ret = -1;
|
||||
char *file_buffer = NULL;
|
||||
unsigned int file_len = 0;
|
||||
FILE *fp = NULL;
|
||||
struct stat st;
|
||||
|
||||
if (0 > stat(fp_file, &st)) {
|
||||
printf("stat() on '%s' failed.\n", fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (st.st_size == 0) {
|
||||
printf("Empty file: %s.\n", fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
file_len = (unsigned int)st.st_size;
|
||||
file_buffer = malloc(file_len + 1);
|
||||
if (file_buffer == NULL) {
|
||||
printf("Not enough memory for file buffer. file name: %s\n",fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fp = fopen(fp_file, "r");
|
||||
if (fp == NULL) {
|
||||
printf("Cannot open '%s' for reading.\n", fp_file);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = fread(file_buffer, 1, file_len, fp);
|
||||
if (ret < 0) {
|
||||
free(file_buffer);
|
||||
fclose(fp);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
file_buffer[file_len] = 0;
|
||||
|
||||
return file_buffer;
|
||||
exit:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int osfp_score_db_load_field(struct osfp_field_score_db *db, cJSON *field, enum osfp_os_class_id os_class)
|
||||
{
|
||||
int ret = -1;
|
||||
struct osfp_os_class_score os_class_score = {0};
|
||||
|
||||
void *value_ptr;
|
||||
unsigned int value_len;
|
||||
|
||||
switch (field->type) {
|
||||
case cJSON_Number:
|
||||
value_ptr = (void *)&field->valueint;
|
||||
value_len = sizeof(field->valueint);
|
||||
break;
|
||||
case cJSON_String:
|
||||
value_ptr = (void *)field->valuestring;
|
||||
value_len = strlen(field->valuestring) + 1;
|
||||
break;
|
||||
case cJSON_NULL:
|
||||
ret = 0;
|
||||
goto exit;
|
||||
default:
|
||||
goto exit;
|
||||
}
|
||||
|
||||
os_class_score.scores[os_class] = 1;
|
||||
|
||||
ret = db->add(db->data, &os_class_score, value_ptr, value_len);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
db->entry_count++;
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int osfp_score_db_load_entry(struct osfp_score_db *score_db, cJSON *entry)
|
||||
{
|
||||
int ret = -1, i;
|
||||
cJSON *field = NULL;
|
||||
struct osfp_field_score_db *db;
|
||||
enum osfp_os_class_id os_class;
|
||||
|
||||
if (score_db == NULL || entry == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
field = cJSON_GetObjectItem(entry, osfp_fingerprint_get_field_name(OSFP_FIELD_OS));
|
||||
if (field == NULL || field->valuestring == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
os_class = osfp_os_class_name_to_id(field->valuestring);
|
||||
if (os_class >= OSFP_OS_CLASS_MAX) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_FIELD_OS; i++) {
|
||||
db = &score_db->field_score_dbs[i];
|
||||
if (db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (!db->enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
field = cJSON_GetObjectItem(entry, osfp_fingerprint_get_field_name(i));
|
||||
if (field == NULL) {
|
||||
printf("json entry missing field: %s\n%s\n",
|
||||
osfp_fingerprint_get_field_name(i), cJSON_Print(entry));
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = osfp_score_db_load_field(db, field, os_class);
|
||||
if (ret != 0) {
|
||||
printf("json entry field load failed. field: %s\n%s\n",
|
||||
osfp_fingerprint_get_field_name(i), cJSON_Print(entry));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
score_db->entry_count++;
|
||||
score_db->os_class_entry_count[os_class]++;
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int osfp_score_db_load(struct osfp_score_db *score_db, char *fp_file)
|
||||
{
|
||||
int ret = OSFP_EINVAL, i;
|
||||
char *file_buffer;
|
||||
cJSON *root = NULL;
|
||||
cJSON *entry;
|
||||
struct osfp_field_score_db *field_score_db;
|
||||
|
||||
if (score_db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
file_buffer = osfp_score_db_read_file(fp_file);
|
||||
if (file_buffer == NULL) {
|
||||
ret = OSFP_ERR_SCORE_DB_READ_FILE;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
root = cJSON_Parse(file_buffer);
|
||||
if (root == NULL) {
|
||||
ret = OSFP_ERR_SCORE_DB_PARSE_FILE;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
score_db->entry_count = cJSON_GetArraySize(root);
|
||||
|
||||
for (i = 0; i < score_db->entry_count; i++) {
|
||||
entry = cJSON_GetArrayItem(root, i);
|
||||
if (entry) {
|
||||
ret = osfp_score_db_load_entry(score_db, entry);
|
||||
if (ret != 0) {
|
||||
osfp_log_debug("json entry load failed.\n%s\n", cJSON_Print(entry));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_FIELD_MAX; i++) {
|
||||
field_score_db = &score_db->field_score_dbs[i];
|
||||
if (field_score_db->enabled) {
|
||||
score_db->perfect_score += osfp_fingerprint_get_field_importance(i);
|
||||
}
|
||||
}
|
||||
|
||||
ret = OSFP_NOERR;
|
||||
exit:
|
||||
if (root) {
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
if (file_buffer) {
|
||||
free((char*)file_buffer);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int osfp_score_db_score(struct osfp_score_db *score_db, unsigned int flags, struct osfp_fingerprint *fp, struct osfp_os_class_score *result_score)
|
||||
{
|
||||
int ret = OSFP_EINVAL, i, j;
|
||||
|
||||
unsigned int tmp_score;
|
||||
unsigned int perfect_score;
|
||||
unsigned int entry_count;
|
||||
unsigned int importance;
|
||||
|
||||
struct osfp_os_class_score *os_class_score_matched;
|
||||
enum osfp_os_class_id os_class_id;
|
||||
|
||||
struct osfp_fingerprint_field *field;
|
||||
struct osfp_field_score_db *field_score_db;
|
||||
|
||||
if (score_db == NULL || fp == NULL || result_score == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memset(result_score, 0, sizeof(struct osfp_os_class_score));
|
||||
|
||||
perfect_score = score_db->perfect_score;
|
||||
if (perfect_score == 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_FIELD_MAX; i++) {
|
||||
field_score_db = &score_db->field_score_dbs[i];
|
||||
if (!field_score_db->enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
field = &fp->fields[i];
|
||||
if (!field->enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
os_class_score_matched = osfp_score_db_filed_match(field_score_db, field->value, field->value_len);
|
||||
if (os_class_score_matched == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
importance = osfp_fingerprint_get_field_importance(i);
|
||||
|
||||
for (j = 0; j < OSFP_OS_CLASS_MAX; j++) {
|
||||
entry_count = score_db->os_class_entry_count[j];
|
||||
tmp_score = os_class_score_matched->scores[j];
|
||||
if (entry_count == 0 || tmp_score == 0) {
|
||||
continue;
|
||||
}
|
||||
if (0 == flags || flags & OSFP_BIT_U32(j)) {
|
||||
printf("%s %s: ((%d * %u / %u) * %u ) / %u\n", osfp_fingerprint_get_field_name(i), osfp_os_class_id_to_name(j), OSFP_PERCENTILE, importance, perfect_score, os_class_score_matched->scores[j], entry_count);
|
||||
result_score->scores[j] += ((OSFP_PERCENTILE * importance / perfect_score) * os_class_score_matched->scores[j]) / entry_count;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == OSFP_FIELD_TCP_OPTIONS) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return OSFP_NOERR;
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
void osfp_score_db_debug_print(struct osfp_score_db *score_db)
|
||||
{
|
||||
int i;
|
||||
printf("score_db:\n");
|
||||
printf("entry_count: %u\n", score_db->entry_count);
|
||||
printf("perfect_score: %u\n", score_db->perfect_score);
|
||||
|
||||
for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
|
||||
const char *name = osfp_os_class_id_to_name(i);
|
||||
printf("os class %s ", name);
|
||||
|
||||
printf("entry_count: %u\n", score_db->os_class_entry_count[i]);
|
||||
printf("os class %s entry_count: %u\n", osfp_os_class_id_to_name(i), score_db->os_class_entry_count[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_FIELD_MAX; i++) {
|
||||
printf("field %s enabled: %u\n", osfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].enabled);
|
||||
printf("field %s type: %u\n", osfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].type);
|
||||
printf("field %s entry_count: %u\n", osfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].entry_count);
|
||||
printf("field %s enabled: %u\n", osfp_fingerprint_get_field_name(i), score_db->field_score_dbs[i].data);
|
||||
}
|
||||
}
|
||||
|
||||
struct osfp_score_db *osfp_score_db_create(void)
|
||||
{
|
||||
int i;
|
||||
struct osfp_score_db *score_db;
|
||||
struct osfp_field_score_db *db;
|
||||
|
||||
score_db = calloc(1, sizeof(struct osfp_score_db));
|
||||
if (score_db == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (i = 0; i < OSFP_FIELD_MAX; i++) {
|
||||
db = &score_db->field_score_dbs[i];
|
||||
|
||||
db->enabled = osfp_fingerprint_get_field_enabled(i);
|
||||
if (!db->enabled) {
|
||||
osfp_log_warning("field disabled: %s", "");
|
||||
continue;
|
||||
}
|
||||
|
||||
db->type = osfp_fingerprint_get_field_type(i);
|
||||
switch (db->type) {
|
||||
case OSFP_FIELD_TYPE_UINT:
|
||||
db->create = osfp_score_db_array_create;
|
||||
db->destroy = osfp_score_db_array_destroy;
|
||||
db->add = osfp_score_db_array_add;
|
||||
db->match = osfp_score_db_array_match;
|
||||
break;
|
||||
case OSFP_FIELD_TYPE_STRING:
|
||||
db->create = osfp_score_db_hash_create;
|
||||
db->destroy = osfp_score_db_hash_destroy;
|
||||
db->add = osfp_score_db_hash_add;
|
||||
db->match = osfp_score_db_hash_match;
|
||||
break;
|
||||
default:
|
||||
osfp_log_debug("unsupported type: %u", db->type);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
db->data = db->create();
|
||||
if (db->data == NULL) {
|
||||
osfp_log_debug("create failed. field: %s", osfp_fingerprint_get_field_name(i));
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
return score_db;
|
||||
exit:
|
||||
if (score_db) {
|
||||
osfp_score_db_destroy(score_db);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void osfp_score_db_destroy(struct osfp_score_db *score_db)
|
||||
{
|
||||
int i;
|
||||
struct osfp_field_score_db *db;
|
||||
|
||||
if (score_db) {
|
||||
for (i = 0; i < OSFP_FIELD_MAX; i++) {
|
||||
db = &score_db->field_score_dbs[i];
|
||||
db->destroy(db->data);
|
||||
db->data = NULL;
|
||||
}
|
||||
free(score_db);
|
||||
}
|
||||
}
|
||||
35
src/osfp_score_db.h
Normal file
35
src/osfp_score_db.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef __OSFP_SCORE_DB_H__
|
||||
#define __OSFP_SCORE_DB_H__
|
||||
|
||||
#include "osfp.h"
|
||||
#include "osfp_fingerprint.h"
|
||||
|
||||
struct osfp_field_score_db {
|
||||
unsigned int enabled;
|
||||
unsigned int type;
|
||||
unsigned int entry_count;
|
||||
|
||||
void *data;
|
||||
|
||||
void *(*create)(void);
|
||||
void (*destroy)(void *);
|
||||
int (*add)(void *data, struct osfp_os_class_score *, void *, unsigned int);
|
||||
struct osfp_os_class_score *(*match)(void *, void *, unsigned int);
|
||||
};
|
||||
|
||||
struct osfp_score_db {
|
||||
unsigned int entry_count;
|
||||
unsigned int perfect_score;
|
||||
unsigned int os_class_entry_count[OSFP_OS_CLASS_MAX];
|
||||
struct osfp_field_score_db field_score_dbs[OSFP_FIELD_MAX];
|
||||
};
|
||||
|
||||
void osfp_score_db_debug_print(struct osfp_score_db *score_db);
|
||||
|
||||
int osfp_score_db_load(struct osfp_score_db *score_db, char *fp_file);
|
||||
int osfp_score_db_score(struct osfp_score_db *score_db, unsigned int flags, struct osfp_fingerprint *fp, struct osfp_os_class_score *result_score);
|
||||
|
||||
struct osfp_score_db *osfp_score_db_create(void);
|
||||
void osfp_score_db_destroy(struct osfp_score_db *score_db);
|
||||
|
||||
#endif
|
||||
19
test/test.c
Normal file
19
test/test.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <linux/in.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/ipv6.h>
|
||||
#include <linux/tcp.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <pcap.h>
|
||||
|
||||
#include "osfp.h"
|
||||
#include "osfp_fingerprint.h"
|
||||
#include "osfp_score_db.h"
|
||||
Reference in New Issue
Block a user