Skip to content

Commit

Permalink
cxx/* posixExec -> execves + execvex
Browse files Browse the repository at this point in the history
  `posixExec()` was produced before had access to `g++` / `clang++`, thus had false
  assumptions about how to use `execve`.

`execves()` now `fork()`s, wraps `execve()` (but accepts std::string inputs, does not
require you to terminate lists with `NULL`, plus allows to omit `envp`),
plus `waitpid()`s.

`execvex()` wraps `execves()`, to do what `posixExec()` was advertised as.

@posts/VirusAnalysis.md /* Reflects this */
  • Loading branch information
SwuduSusuwu committed Jun 17, 2024
1 parent caecee3 commit 9a1aae7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 45 deletions.
36 changes: 24 additions & 12 deletions cxx/ClassCns.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@
#include <vector> /* std::vector */
#include <ctype.h> /* size_t */
#ifdef _POSIX_VERSION
#include <unistd.h> /* execve */
#include <unistd.h> /* execve fork EXIT_FAILURE */
#include <sys/wait.h> /* waitpid */
#endif /* def _POSIX_VERSION */
#include "ClassCns.hxx" /* CnsMode */
namespace Susuwu {
const int posixExec(const std::string &executable, const std::string &argsS, const std::string &envVarsS) {
const int execves(const std::string &executable, const std::vector<const std::string> &argvS, const std::vector<const std::string> &envpS) {
#ifdef _POSIX_VERSION
char *args[] = {
const_cast<char *>(executable.c_str()),
const_cast<char *>(argsS.c_str()),
NULL
};
char *envVars[] = {
const_cast<char *>(envVarsS.c_str()),
NULL
};
return execve(args[0], args, envVars);
pid_t pid = fork();
if(0 != pid) {
int status;
assert(-1 != pid);
waitpid(pid, &status, 0);
return status;
} /* if 0, is fork */
const std::vector<std::string> argvSmutable = {argvS.cbegin(), argvS.cend()};
std::vector<char *> argv;
for(auto x : argvSmutable) {
argv.push_back(const_cast<char *>(x.c_str()));
}
argv.push_back(NULL);
const std::vector<std::string> envpSmutable = {envpS.cbegin(), envpS.cend()};
std::vector<char *> envp;
for(auto x : envpSmutable) {
envp.push_back(const_cast<char *>(x.c_str()));
}
envp.push_back(NULL);
execve(executable.c_str(), &argv[0], &envp[0]); /* NORETURN */
exit(EXIT_FAILURE);
#endif /* def _POSIX_VERSION */
}

Expand Down
3 changes: 2 additions & 1 deletion cxx/ClassCns.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ typedef enum CnsMode {
} CnsMode;

/* @pre @code std::ifstream(executable); @endcode */
const int posixExec(const std::string &executable, const std::string &argsS = "", const std::string &envVarsS = "");
const int execves(const std::string &executable, const std::vector<const std::string> &argvS = {}, const std::vector<const std::string> &envpS = {});
static const int execvex(const std::string &toSh) {return execves("/bin/sh", {"/bin/sh", "-c", toSh});}
typedef class Cns {
public:
virtual const bool hasImplementation() const {return typeid(Cns) != typeid(this);}
Expand Down
8 changes: 4 additions & 4 deletions cxx/ConversationCns.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <tuple> /* std::tuple */
#include "ClassSha2.hxx" /* Sha2 */
#include "ClassPortableExecutable.hxx" /* FilePath FileBytecode */
#include "ClassCns.hxx" /* Cns, CnsMode, posixExec */
#include "ClassCns.hxx" /* Cns, CnsMode, execvex */
#include "ClassResultList.hxx" /* ResultList listMaxSize listHasValue explodeToList ResultListBytecode */
#include "ConversationCns.hxx" /* conversationParseUrls conversationParseQuestion conversationParseResponses */
/* (Work-in-progress) conversation bots with artificial CNS. */
Expand Down Expand Up @@ -51,8 +51,8 @@ void produceConversationCns(const ResultList &questionsOrNull, const ResultList

void questionsResponsesFromHosts(ResultList &questionsOrNull, ResultList &responsesOrNull, const std::vector<FilePath> &hosts) {
for(auto host : hosts) {
posixExec("/bin/wget", "'" + host + "/robots.txt' > robots.txt", NULL);
posixExec("/bin/wget", "'" + host + "' > index.xhtml", NULL);
execvex("wget '" + host + "/robots.txt' -Orobots.txt");
execvex("wget '" + host + "' -Oindex.xhtml");
questionsOrNull.signatures.push_back(host);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, "index.xhtml");
}
Expand Down Expand Up @@ -80,7 +80,7 @@ void questionsResponsesFromXhtml(ResultList &questionsOrNull, ResultList &respon
auto urls = conversationParseUrls(xhtmlFile);
for(auto url : urls) {
if(!listHasValue(questionsOrNull.signatures, url) && !listHasValue(noRobots, url)) {
posixExec("/bin/wget", "'" + url + "' > " + xhtmlFile, NULL);
execvex("wget '" + url + "' -O" + xhtmlFile);
questionsOrNull.signatures.push_back(url);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, xhtmlFile);
}
Expand Down
12 changes: 6 additions & 6 deletions cxx/VirusAnalysis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <algorithm> /* std::sort */
#include <utility> /* std::get */
#include "ClassSha2.hxx" /* Sha2 */
#include "ClassCns.hxx" /* Cns, CnsMode, posixExec */
#include "ClassCns.hxx" /* Cns, CnsMode, execvex */
#include "ClassResultList.hxx" /* ResultList, listMaxSize, listHasValue, ResultList, listProduceUniqueSubstr, listOfSubstrHasMatch */
#include "ClassPortableExecutable.hxx" /* PortableExecutable */
#include "VirusAnalysis.hxx" /* passList, abortList, *AnalyisCaches */
Expand Down Expand Up @@ -136,11 +136,11 @@ const VirusAnalysisResult sandboxAnalysis(const PortableExecutable &file, const
const auto result = sandboxAnalysisCaches.at(fileHash);
return result;
} catch (...) {
posixExec("/bin/cp", "-r '/usr/home/sandbox/' '/usr/home/sandbox.bak'"); /* or produce FS snapshot */
posixExec("/bin/cp", "'" + file.path + "' '/usr/home/sandbox/'");
posixExec("/bin/chroot", "'/usr/home/sandbox/' \"strace basename '" + file.path + "'\" >> strace.outputs");
posixExec("/bin/mv/", "'/usr/home/sandbox/strace.outputs' '/tmp/strace.outputs'");
posixExec("/bin/sh", "-c 'rm -r /usr/home/sandbox/ && mv /usr/home/sandbox.bak /usr/home/sandbox/'"); /* or restore FS snapshot */
execvex("cp -r '/usr/home/sandbox/' '/usr/home/sandbox.bak'"); /* or produce FS snapshot */
execvex("cp '" + file.path + "' '/usr/home/sandbox/'");
execvex("chroot '/usr/home/sandbox/' \"strace basename '" + file.path + "'\" >> strace.outputs");
execvex("mv/ '/usr/home/sandbox/strace.outputs' '/tmp/strace.outputs'");
execvex("rm -r '/usr/home/sandbox/' && mv '/usr/home/sandbox.bak' '/usr/home/sandbox/'"); /* or restore FS snapshot */
return sandboxAnalysisCaches[fileHash] = straceOutputsAnalysis("/tmp/strace.outputs");
}
}
Expand Down
56 changes: 34 additions & 22 deletions posts/VirusAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ typedef enum CnsMode {
} CnsMode;
/* @pre @code std::ifstream(executable); @endcode */
const int posixExec(const std::string &executable, const std::string &argsS = "", const std::string &envVarsS = "");
const int execves(const std::string &executable, const std::vector<const std::string> &argvS = {}, const std::vector<const std::string> &envpS = {});
static const int execvex(const std::string &toSh) {return execves("/bin/sh", {"/bin/sh", "-c", toSh});}
typedef class Cns {
public:
virtual const bool hasImplementation() const {return typeid(Cns) != typeid(this);}
Expand Down Expand Up @@ -249,18 +250,29 @@ typedef class ApxrCns : Cns {
```
`less` [cxx/ClassCns.cxx](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/ClassCns.cxx)
```
const int posixExec(const std::string &executable, const std::string &argsS, const std::string &envVarsS) {
const int execves(const std::string &executable, const std::vector<const std::string> &argvS, const std::vector<const std::string> &envpS) {
#ifdef _POSIX_VERSION
char *args[] = {
const_cast<char *>(executable.c_str()),
const_cast<char *>(argsS.c_str()),
NULL
};
char *envVars[] = {
const_cast<char *>(envVarsS.c_str()),
NULL
};
return execve(args[0], args, envVars);
pid_t pid = fork();
if(0 != pid) {
int status;
assert(-1 != pid);
waitpid(pid, &status, 0);
return status;
} /* if 0, is fork */
const std::vector<std::string> argvSmutable = {argvS.cbegin(), argvS.cend()};
std::vector<char *> argv;
for(auto x : argvSmutable) {
argv.push_back(const_cast<char *>(x.c_str()));
}
argv.push_back(NULL);
const std::vector<std::string> envpSmutable = {envpS.cbegin(), envpS.cend()};
std::vector<char *> envp;
for(auto x : envpSmutable) {
envp.push_back(const_cast<char *>(x.c_str()));
}
envp.push_back(NULL);
execve(executable.c_str(), &argv[0], &envp[0]); /* NORETURN */
exit(EXIT_FAILURE);
#endif /* def _POSIX_VERSION */
}
Expand Down Expand Up @@ -539,11 +551,11 @@ const VirusAnalysisResult sandboxAnalysis(const PortableExecutable &file, const
const auto result = sandboxAnalysisCaches.at(fileHash);
return result;
} catch (...) {
posixExec("/bin/cp", "-r '/usr/home/sandbox/' '/usr/home/sandbox.bak'"); /* or produce FS snapshot */
posixExec("/bin/cp", "'" + file.path + "' '/usr/home/sandbox/'");
posixExec("/bin/chroot", "'/usr/home/sandbox/' \"strace basename '" + file.path + "'\" >> strace.outputs");
posixExec("/bin/mv/", "'/usr/home/sandbox/strace.outputs' '/tmp/strace.outputs'");
posixExec("/bin/sh", "-c 'rm -r /usr/home/sandbox/ && mv /usr/home/sandbox.bak /usr/home/sandbox/'"); /* or restore FS snapshot */
execvex("cp -r '/usr/home/sandbox/' '/usr/home/sandbox.bak'"); /* or produce FS snapshot */
execvex("cp '" + file.path + "' '/usr/home/sandbox/'");
execvex("chroot '/usr/home/sandbox/' \"strace basename '" + file.path + "'\" >> strace.outputs");
execvex("mv/ '/usr/home/sandbox/strace.outputs' '/tmp/strace.outputs'");
execvex("rm -r '/usr/home/sandbox/' && mv '/usr/home/sandbox.bak' '/usr/home/sandbox/'"); /* or restore FS snapshot */
return sandboxAnalysisCaches[fileHash] = straceOutputsAnalysis("/tmp/strace.outputs");
}
}
Expand Down Expand Up @@ -755,9 +767,9 @@ void produceConversationCns(const ResultList &questionsOrNull, const ResultList
void questionsResponsesFromHosts(ResultList &questionsOrNull, ResultList &responsesOrNull, const std::vector<FilePath> &hosts) {
for(auto host : hosts) {
posixExec("/bin/wget", "'" + host + "/robots.txt' > robots.txt", NULL);
posixExec("/bin/wget", "'" + host + "' > index.xhtml", NULL);
questionsOrNull.signatures.push_back(host);
execvex("wget '" + host + "/robots.txt' -Orobots.txt");
execvex("wget '" + host + "' -Oindex.xhtml");
questionsOrNull.signatures.push_back(host);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, "index.xhtml");
}
}
Expand All @@ -784,8 +796,8 @@ void questionsResponsesFromXhtml(ResultList &questionsOrNull, ResultList &respon
auto urls = conversationParseUrls(xhtmlFile);
for(auto url : urls) {
if(!listHasValue(questionsOrNull.signatures, url) && !listHasValue(noRobots, url)) {
posixExec("/bin/wget", "'" + url + "' > " + xhtmlFile, NULL);
questionsOrNull.signatures.push_back(url);
execvex("wget '" + url + "' -O" + xhtmlFile);
questionsOrNull.signatures.push_back(url);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, xhtmlFile);
}
}
Expand Down

0 comments on commit 9a1aae7

Please sign in to comment.