static PyObject * pydeep_hash_buf(PyObject *self, PyObject *args){ PyObject *inputStringBuffer = NULL; PyObject *ssdeepHash= NULL; Py_ssize_t stringSize = 0; char *inputBuffer=NULL; char *hashResult; int ret; if (!PyArg_ParseTuple(args, "s#", &inputBuffer, &stringSize)){ return NULL; } // Allocate return buffers for hash hashResult = (char *)malloc(FUZZY_MAX_RESULT); if (hashResult == NULL){ PyErr_SetString(pydeepError, "Error allocating malloc buffer"); return NULL; } inputStringBuffer = PyString_FromStringAndSize( inputBuffer, stringSize); ret = fuzzy_hash_buf((unsigned char*)inputBuffer, (uint32_t)stringSize, hashResult); if (ret !=0 ){ free(hashResult); PyErr_SetString(pydeepError, "Error in fuzzy_hash!"); return NULL; } ssdeepHash = PyString_FromString( hashResult ); free(hashResult); return ssdeepHash; }
static PyObject * ssdeep_fuzzy_hash_buf(PyObject *self, PyObject *args) { char *result; /* ssdeep hash */ unsigned char *buff; /* function argument */ int buffsize; /* length of function argument (may contain \0) */ PyObject *return_value; /* create a python object before return to allow for free */ /* parse out the function argument containting the buffer that should be * hashed by ssdeep. */ if (!PyArg_ParseTuple(args, "s#", &buff, &buffsize)) { PyErr_SetObject(PyExc_ValueError, Py_BuildValue("s", "Unable to parse string buffer from argument list")); return NULL; } if ((result = malloc(FUZZY_MAX_RESULT)) == NULL) { PyErr_NoMemory(); return NULL; } /* compute the ssdeep hash from buffer */ if (fuzzy_hash_buf(buff, buffsize, result) != 0) { PyErr_SetObject(PyExc_ValueError, Py_BuildValue("s", "Unable to compute hash")); free(result); return NULL; } /* build a python string and free up result buffer. */ return_value = Py_BuildValue("s", result); free(result); return return_value; }
static PyObject * _cffi_f_fuzzy_hash_buf(PyObject *self, PyObject *args) { unsigned char const * x0; uint32_t x1; char * x2; Py_ssize_t datasize; int result; PyObject *arg0; PyObject *arg1; PyObject *arg2; if (!PyArg_ParseTuple(args, "OOO:fuzzy_hash_buf", &arg0, &arg1, &arg2)) return NULL; datasize = _cffi_prepare_pointer_call_argument( _cffi_type(5), arg0, (char **)&x0); if (datasize != 0) { if (datasize < 0) return NULL; x0 = alloca(datasize); memset((void *)x0, 0, datasize); if (_cffi_convert_array_from_object((char *)x0, _cffi_type(5), arg0) < 0) return NULL; } x1 = _cffi_to_c_int(arg1, uint32_t); if (x1 == (uint32_t)-1 && PyErr_Occurred()) return NULL; datasize = _cffi_prepare_pointer_call_argument( _cffi_type(2), arg2, (char **)&x2); if (datasize != 0) { if (datasize < 0) return NULL; x2 = alloca(datasize); memset((void *)x2, 0, datasize); if (_cffi_convert_array_from_object((char *)x2, _cffi_type(2), arg2) < 0) return NULL; } Py_BEGIN_ALLOW_THREADS _cffi_restore_errno(); { result = fuzzy_hash_buf(x0, x1, x2); } _cffi_save_errno(); Py_END_ALLOW_THREADS return _cffi_from_c_int(result, int); }
static void calc_hash(const char *alg_name, const unsigned char *data, size_t size, char *output) { if (strcmp("ssdeep", alg_name) == 0) { fuzzy_hash_buf(data, size, output); return; } const EVP_MD *md = EVP_get_digestbyname(alg_name); //assert(md != NULL); // We already checked this in parse_hash_algorithm() unsigned char md_value[EVP_MAX_MD_SIZE]; unsigned int md_len; EVP_MD_CTX md_ctx; // FIXME: Handle errors - Check return values. EVP_MD_CTX_init(&md_ctx); EVP_DigestInit_ex(&md_ctx, md, NULL); EVP_DigestUpdate(&md_ctx, data, size); EVP_DigestFinal_ex(&md_ctx, md_value, &md_len); EVP_MD_CTX_cleanup(&md_ctx); for (unsigned int i=0; i < md_len; i++) sprintf(&output[i * 2], "%02x", md_value[i]); }
int main(int argc, char **argv) { unsigned char * buf; char * result, * result2; FILE *handle; srand(1); buf = (unsigned char *)malloc(SIZE); result = (char *)malloc(FUZZY_MAX_RESULT); result2 = (char *)malloc(FUZZY_MAX_RESULT); if (NULL == result || NULL == buf || NULL == result2) { fprintf (stderr,"%s: Out of memory\n", argv[0]); return EXIT_FAILURE; } generate_random(buf,SIZE); if (write_data(buf,SIZE,FILENAME)) return EXIT_FAILURE; printf ("Hashing buffer\n"); int status = fuzzy_hash_buf(buf,SIZE,result); if (status) printf ("Error during buf hash\n"); else printf ("%s\n", result); handle = fopen(FILENAME,"rb"); if (NULL == handle) { perror(FILENAME); return EXIT_FAILURE; } printf ("Hashing file\n"); status = fuzzy_hash_file(handle,result); if (status) printf ("Error during file hash\n"); else printf ("%s\n", result); fclose(handle); printf ("Modifying buffer and comparing to file\n"); int i; for (i = 0x100 ; i < 0x110 ; ++i) buf[i] = 37; status = fuzzy_hash_buf(buf,SIZE,result2); if (status) printf ("Error during buffer hash\n"); else printf ("%s\n", result2); i = fuzzy_compare(result,result2); if (-1 == i) printf ("An error occured during matching\n"); else { if (i != 0) printf ("MATCH: score = %d\n", i); else printf ("did not match\n"); } return EXIT_SUCCESS; }