-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptscorechecker.cpp
112 lines (105 loc) · 3.51 KB
/
scriptscorechecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "scriptscorechecker.h"
#include "boost/filesystem.hpp"
#ifdef __linux__
#include "boost/process.hpp"
#endif
ScriptScoreChecker::ScriptScoreChecker()
{
this->description="Run script and parse output";
this->points=1;
this->checkerType="RunScript";
this->desiredState=true;
}
void ScriptScoreChecker::setScript(std::string script)
{
this->script=script;
}
std::string ScriptScoreChecker::getScript()
{
return this->script;
}
void ScriptScoreChecker::setScriptExtension(std::string scriptExtension)
{
this->scriptExtension=scriptExtension;
}
std::string ScriptScoreChecker::getScriptExtension()
{
return this->scriptExtension;
}
void ScriptScoreChecker::setSearchString(std::string searchString)
{
this->searchString=searchString;
}
std::string ScriptScoreChecker::getSearchString()
{
return this->searchString;
}
void ScriptScoreChecker::setDesiredState(bool state)
{
this->desiredState=state;
}
bool ScriptScoreChecker::getDesiredState()
{
return this->desiredState;
}
void ScriptScoreChecker::checkState()
{
this->execute();
boost::regex expression(this->searchString);
this->state=(boost::regex_search(this->getOutput(),expression,boost::match_any)==this->desiredState); //logical xnor
/*std::regex r(this->searchString); // make regex
std::smatch m;
std::regex_search(this->scriptOutput, m, r);
this->state=((m.size()>0)==this->desiredState); //logical xnor
*/
//old - non regex
//this->state=((this->scriptOutput.find(this->searchString)!= std::string::npos)==this->desiredState); //logical xnor
}
std::string ScriptScoreChecker::getOutput()
{
return this->scriptOutput;
}
void ScriptScoreChecker::execute()
{
gen_scriptrun_directory();
std::string sfilename;
std::string file = "bash";
std::string ScriptExtension=this->getScriptExtension();
//file osfile.str();
sfilename=boost::filesystem::current_path().string() + "/tmp/" + file + ScriptExtension;
//std::cout << sfilename << std::endl;
if (!std::ifstream(sfilename)) //check for existence of file
{
std::ofstream ofs;
ofs.open(sfilename); //open file for writing
ofs << this->script;
ofs.close();
}else{std::cerr << "bash.sh already exists- FIX!" << std::endl;} //error if there is already a file
#ifdef _WIN32
if (ScriptExtension==".sh")exec(std::string("chmod +x " + sfilename).c_str()); //make script executable
if (ScriptExtension==".ps1")this->scriptOutput= exec(std::string("powershell " + sfilename).c_str());
else {this->scriptOutput= exec(sfilename.c_str());}
#elif __linux__
if (ScriptExtension==".sh")boost::process::system("chmod +x " + sfilename); //make script executable
if (ScriptExtension==".sh")
{
boost::process::ipstream output;
boost::process::system(sfilename, boost::process::std_out > output);
this->scriptOutput= std::string((std::istreambuf_iterator<char>(output)), std::istreambuf_iterator<char>());
}else
{
std::cerr << "I can't execute a non bash script on Linux!\n";
this->scriptOutput="";
}
#endif
boost::filesystem::remove_all(sfilename); //removes the script now that execution is done
}
bool gen_scriptrun_directory() //creates the runs/scans directory using boost
{
//boost::filesystem::path appPath(boost::filesystem::canonical(boost::filesystem::system_complete(argv[0])).parent_path());
if(boost::filesystem::create_directories("tmp")) {
//std::cout << "Success" << "\n";
return true;
}
return false;
}