Esempio n. 1
0
def request_handler(request):
    """
    Handles a Request for descriptions.
    """
    print "REDB: request_handler called"

    unpickled_request = redb_server_com.Request()
    unpickled_request.from_json(request.FILES['pickled_request'].read())

    # Adding more graph attributes:
    unpickled_request.matching_grade_attributes = \
        _process_matching_grade_attrs(unpickled_request.\
                                      matching_grade_attributes)

    # Building a temporary function
    second_function = Function()

    second_function.primary_attributes = \
        json.dumps(unpickled_request.primary_attributes)
    second_function.filtering_attributes = \
        json.dumps(unpickled_request.filtering_attributes)
    second_function.matching_grade_attributes = \
        json.dumps(unpickled_request.matching_grade_attributes)

    second_function.func_md5 = \
        unpickled_request.primary_attributes["_func_md5_attr"]
    second_function.exe_md5 = \
        unpickled_request.primary_attributes["_exe_md5_attr"]
    second_function.exe_name = \
        unpickled_request.primary_attributes["_exe_name_attr"]
    second_function.first_addr = \
        unpickled_request.primary_attributes["_first_addr_attr"]
    second_function.ins_num = \
        unpickled_request.primary_attributes["_ins_num_attr"]

    num_req_descs = unpickled_request.num_of_returned_comments
    if num_req_descs <= 0 or num_req_descs > MAX_REQ_CMTS:
        return _error_http_response("Error: required descriptions" +
                                    "number not in range")

    filtered_functions = []

    for function_obj in Function.objects.all():
        filter_grade = redb_similarity_grading.similarity_grading().\
            filter_grade(function_obj, second_function)
        if filter_grade >= redb_similarity_grading.FILTERING_THRESHOLD:
            filtered_functions.append(function_obj)

    function_grade_pairs = []
    for filtered_function in filtered_functions:
        matching_grade = redb_similarity_grading.similarity_grading().\
            matching_grade(filtered_function, second_function)
        if matching_grade >= redb_similarity_grading.MATCHING_THRESHOLD:
            pair = (filtered_function, matching_grade)
            function_grade_pairs.append(pair)

    sorted_functions = sorted(function_grade_pairs, key=lambda func: func[1])
    sorted_functions.reverse()

    suggested_descriptions = []

    for func, grade in sorted_functions:
        if len(suggested_descriptions) == num_req_descs:
            break
        fitting_descriptions = Description.objects.filter(function=func)
        for desc in fitting_descriptions:
            func_name_and_cmts = json.loads(desc.func_name_and_cmts,
                                            object_hook=_decode_dict)
            suggested_description = \
                SuggestedDecsription(func_name_and_cmts=func_name_and_cmts,
                                     matching_grade=grade,
                                     can_be_embedded=(func.ins_num ==
                                                      second_function.ins_num),
                                     date=desc.date)

            suggested_description_dict = suggested_description.to_dict()
            suggested_descriptions.append(suggested_description_dict)
            if len(suggested_descriptions) == num_req_descs:
                break

    response = redb_server_com.Response(\
                        suggested_descriptions_list=suggested_descriptions)
    response = response.to_json()

    http_response = HttpResponse(response)
    print "REDB: request_handler finished"
    return http_response
Esempio n. 2
0
def request_handler(request):
    """
    Handles a Request for descriptions.
    """
    print "REDB: request_handler called"

    unpickled_request = redb_server_com.Request()
    unpickled_request.from_json(request.FILES['pickled_request'].read())

    # Adding more graph attributes:
    unpickled_request.matching_grade_attributes = \
        _process_matching_grade_attrs(unpickled_request.\
                                      matching_grade_attributes)

    # Building a temporary function
    second_function = Function()

    second_function.primary_attributes = \
        json.dumps(unpickled_request.primary_attributes)
    second_function.filtering_attributes = \
        json.dumps(unpickled_request.filtering_attributes)
    second_function.matching_grade_attributes = \
        json.dumps(unpickled_request.matching_grade_attributes)

    second_function.func_md5 = \
        unpickled_request.primary_attributes["_func_md5_attr"]
    second_function.exe_md5 = \
        unpickled_request.primary_attributes["_exe_md5_attr"]
    second_function.exe_name = \
        unpickled_request.primary_attributes["_exe_name_attr"]
    second_function.first_addr = \
        unpickled_request.primary_attributes["_first_addr_attr"]
    second_function.ins_num = \
        unpickled_request.primary_attributes["_ins_num_attr"]

    num_req_descs = unpickled_request.num_of_returned_comments
    if num_req_descs <= 0 or num_req_descs > MAX_REQ_CMTS:
        return _error_http_response("Error: required descriptions" +
                                   "number not in range")

    filtered_functions = []

    for function_obj in Function.objects.all():
        filter_grade = redb_similarity_grading.similarity_grading().\
            filter_grade(function_obj, second_function)
        if filter_grade >= redb_similarity_grading.FILTERING_THRESHOLD:
            filtered_functions.append(function_obj)

    function_grade_pairs = []
    for filtered_function in filtered_functions:
        matching_grade = redb_similarity_grading.similarity_grading().\
            matching_grade(filtered_function, second_function)
        if matching_grade >= redb_similarity_grading.MATCHING_THRESHOLD:
            pair = (filtered_function, matching_grade)
            function_grade_pairs.append(pair)

    sorted_functions = sorted(function_grade_pairs, key=lambda func: func[1])
    sorted_functions.reverse()

    suggested_descriptions = []

    for func, grade in sorted_functions:
        if len(suggested_descriptions) == num_req_descs:
            break
        fitting_descriptions = Description.objects.filter(function=func)
        for desc in fitting_descriptions:
            func_name_and_cmts = json.loads(desc.func_name_and_cmts,
                                            object_hook=_decode_dict)
            suggested_description = \
                SuggestedDecsription(func_name_and_cmts=func_name_and_cmts,
                                     matching_grade=grade,
                                     can_be_embedded=(func.ins_num ==
                                                      second_function.ins_num),
                                     date=desc.date)

            suggested_description_dict = suggested_description.to_dict()
            suggested_descriptions.append(suggested_description_dict)
            if len(suggested_descriptions) == num_req_descs:
                break

    response = redb_server_com.Response(\
                        suggested_descriptions_list=suggested_descriptions)
    response = response.to_json()

    http_response = HttpResponse(response)
    print "REDB: request_handler finished"
    return http_response