Added support for random agent names on win32.

This commit is contained in:
Ramon Novoa 2016-09-29 12:50:04 +02:00
parent 65792e6abc
commit f720817e57
9 changed files with 267 additions and 64 deletions

View File

@ -1,9 +1,9 @@
bin_PROGRAMS = PandoraAgent
if DEBUG
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_plugin.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc modules/pandora_module_ping.cc modules/pandora_module_snmpget.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc misc/md5.c windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc debug_new.cpp
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_plugin.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc modules/pandora_module_ping.cc modules/pandora_module_snmpget.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc misc/md5.c misc/sha256.cc windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc debug_new.cpp
PandoraAgent_CXXFLAGS=-g -O0
else
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_plugin.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc modules/pandora_module_ping.cc modules/pandora_module_snmpget.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc misc/md5.c windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc
PandoraAgent_SOURCES = misc/pandora_file.cc modules/pandora_data.cc modules/pandora_module_factory.cc modules/pandora_module.cc modules/pandora_module_list.cc modules/pandora_module_plugin.cc modules/pandora_module_inventory.cc modules/pandora_module_freememory.cc modules/pandora_module_exec.cc modules/pandora_module_perfcounter.cc modules/pandora_module_proc.cc modules/pandora_module_tcpcheck.cc modules/pandora_module_freememory_percent.cc modules/pandora_module_freedisk.cc modules/pandora_module_freedisk_percent.cc modules/pandora_module_logevent.cc modules/pandora_module_service.cc modules/pandora_module_cpuusage.cc modules/pandora_module_wmiquery.cc modules/pandora_module_regexp.cc modules/pandora_module_ping.cc modules/pandora_module_snmpget.cc udp_server/udp_server.cc main.cc pandora_strutils.cc pandora.cc windows_service.cc pandora_agent_conf.cc windows/pandora_windows_info.cc windows/pandora_wmi.cc pandora_windows_service.cc misc/md5.c misc/sha256.cc windows/wmi/disphelper.c ssh/libssh2/channel.c ssh/libssh2/mac.c ssh/libssh2/session.c ssh/libssh2/comp.c ssh/libssh2/misc.c ssh/libssh2/sftp.c ssh/libssh2/crypt.c ssh/libssh2/packet.c ssh/libssh2/userauth.c ssh/libssh2/hostkey.c ssh/libssh2/publickey.c ssh/libssh2/kex.c ssh/libssh2/scp.c ssh/pandora_ssh_client.cc ssh/pandora_ssh_test.cc ftp/pandora_ftp_client.cc ftp/pandora_ftp_test.cc
PandoraAgent_CXXFLAGS=-O2
endif

View File

@ -0,0 +1,44 @@
/* Pandora agents service for Win32.
Copyright (C) 2016 Artica ST.
Written by Ramon Novoa.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include "openssl/sha.h"
#include "sha256.h"
void
sha256(const char *data, char hex_digest[SHA256_HEX_LENGTH + 1])
{
int i, j;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
// Calculate the SHA-256 hash.
SHA256_Init(&sha256);
SHA256_Update(&sha256, data, strlen(data));
SHA256_Final(hash, &sha256);
// Convert it to a hexadecimal string.
for(i = 0, j = 0; i < SHA256_DIGEST_LENGTH, j < SHA256_HEX_LENGTH; i++, j+=2) {
sprintf(&(hex_digest[j]), "%02x", hash[i]);
}
// Add a NULL terminator.
hex_digest[SHA256_HEX_LENGTH] = 0;
}

View File

@ -0,0 +1,29 @@
/* Pandora agents service for Win32.
Copyright (C) 2016 Artica ST.
Written by Ramon Novoa.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SHA256_H_
#define _SHA256_H_
// Length of the sha256 hex string.
#define SHA256_HEX_LENGTH 64
void sha256(const char *data, char hex_digest[SHA256_HEX_LENGTH + 1]);
#endif

View File

@ -99,6 +99,15 @@ Key_Value::getKey () {
return key;
}
/**
* Set the key of the object.
*
*/
void
Key_Value::setKey (const string key) {
this->key = key;
}
/**
* Get the value of the object.
*
@ -109,6 +118,16 @@ Key_Value::getValue () {
return value;
}
/**
* Set the value of the object.
*
* @return The value
*/
void
Key_Value::setValue (const string value) {
this->value = value;
}
void
pandoraWriteLog (string filename, string line) {
string buffer;

View File

@ -62,7 +62,9 @@ namespace Pandora {
void parseLine (string str);
void parseLineByPosition (string str, int pos);
string getKey ();
void setKey (const string value);
string getValue ();
void setValue (const string value);
};
static const HKEY hkey = HKEY_LOCAL_MACHINE;

View File

@ -350,6 +350,70 @@ Pandora::Pandora_Agent_Conf::setFile (string filename) {
file.close ();
}
/**
* Update a configuration value in the configuration file. If it is not found,
* it is appended at the end of the file.
*
* @param string key Name of the configuration option.
* @param string value New value.
*/
void
Pandora::Pandora_Agent_Conf::updateFile (string key, string value){
string buffer, filename, temp_filename;
int pos;
/* Open the configuration file. */
filename = Pandora::getPandoraInstallDir ();
filename += "pandora_agent.conf";
ifstream file (filename.c_str ());
if (!file.is_open ()) {
return;
}
/* Open the temporary file. */
temp_filename = filename + ".tmp";
ofstream temp_file (temp_filename.c_str ());
if (!temp_file.is_open ()) {
return;
}
/* Look for the configuration value. */
bool found = false;
while (!file.eof ()) {
getline (file, buffer);
/* Copy the rest of the file if the key was found. */
if (found) {
temp_file << buffer << std::endl;
continue;
}
/* We will only look for the key in the first three characters, hoping
to catch "key", "#key" and "# key". We would also catch "..key", but
no such keys exist in the configuration file. */
pos = buffer.find(key);
if (pos == std::string::npos || pos > 2) {
temp_file << buffer << std::endl;
continue;
}
/* Match! */
found = true;
temp_file << key + " " + value << std::endl;
}
/* Append the value at the end of the file if it was not found. */
if (!found) {
temp_file << key + " " + value << std::endl;
}
/* Rename the temporary file. */
file.close ();
temp_file.close ();
remove(filename.c_str());
rename(temp_filename.c_str(), filename.c_str());
}
/**
* Queries for a configuration value.
*
@ -375,6 +439,33 @@ Pandora::Pandora_Agent_Conf::getValue (const string key)
return "";
}
/**
* Sets a configuration value.
*
* @param key Key to look for.
* @param string New value.
*
*/
void
Pandora::Pandora_Agent_Conf::setValue (const string key, const string value)
{
std::list<Key_Value>::iterator i;
// Update.
for (i = this->key_values->begin (); i != this->key_values->end (); i++) {
if ((*i).getKey () == key) {
(*i).setValue (value);
return;
}
}
// Append.
Key_Value kv;
kv.setKey(key);
kv.setValue(value);
this->key_values->push_back (kv);
}
/**
* Queries for a collection name.
*

View File

@ -55,7 +55,9 @@ namespace Pandora {
void parseFile(string path_file, Collection *aux);
void setFile (string *all_conf);
void setFile (string filename);
void updateFile (string key, string value);
string getValue (const string key);
void setValue (const string key, const string value);
string getCurrentCollectionName();
unsigned char getCurrentCollectionVerify();

View File

@ -26,6 +26,7 @@
#include "ssh/pandora_ssh_client.h"
#include "ftp/pandora_ftp_client.h"
#include "misc/pandora_file.h"
#include "misc/sha256.h"
#include "windows/pandora_windows_info.h"
#include "udp_server/udp_server.h"
@ -39,6 +40,8 @@
#include <pandora_agent_conf.h>
#include <fstream>
#include <unistd.h>
#include <sstream>
#include <string>
#define BUFSIZE 4096
@ -207,7 +210,7 @@ void
Pandora_Windows_Service::pandora_init () {
string conf_file, interval, debug, disable_logfile, intensive_interval, util_dir, path, env;
string udp_server_enabled, udp_server_port, udp_server_addr, udp_server_auth_addr;
string name_agent, name;
string agent_name, agent_name_cmd, agent_alias, pandora_agent;
string proxy_mode, server_ip;
string *all_conf;
int pos, num;
@ -255,12 +258,60 @@ Pandora_Windows_Service::pandora_init () {
this->modules = new Pandora_Module_List (conf_file);
delete []all_conf;
name = checkAgentName(conf_file);
if (name.empty ()) {
name = Pandora_Windows_Info::getSystemName ();
// Get the agent name.
agent_name = conf->getValue ("agent_name");
printf("AGENT NAME: %s\n", agent_name.c_str());
if (agent_name == "") {
agent_name_cmd = conf->getValue ("agent_name_cmd");
// Random name.
if (agent_name_cmd == "__rand__") {
agent_name = generateAgentName();
this->conf->setValue("agent_name", agent_name);
conf->updateFile("agent_name", agent_name); // Write random names to disk!
}
// Name from command.
else if (agent_name_cmd != "") {
agent_name_cmd = "cmd.exe /c \"" + agent_name_cmd + "\"";
static string temp_agent_name = getAgentNameFromCmdExec(agent_name_cmd);
// Delete new line and carriage return.
pos = temp_agent_name.find("\n");
if(pos != string::npos) {
temp_agent_name.erase(pos, temp_agent_name.size () - pos);
}
pos = temp_agent_name.find("\r");
if(pos != string::npos) {
temp_agent_name.erase(pos, temp_agent_name.size () - pos);
}
// Remove leading and trailing white spaces.
temp_agent_name = trim(temp_agent_name);
if (temp_agent_name != "") {
agent_name = temp_agent_name;
this->conf->setValue("agent_name", agent_name);
}
}
}
name_agent = "PANDORA_AGENT=" + name;
putenv(name_agent.c_str());
printf("AGENT NAME2: %s\n", agent_name.c_str());
// Fall back to the hostname if agent_name is still empty.
if (agent_name == "") {
agent_name = Pandora_Windows_Info::getSystemName ();
this->conf->setValue("agent_name", agent_name);
}
printf("AGENT NAME3: %s\n", agent_name.c_str());
printf("AGENT NAME4: %s\n", this->conf->getValue("agent_name").c_str());
// Get the agent alias.
conf->getValue ("agent_alias");
if (agent_alias == "") {
agent_alias = Pandora_Windows_Info::getSystemName ();
this->conf->setValue("agent_alias", agent_alias);
}
pandora_agent = "PANDORA_AGENT=" + agent_name;
putenv(pandora_agent.c_str());
debug = conf->getValue ("debug");
setPandoraDebug (is_enabled (debug));
@ -383,7 +434,7 @@ Pandora_Windows_Service::launchTentacleProxy() {
string
Pandora_Windows_Service::getXmlHeader () {
char timestamp[20];
string agent_name, os_name, os_version, encoding, value, xml, address, parent_agent_name, agent_name_cmd;
string agent_name, os_name, os_version, encoding, value, xml, address, parent_agent_name, agent_name_cmd, agent_alias;
string custom_id, url_address, latitude, longitude, altitude, position_description, gis_exec, gis_result, agent_mode;
time_t ctime;
struct tm *ctime_tm = NULL;
@ -391,30 +442,9 @@ Pandora_Windows_Service::getXmlHeader () {
// Get agent name
agent_name = conf->getValue ("agent_name");
if (agent_name == "") {
agent_name = Pandora_Windows_Info::getSystemName ();
}
agent_name_cmd = conf->getValue ("agent_name_cmd");
if (agent_name_cmd != "") {
agent_name_cmd = "cmd.exe /c \"" + agent_name_cmd + "\"";
static string temp_agent_name = getAgentNameFromCmdExec(agent_name_cmd);
// Delete carriage return if is provided
pos = temp_agent_name.find("\n");
if(pos != string::npos) {
temp_agent_name.erase(pos, temp_agent_name.size () - pos);
}
pos = temp_agent_name.find("\r");
if(pos != string::npos) {
temp_agent_name.erase(pos, temp_agent_name.size () - pos);
}
// Remove white spaces of the first and last.
temp_agent_name = trim(temp_agent_name);
if (temp_agent_name != "") {
agent_name = temp_agent_name;
}
}
// Get agent alias
conf->getValue ("agent_alias");
// Get parent agent name
parent_agent_name = conf->getValue ("parent_agent_name");
@ -442,6 +472,7 @@ Pandora_Windows_Service::getXmlHeader () {
xml = "<?xml version=\"1.0\" encoding=\"" + encoding + "\" ?>\n" +
"<agent_data agent_name=\"" + agent_name +
"\" agent_alias=\"" + agent_alias +
"\" description=\"" + conf->getValue ("description") +
"\" version=\"" + getPandoraAgentVersion ();
@ -1477,35 +1508,8 @@ Pandora_Windows_Service::checkConfig (string file) {
}
/* Get agent name */
tmp = checkAgentName(file);
if (tmp.empty ()) {
tmp = Pandora_Windows_Info::getSystemName ();
}
agent_name = tmp;
tmp = conf->getValue ("agent_name");
/* Get agent name cmd */
tmp = conf->getValue ("agent_name_cmd");
if (!tmp.empty ()) {
tmp = "cmd.exe /c \"" + tmp + "\"";
tmp = getCoordinatesFromCmdExec(tmp);
// Delete carriage return if is provided
pos = tmp.find("\n");
if(pos != string::npos) {
tmp.erase(pos, tmp.size () - pos);
}
pos = tmp.find("\r");
if(pos != string::npos) {
tmp.erase(pos, tmp.size () - pos);
}
// Remove white spaces of the first and last.
tmp = trim (tmp);
if (tmp != "") {
agent_name = tmp;
}
}
/* Error getting agent name */
if (agent_name.empty ()) {
@ -1691,10 +1695,6 @@ Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
/* Generate temporal filename */
random_integer = inttostr (rand());
tmp_filename = conf->getValue ("agent_name");
if (tmp_filename == "") {
tmp_filename = Pandora_Windows_Info::getSystemName ();
}
tmp_filename += "." + random_integer + ".data";
xml_filename = conf->getValue ("temporal");
@ -2036,3 +2036,17 @@ Pandora_Windows_Service::getIntensiveInterval () {
return this->intensive_interval;
}
string
Pandora_Windows_Service::generateAgentName () {
stringstream data;
char digest[SHA256_HEX_LENGTH + 1];
std::srand(std::time(0));
data << this->conf->getValue("agent_alias") <<
this->conf->getValue("server_ip") <<
time(NULL) <<
std::rand();
sha256(data.str().c_str(), digest);
return std::string(digest);
}

View File

@ -44,6 +44,7 @@ namespace Pandora {
Pandora_Module_List *modules;
long execution_number;
string agent_name;
string alias;
time_t timestamp;
time_t run_time;
bool started;
@ -116,6 +117,7 @@ namespace Pandora {
Pandora_Agent_Conf *getConf ();
long getInterval ();
long getIntensiveInterval ();
string generateAgentName ();
};
}