Skip to content

Commit 85aa880

Browse files
davidmcgrewGitHub Enterprise
authored andcommitted
Merge pull request #504 from network-intelligence/fdc-json-fix
Fixing `get_json_decoded_fdc()` to no longer assume UTF-8 strings in `cbor::text_string` elements
2 parents d3cd14b + 6234ca3 commit 85aa880

File tree

5 files changed

+33
-44
lines changed

5 files changed

+33
-44
lines changed

doc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# CHANGELOG for Mercury
22

3+
* Fixed `get_json_decoded_fdc()` to not assume UTF-8 inputs.
34
* Added CBOR encoding/decoding for SSH and STUN fingerprints.
45
* Fixing compiler warnings related to ABI differences
56
* CMake changes required to add xsimd as submodule and fixing

src/cbor.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,10 @@ int main(int argc, char *argv[]) {
8787
}
8888
}
8989
if (decode_fdc) {
90-
static const size_t MAX_FP_STR_LEN = 8192;
91-
char fp_str[MAX_FP_STR_LEN];
92-
char dst_ip_str[MAX_ADDR_STR_LEN];
93-
char sn_str[MAX_SNI_LEN];
94-
char ua_str[MAX_USER_AGENT_LEN];
95-
uint16_t dst_port;
96-
uint64_t truncation;
97-
98-
bool ok = fdc::decode(input,
99-
writeable{(uint8_t*)fp_str, MAX_FP_STR_LEN},
100-
writeable{(uint8_t*)sn_str, MAX_SNI_LEN},
101-
writeable{(uint8_t*)dst_ip_str, MAX_ADDR_STR_LEN},
102-
dst_port,
103-
writeable{(uint8_t*)ua_str, MAX_USER_AGENT_LEN},
104-
truncation);
105-
if (ok) {
106-
fprintf(stdout, "{\"fdc\":{");
107-
fprintf(stdout, "\"fingerprint\": \"%s\",", fp_str);
108-
fprintf(stdout, "\"sni\": \"%s\",", sn_str);
109-
fprintf(stdout, "\"dst_ip_str\": \"%s\",", dst_ip_str);
110-
fprintf(stdout, "\"dst_port\": %u,", dst_port);
111-
fprintf(stdout, "\"user-agent\": \"%s\",", ua_str);
112-
fprintf(stdout, "\"truncation\": %lu", truncation);
113-
fprintf(stdout, "}}\n");
90+
91+
std::string json_string = get_json_decoded_fdc((const char *)input.data, input.length());
92+
if (json_string != "") {
93+
fprintf(stdout , "%s\n", json_string.c_str());
11494
} else {
11595
fprintf(stderr, "error: could not decode FDC\n");
11696
}

src/libmerc/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ set(cbor_src
318318
../cbor.cpp
319319
)
320320
add_executable(cbor ${cbor_src})
321-
target_compile_options(cbor PUBLIC ${LIBMERC_COMPILE_OPT})
322-
target_link_libraries(cbor ZLIB::ZLIB)
321+
target_compile_options(cbor PUBLIC ${LIBMERC_COMPILE_OPT} -UDONT_USE_STDERR)
322+
target_link_libraries(cbor ZLIB::ZLIB )
323323
# }
324324

325325

src/libmerc/fdc.hpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ class fdc {
739739
//
740740
const char *tls_fp = "tls/1/(0301)(c014c00a00390038c00fc0050035c012c00800160013c00dc003000ac013c00900330032c00ec004002fc011c007c00cc002000500040015001200090014001100080006000300ff)[(0000)(000a00340032000100020003000400050006000700080009000a000b000c000d000e000f0010001100120013001400150016001700180019)(000b000403000102)(0023)]";
741741
const char *http_fp = "http/(434f4e4e454354)(485454502f312e31)((486f7374)(557365722d4167656e74))";
742-
static constexpr size_t num_tests = 4;
742+
static constexpr size_t num_tests = 5;
743743
fdc fdc_object[num_tests]{
744744
{
745745
datum{tls_fp},
@@ -772,7 +772,15 @@ class fdc {
772772
"72.163.217.105",
773773
80,
774774
truncation_status::reassembled_truncated
775-
}
775+
},
776+
{
777+
datum{http_fp},
778+
"user-agent with utf8: stra\u00DFe \r\n\"",
779+
"abc.com",
780+
"72.163.217.105",
781+
80,
782+
truncation_status::reassembled_truncated
783+
},
776784
};
777785
for (size_t i = 0; i < num_tests; i++){
778786

@@ -785,16 +793,15 @@ class fdc {
785793

786794
// decode the data in the buffer to decoded_fdc
787795
//
788-
static const size_t MAX_FP_STR_LEN = 8192;
789-
char fp_str[MAX_FP_STR_LEN];
796+
char fp_str[fingerprint::MAX_FP_STR_LEN];
790797
char dst_ip_str[MAX_ADDR_STR_LEN];
791798
char sn_str[MAX_SNI_LEN];
792799
char ua_str[MAX_USER_AGENT_LEN];
793800
uint16_t dst_port;
794801
uint64_t truncation;
795802

796803
bool decoding_ok = fdc::decode(encoded_fdc,
797-
writeable{(uint8_t*)fp_str, MAX_FP_STR_LEN},
804+
writeable{(uint8_t*)fp_str, fingerprint::MAX_FP_STR_LEN},
798805
writeable{(uint8_t*)sn_str, MAX_SNI_LEN},
799806
writeable{(uint8_t*)dst_ip_str, MAX_ADDR_STR_LEN},
800807
dst_port,
@@ -840,42 +847,41 @@ class fdc {
840847

841848
[[maybe_unused]] static std::string get_json_decoded_fdc(const char *fdc_blob, ssize_t blob_len) {
842849
datum fdc_data = datum{(uint8_t*)fdc_blob,(uint8_t*)(fdc_blob+blob_len)};
843-
static const size_t MAX_FP_STR_LEN = 8192;
844-
char fp_str[MAX_FP_STR_LEN];
850+
char fp_str[fingerprint::MAX_FP_STR_LEN];
845851
char dst_ip_str[MAX_ADDR_STR_LEN];
846852
char sn_str[MAX_SNI_LEN];
847853
char ua_str[MAX_USER_AGENT_LEN];
848854
uint16_t dst_port;
849855
uint64_t truncation;
850856

851-
char buffer[8192];
857+
char buffer[10240];
852858
struct buffer_stream buf_json(buffer, sizeof(buffer));
853859
struct json_object record(&buf_json);
854860

855861
bool ok = fdc::decode(fdc_data,
856-
writeable{(uint8_t*)fp_str, MAX_FP_STR_LEN},
862+
writeable{(uint8_t*)fp_str, fingerprint::MAX_FP_STR_LEN},
857863
writeable{(uint8_t*)sn_str, MAX_SNI_LEN},
858864
writeable{(uint8_t*)dst_ip_str, MAX_ADDR_STR_LEN},
859865
dst_port,
860866
writeable{(uint8_t*)ua_str, MAX_USER_AGENT_LEN},
861867
truncation);
862868
if (ok) {
863869
json_object fdc_json(record,"fdc");
864-
fdc_json.print_key_string("fingerprint",fp_str);
865-
fdc_json.print_key_string("sni",sn_str);
866-
fdc_json.print_key_string("dst_ip_str",dst_ip_str);
867-
fdc_json.print_key_int("dst_port",dst_port);
868-
fdc_json.print_key_string("user_agent",ua_str);
869-
fdc_json.print_key_string("truncation",get_truncation_str(((truncation_status)truncation)));
870+
fdc_json.print_key_string("fingerprint", fp_str);
871+
fdc_json.print_key_json_string("sni", datum{sn_str});
872+
fdc_json.print_key_json_string("dst_ip_str", datum{dst_ip_str});
873+
fdc_json.print_key_int("dst_port", dst_port);
874+
fdc_json.print_key_json_string("user_agent", datum{ua_str});
875+
fdc_json.print_key_string("truncation", get_truncation_str(((truncation_status)truncation)));
870876
fdc_json.close();
871877
record.close();
872878
buf_json.write_char('\0'); // null terminate
873879
return buf_json.get_string();
874-
880+
875881
} else {
876882
return "";
877883
}
878-
884+
879885
}
880886

881887

src/libmerc/fingerprint.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include "libmerc.h" // for fingerprint_type
1212

1313
class fingerprint {
14-
enum fingerprint_type type;
14+
public:
1515
static const size_t MAX_FP_STR_LEN = 8192;
16+
private:
17+
enum fingerprint_type type;
1618
char fp_str[MAX_FP_STR_LEN];
1719
struct buffer_stream fp_buf;
1820

0 commit comments

Comments
 (0)