Add MySQL Cache Manager & code cleanup

This commit is contained in:
Brendan LE GLAUNEC
2016-05-21 11:17:08 +02:00
committed by Brendan Le Glaunec
parent faa2570883
commit 8a8e4faa42
29 changed files with 844 additions and 69 deletions
+14 -3
View File
@@ -1,5 +1,13 @@
{
"subnets" : "172.16.100.13,localhost",
"mysql_db" : {
"host" : "0.0.0.0",
"port" : 3306,
"user": "root",
"password": "root",
"db_name": "cctv_dev"
},
"subnets" : "172.16.100.11",
// If not specified, will scan all ports (1-65535)
"ports" : "554,8554",
@@ -9,6 +17,9 @@
// You must give an accessible path to an already existing directory
"thumbnail_storage_path" : "/tmp",
"cache_manager_path" : "../cache_managers/dumb_cache_manager",
"cache_manager_name" : "dumb"
// This is the path that will be used in the Docker container
// if you're not familiar with Docker, only change the
// cache_manager_name value
"cache_manager_path" : "../cache_managers",
"cache_manager_name" : "mysql"
}
+6 -6
View File
@@ -14,10 +14,10 @@
#pragma once
#include <vector>
#include <configuration.h>
#include <memory>
#include <stream_model.h>
#include <configuration.h>
#include <vector>
namespace etix {
namespace cameradar {
@@ -42,10 +42,10 @@ public:
virtual void update_stream(const etix::cameradar::stream_model& newmodel) = 0;
//! Gets all cached streams
virtual std::vector<etix::cameradar::stream_model> get_streams() const = 0;
virtual std::vector<etix::cameradar::stream_model> get_streams() = 0;
//! Gets all valid streams which have been accessed
virtual std::vector<etix::cameradar::stream_model> get_valid_streams() const = 0;
virtual std::vector<etix::cameradar::stream_model> get_valid_streams() = 0;
};
class cache_manager_base : public cache_manager_iface {
@@ -68,10 +68,10 @@ public:
virtual void update_stream(const etix::cameradar::stream_model& newmodel) = 0;
//! Gets all cached streams
virtual std::vector<etix::cameradar::stream_model> get_streams() const = 0;
virtual std::vector<etix::cameradar::stream_model> get_streams() = 0;
//! Gets all valid streams which have been accessed
virtual std::vector<etix::cameradar::stream_model> get_valid_streams() const = 0;
virtual std::vector<etix::cameradar::stream_model> get_valid_streams() = 0;
//! Get the manager's instance
cache_manager_base& get_instance();
@@ -14,11 +14,15 @@
#pragma once
#include <assert.h> // assert
#include <csignal> // sigint
#include <iostream> // stc::cout
#include <assert.h> // assert
// To avoid an unused warning for the asserted in handle_signal
#define _unused(x) ((void)(x))
namespace etix {
namespace cameradar {
enum class stop_priority { running, stop, force_stop };
@@ -30,6 +34,7 @@ public:
virtual int
handle_signal(int signum) {
assert(signum == SIGINT);
_unused(signum);
std::cout << "\b\b\b\033[K";
if (this->ss == stop_priority::running)
this->ss = stop_priority::stop;
+2 -2
View File
@@ -14,8 +14,8 @@
#pragma once
#include <string>
#include <json/value.h>
#include <string>
namespace etix {
namespace cameradar {
@@ -24,7 +24,7 @@ struct stream_model {
// Ex : "172.16.100.113"
std::string address;
// Ex : 8554
unsigned short port;
unsigned int port;
// Ex : "admin"
std::string username = "";
// Ex : "123456"
+5 -6
View File
@@ -14,12 +14,11 @@
#pragma once
#include <cameradar_task.h> // task interface
#include <boost/algorithm/string/find.hpp> // boost::find
#include <iostream> // std::ofstream
#include <fstream> // std::ofstream
#include <stream_model.h> // data model
#include <cachemanager.h> // cacheManager
#include <cachemanager.h> // cacheManager
#include <cameradar_task.h> // task interface
#include <fstream> // std::ofstream
#include <iostream> // std::ofstream
#include <stream_model.h> // data model
namespace etix {
namespace cameradar {
+4 -13
View File
@@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <configuration.h> // configuration
#include <fstream> // std::ifstream
#include <unistd.h> // access, F_OK
#include <configuration.h> // configuration
namespace etix {
@@ -22,9 +22,9 @@ namespace cameradar {
const std::string configuration::name_ = "configuration";
// read a file at the path "path"
// if the file is available we return the whole content as an std::string inside
// a pair
// Read a file at the path "path"
// If the file is available we return the whole content as
// an std::string inside a pair
// otherwise return false and an empty string inside a pair
std::pair<bool, std::string>
read_file(const std::string& path) {
@@ -107,14 +107,6 @@ configuration::load_url() {
auto root = Json::Value();
auto reader = Json::Reader();
reader.parse(content, root);
// auto result = tool::json::check_fields(
// {{"urls", Json::arrayValue, root["urls"]}}, "general
// configuration");
// if (not result.first) {
// LOG_ERR_(result.second, "general configuration");
// return false;
// }
for (unsigned int i = 0; i < root["urls"].size(); i++) {
if (not root["urls"][i].isString()) {
@@ -194,7 +186,6 @@ load(const std::string& path) {
}
// Deserialize the json to a configuration struct
// and return
// REPLACE THIS WITH JSONCPP
std::pair<bool, configuration> conf = serialize(root);
conf.second.raw_conf = root;
conf.first &= conf.second.load_url();
+13 -4
View File
@@ -20,12 +20,20 @@ namespace cameradar {
// The main loop of the binary
void
dispatcher::run() {
if (not(*cache)->configure(std::make_shared<configuration>(conf))) {
LOG_ERR_(
"There was a problem with the cache manager, Cameradar can't work properly without "
"cache management",
"dispatcher");
return;
}
std::thread worker(&dispatcher::do_stuff, this);
using namespace std::chrono_literals;
// catch CTRL+C signal
// Catch CTRL+C signal
signal_handler::instance();
// wait for event or end
// Wait for event or end
while (signal_handler::instance().should_stop() not_eq stop_priority::stop &&
current != task::finished) {
std::this_thread::sleep_for(30ms);
@@ -36,7 +44,7 @@ dispatcher::run() {
LOG_INFO_("Press CTRL+C again to force stop", "dispatcher");
}
// waiting for task to cleanup / force stop command
// Waiting for task to cleanup / force stop command
while ((signal_handler::instance().should_stop() not_eq stop_priority::force_stop) and
doing_stuff()) {
std::this_thread::sleep_for(std::chrono::milliseconds(30));
@@ -76,7 +84,8 @@ dispatcher::do_stuff() {
if (queue.front()->run())
queue.pop_front();
else {
LOG_ERR_("An error occured in one of the tasks, Cameradar will now stop.", "dispatcher");
LOG_ERR_("An error occured in one of the tasks, Cameradar will now stop.",
"dispatcher");
break;
}
}
+4 -4
View File
@@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fs.h> // fs::home
#include <opt_parse.h> // parsing opt
#include <dispatcher.h> // program loop
#include <iostream> // iostream
#include "version.h" // versionning
#include <dispatcher.h> // program loop
#include <fs.h> // fs::home
#include <iostream> // iostream
#include <opt_parse.h> // parsing opt
namespace cmrdr = etix::cameradar;
+8 -8
View File
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <tasks/brutelogs.h>
#include <cachemanager.h>
#include <tasks/brutelogs.h>
namespace etix {
namespace cameradar {
@@ -36,10 +36,10 @@ brutelogs::test_ids(const etix::cameradar::stream_model& stream,
std::string path = stream.service_name + "://";
if (username != "" || password != "") { path += username + ":" + password + "@"; }
path += stream.address + ":" + std::to_string(stream.port);
LOG_DEBUG_("Testing ids : " + path, "bruteforce");
LOG_DEBUG_("Testing ids : " + path, "brutelogs");
try {
if (curl_describe(path, true)) {
LOG_DEBUG_("[FOUND IDS] : " + path, "bruteforce");
LOG_DEBUG_("[FOUND IDS] : " + path, "brutelogs");
found = true;
stream_model newstream{
stream.address, stream.port, username, password,
@@ -55,7 +55,7 @@ brutelogs::test_ids(const etix::cameradar::stream_model& stream,
(*cache)->update_stream(newstream);
}
} catch (const std::runtime_error& e) {
LOG_DEBUG_("Ids already tested : " + std::string(e.what()), "bruteforce");
LOG_DEBUG_("Ids already tested : " + std::string(e.what()), "brutelogs");
}
return found;
}
@@ -75,7 +75,7 @@ brutelogs::run() const {
LOG_INFO_(
"Beginning bruteforce of the usernames and passwords task, it may "
"take a while.",
"bruteforce");
"brutelogs");
std::vector<etix::cameradar::stream_model> streams = (*cache)->get_streams();
bool doubleskip;
size_t found = 0;
@@ -88,7 +88,7 @@ brutelogs::run() const {
" : This camera's ids were already discovered in "
"the database. Skipping to "
"the next camera.",
"bruteforce");
"brutelogs");
++found;
} else {
for (const auto& username : conf.usernames) {
@@ -110,12 +110,12 @@ brutelogs::run() const {
}
}
if (!found) {
LOG_WARN_(no_ids_warning_, "bruteforce");
LOG_WARN_(no_ids_warning_, "brutelogs");
return false;
} else
LOG_INFO_("Found " + std::to_string(found) + " ids for " + std::to_string(streams.size()) +
" cameras",
"bruteforce");
"brutelogs");
return true;
}
}
+7 -3
View File
@@ -28,9 +28,12 @@ namespace cameradar {
//! problem.
bool
nmap_is_ok() {
return (launch_command("test `dpkg -l | cut -c 5-9 | grep nmap` = nmap")
// && launch_command("test `nmap --version | cut -c 14-18 | head -n2 | tail -n1` = 6.47")
&& launch_command("mkdir -p scans")); // Creates the directory in which the scans will be stored
return (
launch_command("test `dpkg -l | cut -c 5-9 | grep nmap` = nmap")
// && launch_command("test `nmap --version | cut -c 14-18 | head -n2 | tail -n1` = 6.47")
&&
launch_command(
"mkdir -p scans")); // Creates the directory in which the scans will be stored
}
//! Launches and checks the return of the nmap command
@@ -44,6 +47,7 @@ mapping::run() const {
LOG_INFO_("Beginning mapping task. This may take a while.", "mapping");
std::string cmd =
"nmap -T4 -A " + subnets + " -p " + this->conf.ports + " -oX " + nmap_output;
LOG_DEBUG_("Launching nmap : " + cmd, "mapping");
bool ret = launch_command(cmd);
if (ret)
LOG_INFO_("Nmap XML output successfully generated in file: " + nmap_output, "mapping");