-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinjcode.cc
135 lines (125 loc) · 4.17 KB
/
injcode.cc
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <cstdio>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include "inject.h"
#include "injcode.h"
options_t options;
const char* defaultModule = "test";
const float version = 0.11;
class ErrModule: public std::exception {
protected:
const std::string func;
const std::string msg;
std::string huh;
public:
ErrModule(const std::string &func,
const std::string &msg)
:func(func),msg(msg)
{
huh = func + ": " + msg;
}
virtual ~ErrModule() throw() {}
const char *what() const throw() { return huh.c_str(); };
};
static void
injcode()
{
Inject injector(options.targetpid, options.verbose, options.argv0);
if (options.moduleName == "retty") {
options.module.reset(new Retty(injector));
} else if (options.moduleName == "test") {
options.module.reset(new TestModule(injector));
} else if (options.moduleName == "close") {
options.module.reset(new CloseModule(injector));
} else if (options.moduleName == "dup2") {
options.module.reset(new Dup2Module(injector));
} else {
throw ErrModule("injcode",
std::string("Unknown module name: '")
+ options.moduleName
+ std::string("'\n"));
}
injector.run();
//injector.dumpregs(options.verbose < 2);
injector.detach();
options.module->run();
}
/**
*
*/
static
void usage(int err)
{
printf("Injcode %.2f, by Thomas Habets <[email protected]>\n"
"Usage: %s [ -hv ] [ -m <payload> ]\n"
"\t-h Show this help text\n"
"\t-m <payload> test/retty/close/dup2. Default: %s\n"
"\t-o<opt>=<val> Module-specific parameters\n"
"\t-v Increase verbosity.\n"
"\n"
" Close module\n"
"\t-ofd=<num> File descriptor to close\n"
"\n"
" Dup2 module\n"
"\t-ofd=<num> File descriptor to overwrite\n"
"\t-ofilename=<file> File to open()\n"
"\t-oflags=<num> open() flags\n"
,version, options.argv0, defaultModule);
exit(err);
}
int
main(int argc, char **argv)
{
// default options
options.verbose = 0;
options.argv0 = argv[0];
options.moduleName = defaultModule;
// option parsing
for(int c = 0; c != -1;) {
switch((c = getopt(argc, argv, "hm:o:v"))) {
case -1:
break;
case 'h':
usage(0);
break;
case 'm':
options.moduleName = optarg;
break;
case 'o': {
char *t = strchr(optarg, '=');
if (!t) {
usage(1);
}
options.parameters[std::string(optarg,t)]
= std::string(t+1);
}
break;
case 'v':
options.verbose++;
break;
default:
usage(1);
break;
}
}
if (argc != optind + 1) {
usage(1);
}
options.targetpid = atoi(argv[optind]);
try {
injcode();
} catch(const Inject::ErrMalformed &e) {
fprintf(stderr, "%s: %s\n", argv[0], e.whatMsg());
usage(1);
} catch(const Inject::ErrSysPtrace &e) {
fprintf(stderr, "%s: Unable to connect to pid: %s\n",
argv[0], e.what());
} catch(const std::exception &e) {
fprintf(stderr, "%s: Error: %s\n", argv[0], e.what());
} catch(...) {
fprintf(stderr, "%s: An error occured\n", argv[0]);
throw;
}
}