2019-04-22 12:25:25 +00:00
|
|
|
#include <hook/shell.hpp>
|
2019-07-12 13:06:59 +00:00
|
|
|
|
|
|
|
#if defined(ENABLE_SHELLHOOKS)
|
2019-05-18 19:28:14 +00:00
|
|
|
#include <util/thread_pool.hpp>
|
2019-04-22 12:25:25 +00:00
|
|
|
#include <util/logger.hpp>
|
2019-04-22 12:48:48 +00:00
|
|
|
#include <sys/wait.h>
|
2019-04-27 10:48:01 +00:00
|
|
|
#include <unistd.h>
|
2019-05-06 02:41:06 +00:00
|
|
|
#if !defined(__linux__) || !defined(_GNU_SOURCE)
|
|
|
|
// Not all systems declare this variable
|
2020-04-07 18:38:56 +00:00
|
|
|
extern char** environ;
|
2019-05-06 02:41:06 +00:00
|
|
|
#endif
|
2019-04-27 10:48:01 +00:00
|
|
|
#if defined(Darwin)
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#endif
|
2019-07-12 13:06:59 +00:00
|
|
|
#endif
|
2019-04-22 12:25:25 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace hooks
|
|
|
|
{
|
2019-07-12 13:06:59 +00:00
|
|
|
#if defined(ENABLE_SHELLHOOKS)
|
2020-04-07 18:38:56 +00:00
|
|
|
struct ExecShellHookBackend : public IBackend,
|
|
|
|
public std::enable_shared_from_this<ExecShellHookBackend>
|
2019-04-22 12:25:25 +00:00
|
|
|
{
|
2019-05-18 19:28:14 +00:00
|
|
|
thread::ThreadPool m_ThreadPool;
|
2019-05-05 13:51:48 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<std::string> _args;
|
|
|
|
std::vector<char*> args;
|
2019-04-22 14:00:59 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
ExecShellHookBackend(std::string script) : m_ThreadPool(1, 1000, "exechook")
|
2019-04-22 12:25:25 +00:00
|
|
|
{
|
2019-05-05 13:51:48 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
const auto idx = script.find_first_of(' ');
|
|
|
|
std::string sub;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (idx == std::string::npos)
|
2019-05-05 13:51:48 +00:00
|
|
|
sub = script;
|
|
|
|
else
|
|
|
|
sub = script.substr(0, idx);
|
|
|
|
_args.emplace_back(std::move(sub));
|
2020-04-07 18:38:56 +00:00
|
|
|
args.push_back((char*)_args.back().c_str());
|
2019-05-05 13:51:48 +00:00
|
|
|
script = script.substr(idx + 1);
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (script.find_first_of(' ') != std::string::npos);
|
2019-05-05 13:51:48 +00:00
|
|
|
args.push_back(nullptr);
|
|
|
|
LogInfo("make hook ", args.size());
|
2019-04-22 12:25:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 14:00:59 +00:00
|
|
|
~ExecShellHookBackend()
|
|
|
|
{
|
2019-05-18 19:28:14 +00:00
|
|
|
m_ThreadPool.shutdown();
|
2019-04-22 14:00:59 +00:00
|
|
|
}
|
2019-04-22 12:25:25 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Start() override
|
|
|
|
{
|
2019-05-18 19:28:14 +00:00
|
|
|
m_ThreadPool.start();
|
2019-04-22 14:00:59 +00:00
|
|
|
return true;
|
2019-04-22 12:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Stop() override
|
|
|
|
{
|
2019-05-18 19:28:14 +00:00
|
|
|
m_ThreadPool.stop();
|
2019-04-22 12:25:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
char*
|
2019-05-05 13:51:48 +00:00
|
|
|
Exe() const
|
|
|
|
{
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
char* const*
|
2019-05-05 13:51:48 +00:00
|
|
|
Args() const
|
|
|
|
{
|
|
|
|
return args.data();
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
NotifyAsync(std::unordered_map<std::string, std::string> params) override;
|
2019-05-05 13:51:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ExecShellHookJob
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<std::string> m_env;
|
|
|
|
std::vector<char*> _m_env;
|
|
|
|
std::shared_ptr<ExecShellHookBackend> m_Parent;
|
2019-05-05 13:51:48 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
ExecShellHookJob(
|
|
|
|
std::shared_ptr<ExecShellHookBackend> b,
|
|
|
|
const std::unordered_map<std::string, std::string> env)
|
2019-05-05 13:51:48 +00:00
|
|
|
: m_Parent(b)
|
2019-04-22 12:25:25 +00:00
|
|
|
{
|
2019-05-05 13:51:48 +00:00
|
|
|
#if defined(Darwin)
|
2020-04-07 18:38:56 +00:00
|
|
|
char** ptr = *_NSGetEnviron();
|
2019-05-05 13:51:48 +00:00
|
|
|
#else
|
2020-04-07 18:38:56 +00:00
|
|
|
char** ptr = environ;
|
2019-05-05 13:51:48 +00:00
|
|
|
#endif
|
|
|
|
do
|
|
|
|
{
|
|
|
|
m_env.emplace_back(*ptr);
|
|
|
|
++ptr;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (ptr && *ptr);
|
|
|
|
for (const auto& item : env)
|
2019-05-05 13:51:48 +00:00
|
|
|
m_env.emplace_back(item.first + "=" + item.second);
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& item : m_env)
|
|
|
|
_m_env.push_back((char*)item.c_str());
|
2019-05-05 13:51:48 +00:00
|
|
|
_m_env.push_back(nullptr);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
char* const*
|
2019-05-05 13:51:48 +00:00
|
|
|
Env()
|
|
|
|
{
|
|
|
|
return _m_env.data();
|
|
|
|
}
|
|
|
|
|
2019-05-18 19:28:14 +00:00
|
|
|
void
|
|
|
|
Exec()
|
2019-05-05 13:51:48 +00:00
|
|
|
{
|
|
|
|
std::thread t([&]() {
|
2020-04-07 18:38:56 +00:00
|
|
|
int result = 0;
|
2019-05-05 13:51:48 +00:00
|
|
|
const pid_t child = ::fork();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (child == -1)
|
2019-05-05 13:51:48 +00:00
|
|
|
return;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (child)
|
2019-05-05 13:51:48 +00:00
|
|
|
::waitpid(child, &result, 0);
|
|
|
|
else
|
2019-05-18 19:28:14 +00:00
|
|
|
::execve(m_Parent->Exe(), m_Parent->Args(), Env());
|
2019-05-05 13:51:48 +00:00
|
|
|
});
|
|
|
|
t.join();
|
2019-04-22 12:25:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-05 13:51:48 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ExecShellHookBackend::NotifyAsync(std::unordered_map<std::string, std::string> params)
|
2019-05-05 13:51:48 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto job = std::make_shared<ExecShellHookJob>(shared_from_this(), std::move(params));
|
2019-05-18 19:28:14 +00:00
|
|
|
|
|
|
|
m_ThreadPool.addJob(std::bind(&ExecShellHookJob::Exec, job));
|
2019-05-05 13:51:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
Backend_ptr
|
|
|
|
ExecShellBackend(std::string execFilePath)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
Backend_ptr ptr = std::make_shared<ExecShellHookBackend>(execFilePath);
|
|
|
|
if (!ptr->Start())
|
2019-04-22 12:25:25 +00:00
|
|
|
return nullptr;
|
|
|
|
return ptr;
|
|
|
|
}
|
2019-07-12 13:06:59 +00:00
|
|
|
#else
|
|
|
|
Backend_ptr ExecShellBackend(std::string)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-05-01 15:36:20 +00:00
|
|
|
#endif
|
2019-04-22 12:25:25 +00:00
|
|
|
} // namespace hooks
|
2019-04-27 10:48:01 +00:00
|
|
|
} // namespace llarp
|