forked from jcramb/revshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cc
163 lines (134 loc) · 3.96 KB
/
core.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
////////////////////////////////////////////////////////////////////////////////
// core.cc
// author: [email protected]
#include "core.h"
#include <ctype.h>
#include <string>
#include <cstring>
#include <cstdarg>
#include <cstdio>
////////////////////////////////////////////////////////////////////////////////
// debugging log global vars
int g_logflags = 0;
std::string g_logpath;
////////////////////////////////////////////////////////////////////////////////
// set log flags and filename
void log_init(const char * prefix, int flags) {
char * p = (char*)prefix;
while (!isalpha(*p)) p++;
g_logpath = ".";
g_logpath += p;
g_logpath += "_log";
log_flags(flags);
}
////////////////////////////////////////////////////////////////////////////////
// set log flags
void log_flags(int flags) {
g_logflags = flags;
}
////////////////////////////////////////////////////////////////////////////////
// debug log printing (to file / echo)
void log_print(const char * fmt, ...) {
char logbuf[LOG_BUFSIZE] = {0};
static bool created = false;
va_list va;
va_start(va, fmt);
vsnprintf(logbuf, LOG_BUFSIZE, fmt, va);
va_end(va);
if (g_logflags & LOG_ECHO) {
printf("%s", logbuf);
fflush(stdout);
}
if (g_logflags & LOG_FILE) {
FILE * f;
if (created) {
f = fopen(g_logpath.c_str(), "a");
} else {
f = fopen(g_logpath.c_str(), "w");
created = true;
}
if (f) {
fprintf(f, "%s", logbuf);
fclose(f);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// prints binary buffer in hex + ascii format
void hexdump(const char * buf, int len, int cols, bool ascii) {
int pos = 0;
int bytes_left = len;
const int word_size = 4;
while (bytes_left) {
// determine how many columns for this row
int c = MIN(bytes_left, cols);
bytes_left -= c;
LOG(" %4x:", pos);
// print hex bytes
for (int i = 0; i < c; i++) {
if (i % word_size == 0) LOG(" ");
LOG(" %02x", buf[pos+i] & 0xff);
}
// do we need to add the ascii column?
if (ascii) {
// pad spacing for missing hex bytes
int empty_bytes = cols - c;
for (int i = 0; i <= empty_bytes; i++) {
LOG(" ");
}
// pad for missing word column spacing
for (int i = 0; i < empty_bytes / word_size; i++) {
LOG(" ");
}
// print ascii bytes
for (int i = 0; i < c; i++) {
LOG("%c", isprint(buf[pos+i]) ? buf[pos+i] & 0xff : '.');
}
}
pos += c;
LOG("\n");
}
}
////////////////////////////////////////////////////////////////////////////////
// transport message implementation
message::message(int type, size_t len) {
memset(&m_data, 0, sizeof(m_data));
m_data.type = type;
resize(len);
}
message::message(int type, const char * buf, size_t len) {
m_data.type = type;
resize(len);
memcpy(body(), buf, body_len());
}
message::message(const char * buf, size_t len) {
memcpy(data(), buf, header_len);
m_data.body_len = MAX(0, MIN(len, body_max_len));
memcpy(body(), buf + header_len, m_data.body_len);
}
char * message::data() {
return (char*)&m_data;
}
const char * message::data() const {
return (char*)&m_data;
}
char * message::body() {
return m_data.body;
}
const char * message::body() const {
return m_data.body;
}
int message::type() const {
return m_data.type;
}
size_t message::data_len() const {
return header_len + m_data.body_len;
}
size_t message::body_len() const {
return m_data.body_len;
}
size_t message::resize(size_t len) {
m_data.body_len = MIN(len, body_max_len);
return m_data.body_len;
}
////////////////////////////////////////////////////////////////////////////////