@@ -31,7 +31,7 @@ from cython.operator import dereference
3131# CC=g++ CXX=g++ python setup.py install
3232
3333# TODO: actually handle version
34- __version__ = ' 2.7.0 '
34+ __version__ = ' 2.7.1 '
3535
3636# imports from mercury's dns
3737cdef extern from " ../libmerc/dns.h" :
@@ -116,10 +116,10 @@ cdef extern from "../libmerc/analysis.h":
116116 cdef cppclass classifier:
117117 analysis_result perform_analysis(const char * fp_str, const char * server_name, const char * dst_ip, uint16_t dst_port, const char * user_agent)
118118
119- # analysis_result perform_analysis_with_weights(const char *fp_str, const char *server_name, const char *dst_ip, uint16_t dst_port, const char *user_agent,
120- # long double new_as_weight, long double new_domain_weight,
121- # long double new_port_weight, long double new_ip_weight,
122- # long double new_sni_weight, long double new_ua_weight)
119+ analysis_result perform_analysis_with_weights(const char * fp_str, const char * server_name, const char * dst_ip, uint16_t dst_port, const char * user_agent,
120+ double new_as_weight, double new_domain_weight,
121+ double new_port_weight, double new_ip_weight,
122+ double new_sni_weight, double new_ua_weight)
123123
124124
125125cdef extern from " ../libmerc/watchlist.hpp" :
@@ -435,73 +435,72 @@ cdef class Mercury:
435435
436436 return result
437437
438- # cpdef dict perform_analysis_with_weights(self, str fp_str, str server_name, str dst_ip, int dst_port, str user_agent,
439- # long double new_as_weight, long double new_domain_weight,
440- # long double new_port_weight, long double new_ip_weight,
441- # long double new_sni_weight, long double new_ua_weight):
442- # """
443- # Directly call into mercury analysis functionality by providing all needed data features. Additionally,
444- # supply custom weights for each data feature.
445- #
446- # :param fp_str: mercury-generated network protocol fingerprint
447- # :type fp_str: str
448- # :param server_name: The visible, fully qualified domain name, found in the server_name extension or the HTTP Host field
449- # :type server_name: str
450- # :param dst_ip: The destination IP address associated with the packet of interest
451- # :type dst_ip: str
452- # :param dst_port: The destination port associated with the packet of interest
453- # :type dst_port: int
454- # :param user_agent: If analyzing an HTTP packet, provide the contents of the HTTP User-Agent field
455- # :type user_agent: str
456- # :param new_as_weight: Updated weight for the Autonomous System data feature
457- # :type new_as_weight: long double
458- # :param new_domain_weight: Updated weight for the domain name data feature
459- # :type new_domain_weight: long double
460- # :param new_port_weight: Updated weight for the destination port data feature
461- # :type new_port_weight: long double
462- # :param new_ip_weight: Updated weight for the destination IP address data feature
463- # :type new_ip_weight: long double
464- # :param new_sni_weight: Updated weight for the server_name data feature
465- # :type new_sni_weight: long double
466- # :param new_ua_weight: Updated weight for the User-Agent data feature
467- # :type new_ua_weight: long double
468- # :return: JSON-encoded analysis output
469- # :rtype: dict
470- # """
471- # if not self.do_analysis:
472- # print(f'error: classifier not loaded (is do_analysis set to True?)')
473- # return None
474- #
475- # cdef bytes fp_str_b = fp_str.encode()
476- # cdef char* fp_str_c = fp_str_b
477- # cdef bytes server_name_b = server_name.encode()
478- # cdef char* server_name_c = server_name_b
479- # cdef bytes dst_ip_b = dst_ip.encode()
480- # cdef char* dst_ip_c = dst_ip_b
481- # if user_agent == None:
482- # user_agent = 'None'
483- # cdef bytes user_agent_b = user_agent.encode()
484- # cdef char* user_agent_c = user_agent_b
485- # if user_agent == 'None':
486- # user_agent_c = NULL
487- #
488- # cdef analysis_result ar = self.clf.perform_analysis_with_weights(fp_str_c, server_name_c, dst_ip_c, dst_port, user_agent_c,
489- # new_as_weight, new_domain_weight, new_port_weight,
490- # new_ip_weight, new_sni_weight, new_ua_weight)
491- #
492- # cdef fingerprint_status fp_status_enum = ar.status
493- # fp_status = fp_status_dict[fp_status_enum]
494- #
495- # cdef dict result = {}
496- # result['fingerprint_info'] = {}
497- # result['fingerprint_info']['status'] = fp_status
498- # result['analysis'] = {}
499- # result['analysis']['process'] = ar.max_proc.decode('UTF-8')
500- # result['analysis']['score'] = ar.max_score
501- # result['analysis']['malware'] = ar.max_mal
502- # result['analysis']['p_malware'] = ar.malware_prob
503- #
504- # return result
438+ cpdef dict perform_analysis_with_weights(self , str fp_str, str server_name, str dst_ip, int dst_port, str user_agent,
439+ double new_as_weight, double new_domain_weight,
440+ double new_port_weight, double new_ip_weight,
441+ double new_sni_weight, double new_ua_weight):
442+ """
443+ Directly call into mercury analysis functionality by providing all needed data features. Additionally,
444+ supply custom weights for each data feature.
445+
446+ :param fp_str: mercury-generated network protocol fingerprint
447+ :type fp_str: str
448+ :param server_name: The visible, fully qualified domain name, found in the server_name extension or the HTTP Host field
449+ :type server_name: str
450+ :param dst_ip: The destination IP address associated with the packet of interest
451+ :type dst_ip: str
452+ :param dst_port: The destination port associated with the packet of interest
453+ :type dst_port: int
454+ :param user_agent: If analyzing an HTTP packet, provide the contents of the HTTP User-Agent field
455+ :type user_agent: str
456+ :param new_as_weight: Updated weight for the Autonomous System data feature
457+ :type new_as_weight: long double
458+ :param new_domain_weight: Updated weight for the domain name data feature
459+ :type new_domain_weight: long double
460+ :param new_port_weight: Updated weight for the destination port data feature
461+ :type new_port_weight: long double
462+ :param new_ip_weight: Updated weight for the destination IP address data feature
463+ :type new_ip_weight: long double
464+ :param new_sni_weight: Updated weight for the server_name data feature
465+ :type new_sni_weight: long double
466+ :param new_ua_weight: Updated weight for the User-Agent data feature
467+ :type new_ua_weight: long double
468+ :return: JSON-encoded analysis output
469+ :rtype: dict
470+ """
471+ if not self .do_analysis:
472+ print (f' error: classifier not loaded (is do_analysis set to True?)' )
473+ return None
474+
475+ cdef bytes fp_str_b = fp_str.encode()
476+ cdef char * fp_str_c = fp_str_b
477+ cdef bytes server_name_b = server_name.encode()
478+ cdef char * server_name_c = server_name_b
479+ cdef bytes dst_ip_b = dst_ip.encode()
480+ cdef char * dst_ip_c = dst_ip_b
481+ if user_agent == None :
482+ user_agent = ' None'
483+ cdef bytes user_agent_b = user_agent.encode()
484+ cdef char * user_agent_c = user_agent_b
485+
486+ cdef analysis_result ar = self .clf.perform_analysis_with_weights(
487+ fp_str_c, server_name_c, dst_ip_c, dst_port, user_agent_c,
488+ new_as_weight, new_domain_weight, new_port_weight,
489+ new_ip_weight, new_sni_weight, new_ua_weight)
490+
491+ cdef fingerprint_status fp_status_enum = ar.status
492+ fp_status = fp_status_dict[fp_status_enum]
493+
494+ cdef dict result = {}
495+ result[' fingerprint_info' ] = {}
496+ result[' fingerprint_info' ][' status' ] = fp_status
497+ result[' analysis' ] = {}
498+ result[' analysis' ][' process' ] = ar.max_proc.decode(' UTF-8' )
499+ result[' analysis' ][' score' ] = ar.max_score
500+ result[' analysis' ][' malware' ] = ar.max_mal
501+ result[' analysis' ][' p_malware' ] = ar.malware_prob
502+
503+ return result
505504
506505 cdef list extract_attributes(self , analysis_result ar):
507506 cdef char tags_buf[8192 ]
@@ -551,8 +550,6 @@ cdef class Mercury:
551550 user_agent = ' None'
552551 cdef bytes user_agent_b = user_agent.encode()
553552 cdef char * user_agent_c = user_agent_b
554- if user_agent == ' None' :
555- user_agent_c = NULL
556553
557554 cdef analysis_result ar = self .clf.perform_analysis(fp_str_c, server_name_c, dst_ip_c, dst_port, user_agent_c)
558555
0 commit comments