Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remap Port Fields to ECS compliance #310

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/modules/inventory/include/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Inventory {
nlohmann::json GetOSData();
nlohmann::json EcsHardwareData(const nlohmann::json& originalData);
nlohmann::json EcsPackageData(const nlohmann::json& originalData);
nlohmann::json EcsPortData(const nlohmann::json& originalData);
nlohmann::json GetHardwareData();
nlohmann::json GetNetworkData();
nlohmann::json GetPortsData();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/inventory/src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ cJSON * Inventory::Dump() {

void Inventory::LogErrorInventory(const std::string& log)
{
LogError("{}", log.c_str());
LogError("{}", log);
}
38 changes: 32 additions & 6 deletions src/modules/inventory/src/inventoryImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ constexpr auto PORTS_SQL_STATEMENT
process TEXT,
checksum TEXT,
item_id TEXT,
PRIMARY KEY (inode, protocol, local_ip, local_port)) WITHOUT ROWID;)"
PRIMARY KEY (inode,protocol,local_ip,local_port)) WITHOUT ROWID;)"
};
static const std::vector<std::string> PORTS_ITEM_ID_FIELDS{"inode", "protocol", "local_ip", "local_port"};

Expand Down Expand Up @@ -271,6 +271,10 @@ nlohmann::json Inventory::EcsData(const nlohmann::json& data, const std::string&
{
ret = EcsProcessesData(data);
}
else if (table == PORTS_TABLE)
{
ret = EcsPortData(data);
}
return ret;
}

Expand All @@ -293,6 +297,10 @@ std::string Inventory::GetPrimaryKeys([[maybe_unused]] const nlohmann::json& dat
{
ret = data["process"]["pid"];
}
else if (table == PORTS_TABLE)
{
ret = std::to_string(data["file"]["inode"].get<int>()) + ":" + data["network"]["protocol"].get<std::string>() + ":" + data["source"]["ip"].get<std::string>() + ":" + std::to_string(data["source"]["port"].get<int>());
}
return ret;
}

Expand Down Expand Up @@ -515,6 +523,27 @@ nlohmann::json Inventory::EcsProcessesData(const nlohmann::json& originalData)
return ret;
}

nlohmann::json Inventory::EcsPortData(const nlohmann::json& originalData)
{
nlohmann::json ret;

ret["network"]["protocol"] = originalData.contains("protocol") ? originalData["protocol"] : "";
ret["source"]["ip"] = originalData.contains("local_ip") ? originalData["local_ip"] : "";
ret["source"]["port"] = originalData.contains("local_port") ? originalData["local_port"] : nlohmann::json(0);
ret["destination"]["ip"] = originalData.contains("remote_ip") ? originalData["remote_ip"] : "";
ret["destination"]["port"] = originalData.contains("remote_port") ? originalData["remote_port"] : nlohmann::json(0);
ret["host"]["network"]["egress"]["queue"] = originalData.contains("tx_queue") ? originalData["tx_queue"] : nlohmann::json(0);
ret["host"]["network"]["ingress"]["queue"] = originalData.contains("rx_queue") ? originalData["rx_queue"] : nlohmann::json(0);
ret["file"]["inode"] = originalData.contains("inode") ? originalData["inode"] : nlohmann::json(0);
ret["interface"]["state"] = originalData.contains("state") ? originalData["state"] : "";
ret["process"]["pid"] = originalData.contains("pid") ? originalData["pid"] : nlohmann::json(0);
ret["process"]["name"] = originalData.contains("process") ? originalData["process"] : "";
ret["device"]["id"] = originalData.contains("item_id") ? originalData["item_id"] : "";
ret["@ŧimestamp"] = originalData.contains("scan_time") ? originalData["scan_time"] : "";

return ret;
}

nlohmann::json Inventory::GetHardwareData()
{
nlohmann::json ret;
Expand Down Expand Up @@ -782,7 +811,6 @@ nlohmann::json Inventory::GetPortsData()

if (!IsElementDuplicated(ret, std::make_pair("item_id", itemId)))
{
item["checksum"] = GetItemChecksum(item);
item["item_id"] = itemId;
ret.push_back(item);
}
Expand All @@ -798,7 +826,6 @@ nlohmann::json Inventory::GetPortsData()

if (!IsElementDuplicated(ret, std::make_pair("item_id", itemId)))
{
item["checksum"] = GetItemChecksum(item);
item["item_id"] = itemId;
ret.push_back(item);
}
Expand All @@ -811,7 +838,6 @@ nlohmann::json Inventory::GetPortsData()

if (!IsElementDuplicated(ret, std::make_pair("item_id", itemId)))
{
item["checksum"] = GetItemChecksum(item);
item["item_id"] = itemId;
ret.push_back(item);
}
Expand Down Expand Up @@ -877,11 +903,11 @@ void Inventory::Scan()
TryCatchTask([&]() { ScanOs(); });
TryCatchTask([&]() { ScanPackages(); });
TryCatchTask([&]() { ScanProcesses(); });
TryCatchTask([&]() { ScanPorts(); });

// TO DO: enable each scan once the ECS translation is done
//TryCatchTask([&]() { ScanNetwork(); });
//TryCatchTask([&]() { ScanHotfixes(); });
//TryCatchTask([&]() { ScanPorts(); });
//TryCatchTask([&]() { ScanHotfixes(); });;
m_notify = true;
LogInfo("Evaluation finished.");
}
Expand Down
Loading