diff --git a/Mrida/.vs/mrida/v15/.suo b/Mrida/.vs/mrida/v15/.suo index 4e688f7..407e3ec 100644 Binary files a/Mrida/.vs/mrida/v15/.suo and b/Mrida/.vs/mrida/v15/.suo differ diff --git a/Mrida/Mrida/Mrida.vcxproj.filters b/Mrida/Mrida/Mrida.vcxproj.filters index 3794e49..18a2f24 100644 --- a/Mrida/Mrida/Mrida.vcxproj.filters +++ b/Mrida/Mrida/Mrida.vcxproj.filters @@ -144,7 +144,7 @@ Header Files\trend - Header Files + Header Files\database \ No newline at end of file diff --git a/Mrida/Mrida/mrida.cpp b/Mrida/Mrida/mrida.cpp index e8cb433..5e4bb16 100644 --- a/Mrida/Mrida/mrida.cpp +++ b/Mrida/Mrida/mrida.cpp @@ -226,7 +226,7 @@ int main(int argc, char** argv) long min_size = std::stoll(req.get_param_value("min_size")); long max_size = std::stoll(req.get_param_value("max_size")); std::string type = req.get_param_value("type"); - trendcpp tlsh; + threat_database tlsh; long id = tlsh.matching_hash_from_threat_db(tlsh_hash, type, min_size, max_size); res.set_content(return_json(id), "application/json"); } @@ -239,6 +239,21 @@ int main(int argc, char** argv) res.set_content(send_success_response(), "application/json"); }); + // Add threat + server.Post("/add_threat", [](const httplib::Request& req, httplib::Response& res) { + std::string threat_hash = req.get_param_value("hash"); + std::string name = req.get_param_value("name"); + unsigned long size = std::stol(req.get_param_value("size")); + std::string type = req.get_param_value("type"); + threat_database database; + database.add_threat_to_database(threat_hash, name, size, type); + print_terminal_info(); + set_terminal_color(CYAN); + std::cout << "ADDED HASH\n"; + set_terminal_color(); + res.set_content(send_success_response(), "application/json"); + }); + print_terminal_info(); std::cout << "Server started on: " << "127.0.0.1:" << 5660 << "\n"; server.listen("127.0.0.1", 5660); diff --git a/Mrida/Mrida/threat_database.cpp b/Mrida/Mrida/threat_database.cpp index 5ead1b6..96230a8 100644 --- a/Mrida/Mrida/threat_database.cpp +++ b/Mrida/Mrida/threat_database.cpp @@ -10,6 +10,7 @@ threat_database::threat_database() { + } @@ -92,3 +93,63 @@ void threat_database::refactor() // Reset the terminal color to original set_terminal_color(); } + +void threat_database::add_threat_to_database(std::string tlsh_hash, std::string threat_name, unsigned long file_size, std::string file_type) +{ + unsigned int file_type_id = mime_to_id(file_type); + try { + sqlite::database _threat_database("threat_db.db"); + _threat_database << "create table if not exists threat(id unsigned bigint primary key, threat_hash text, threat_name text, threat_size unsigned int, threat_type unsigned int);"; + unsigned long max_id = 0; + _threat_database << "select max(id) from threat" >> max_id; + max_id++; + _threat_database << "insert into threat(id, threat_hash, threat_name, threat_size, threat_type) values(?, ?, ?, ?, ?)" << max_id << tlsh_hash << threat_name << file_size << file_type_id; + } + catch (std::exception &e) + { + std::cout << e.what(); + } +} + + +unsigned int threat_database::mime_to_id(std::string mime_type) +{ + + sqlite::database db("threat_db.db"); + db << "create table if not exists mime_table(mime text, id int)"; + int count = 0; + db << "select count(id) from mime_table where mime=?" << mime_type >> count; + int max = 0; + db << "select max(id) from mime_table limit 1" >> max; + if (count == 0) + { + max++; + db << "insert into mime_table(mime, id) values(?, ?)" << mime_type << max; + return max; + } + else + { + unsigned int id; + db << "select id from mime_table where mime=? limit 1" << mime_type >> id; + return id; + } + return 0; +} + +long threat_database::matching_hash_from_threat_db(std::string tlsh_hash, std::string file_type, long file_size_minimum, unsigned long file_size_maximum) +{ + long matched_id = -1; + sqlite::database threat_table("threat_db.db"); + threat_table << "create table if not exists threat(id unsigned bigint primary key, threat_hash text, threat_name text, threat_size unsigned int, threat_type unsigned int);"; + unsigned int file_id = mime_to_id(file_type); + threat_table << "select id, threat_hash from threat where threat_size>=? and threat_size<=? and threat_type=?" + << file_size_minimum << file_size_maximum << file_id >> [&](unsigned long id, std::string threat_hash) + { + trendcpp trend; + if (trend.similarity_distance(tlsh_hash, threat_hash) < 20) + { + matched_id = id; + } + }; + return matched_id; +} diff --git a/Mrida/Mrida/threat_database.h b/Mrida/Mrida/threat_database.h index 229f4da..8a5d06f 100644 --- a/Mrida/Mrida/threat_database.h +++ b/Mrida/Mrida/threat_database.h @@ -1,6 +1,7 @@ // SWAMI KARUPPASWAMI THUNNAI #pragma once +#include "httplib.h" #include #include @@ -12,5 +13,14 @@ class threat_database // Refactor the threat database -- will remove duplicates in the threat database void refactor(); + + // Bool add threat to database + void add_threat_to_database(std::string tlsh_hash, std::string threat_name, unsigned long file_size, std::string file_type); + + // Mime Type to Id + unsigned int mime_to_id(std::string mime_type); + + // Will get the similar hash matching id from threat db [returns -1 if nothing is matching] + long matching_hash_from_threat_db(std::string tlsh_hash, std::string file_type, long file_size_minimum, unsigned long file_size_maximum); }; diff --git a/Mrida/Mrida/threat_db.db b/Mrida/Mrida/threat_db.db index 7346dcb..977a608 100644 Binary files a/Mrida/Mrida/threat_db.db and b/Mrida/Mrida/threat_db.db differ diff --git a/Mrida/Mrida/trendcpp.cpp b/Mrida/Mrida/trendcpp.cpp index d5e6d1e..84b1994 100644 --- a/Mrida/Mrida/trendcpp.cpp +++ b/Mrida/Mrida/trendcpp.cpp @@ -160,19 +160,6 @@ const Tlsh * trendcpp::hash_file(std::string file_location) return &th; } -void trendcpp::add_threat_to_database(unsigned long int id, std::string tlsh_hash, std::string threat_name, unsigned long file_size, unsigned int file_type) -{ - try { - sqlite::database threat_database("threat_db.db"); - threat_database << "create table if not exists threat(id unsigned bigint primary key, threat_hash text, threat_name text, threat_size unsigned int, threat_type unsigned int);"; - threat_database << "insert into threat(id, threat_hash, threat_name, threat_size, threat_type) values(?, ?, ?, ?, ?)" << id << tlsh_hash << threat_name << file_size << file_type; - } - catch (std::exception &e) - { - std::cout << e.what(); - } -} - int trendcpp::similarity_distance(std::string hash_one, std::string hash_two) { Tlsh t1; @@ -185,43 +172,3 @@ int trendcpp::similarity_distance(std::string hash_one, std::string hash_two) return t1.totalDiff(&t2); } -unsigned int trendcpp::mime_to_id(std::string mime_type) -{ - - sqlite::database db("threat_db.db"); - db << "create table if not exists mime_table(mime text, id int)"; - int count = 0; - db << "select count(id) from mime_table where mime=?" << mime_type >> count; - int max = 0; - db << "select max(id) from mime_table limit 1" >> max; - if (count == 0) - { - max++; - db << "insert into mime_table(mime, id) values(?, ?)" << mime_type << max; - return max; - } - else - { - unsigned int id; - db << "select id from mime_table where mime=? limit 1" << mime_type >> id; - return id; - } - return 0; -} - -long trendcpp::matching_hash_from_threat_db(std::string tlsh_hash, std::string file_type, long file_size_minimum, unsigned long file_size_maximum) -{ - long matched_id = -1; - sqlite::database threat_table("threat_db.db"); - threat_table << "create table if not exists threat(id unsigned bigint primary key, threat_hash text, threat_name text, threat_size unsigned int, threat_type unsigned int);"; - unsigned int file_id = mime_to_id(file_type); - threat_table << "select id, threat_hash from threat where threat_size>=? and threat_size<=? and threat_type=?" - << file_size_minimum << file_size_maximum << file_id >> [&] (unsigned long id, std::string threat_hash) - { - if (similarity_distance(tlsh_hash, threat_hash) < 20) - { - matched_id = id; - } - }; - return matched_id; -} diff --git a/Mrida/Mrida/trendcpp.h b/Mrida/Mrida/trendcpp.h index 87c7b79..fc73adb 100644 --- a/Mrida/Mrida/trendcpp.h +++ b/Mrida/Mrida/trendcpp.h @@ -16,16 +16,7 @@ class trendcpp // Get TLSH object const Tlsh* hash_file(std::string file_location); - // Bool add threat to database - void add_threat_to_database(unsigned long int id, std::string tlsh_hash, std::string threat_name, unsigned long file_size, unsigned int file_type); - // Getting the similarity distance int similarity_distance(std::string hash_one, std::string hash_two); - - // Mime Type to Id - unsigned int mime_to_id(std::string mime_type); - - // Will get the similar hash matching id from threat db [returns -1 if nothing is matching] - long matching_hash_from_threat_db(std::string tlsh_hash, std::string file_type, long file_size_minimum, unsigned long file_size_maximum); }; diff --git a/Mrida/Release/mrida.iobj b/Mrida/Release/mrida.iobj new file mode 100644 index 0000000..62d5d43 Binary files /dev/null and b/Mrida/Release/mrida.iobj differ diff --git a/Mrida/Release/mrida.ipdb b/Mrida/Release/mrida.ipdb new file mode 100644 index 0000000..d0710cb Binary files /dev/null and b/Mrida/Release/mrida.ipdb differ diff --git a/Mrida/Release/mrida.pdb b/Mrida/Release/mrida.pdb new file mode 100644 index 0000000..bee9fcf Binary files /dev/null and b/Mrida/Release/mrida.pdb differ diff --git a/mrida_gui/.idea/workspace.xml b/mrida_gui/.idea/workspace.xml index ca0ce69..572ac38 100644 --- a/mrida_gui/.idea/workspace.xml +++ b/mrida_gui/.idea/workspace.xml @@ -10,12 +10,36 @@ @@ -114,14 +139,34 @@