Skip to content

Commit 2d523c3

Browse files
authored
Merge pull request #2 from 0xvpr/dev
Main <- Dev
2 parents 6415bbf + 7377af3 commit 2d523c3

File tree

2 files changed

+107
-32
lines changed

2 files changed

+107
-32
lines changed

makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,12 @@ $(BUILD):
5252
mkdir -p $@/debug
5353
mkdir -p $@/release
5454

55-
56-
.PHONY: tests
57-
tests:
58-
make -C Tests/ELF all
59-
make -C Tests/PE all
60-
6155
.PHONY: install
6256
install: release
57+
cp $(BIN)/$(TARGET).exe $(BIN)/$(TARGET)
6358
install -d $(PREFIX)/bin
6459
install -m 555 $(BIN)/$(TARGET) $(PREFIX)/bin
60+
rm $(BIN)/$(TARGET)
6561

6662
.PHONY: clean
6763
clean:

src/main.c

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
#define OP_CUSTOM (1 << 0)
77
#define OP_SILENT (1 << 31)
88

9-
_Noreturn void __usage_error(char* msg, char* argv_0)
9+
static int operation = 0;
10+
static char* argv_0;
11+
12+
_Noreturn void __usage_error(char* msg)
1013
{
1114
fprintf(stderr,
1215
"Error message: %s.\n"
1316
"\n"
14-
"Usage: %s <path/to/file> [path/to/other/files] [ <optional arguments> ]\n"
17+
"Usage: \n"
18+
" %s <path/to/file> [path/to/other/files] [ <optional arguments> ]\n"
1519
"\n"
1620
"positional arguments:\n"
1721
" path/to/payload\n"
@@ -22,19 +26,20 @@ _Noreturn void __usage_error(char* msg, char* argv_0)
2226
" -s,--silent\tsuppress the standard output\n"
2327
"\n"
2428
"example:\n"
25-
" %s test.txt -c \"19990101000000\""
29+
" %s test.txt -c \"19990101000000\"\n"
2630
, msg, argv_0, argv_0);
2731
exit(-1);
2832
}
2933

30-
int parse_command_line(int argc, char** argv)
34+
char* parse_command_line(int argc, char** argv)
3135
{
32-
int operation = 0;
36+
37+
char* datetime = (char *)calloc(14, sizeof(char));
3338
char buffer[32];
3439

3540
if (argc < 2)
3641
{
37-
__usage_error(argv[0], "No arguments supplied");
42+
__usage_error("No arguments supplied");
3843
}
3944

4045
for (int i = 1; i < argc; i++)
@@ -46,9 +51,22 @@ int parse_command_line(int argc, char** argv)
4651
switch (argv[i][1])
4752
{
4853
case 'c':
49-
case 'C':
5054
{
51-
operation |= OP_CUSTOM;
55+
if (i+1 >= argc)
56+
{
57+
__usage_error("No format specified");
58+
}
59+
else
60+
{
61+
operation |= OP_CUSTOM;
62+
strncpy(datetime, argv[i+1], sizeof("YYYYMMDDHHMMSS"));
63+
}
64+
65+
break;
66+
}
67+
case 's':
68+
{
69+
operation |= OP_SILENT;
5270
break;
5371
}
5472
case '-':
@@ -60,20 +78,83 @@ int parse_command_line(int argc, char** argv)
6078
}
6179
else if (!strncmp("custom", buffer, sizeof("custom")))
6280
{
63-
operation |= OP_CUSTOM;
81+
if (i+1 >= argc)
82+
{
83+
__usage_error("No format specified");
84+
}
85+
else
86+
{
87+
operation |= OP_CUSTOM;
88+
strncpy(datetime, argv[i+1], sizeof("YYYYMMDDHHMMSS"));
89+
}
6490
}
6591
else
6692
{
67-
__usage_error(argv[0], "unknown option supplied");
93+
__usage_error("unknown option supplied");
6894
}
6995
break;
7096
}
97+
default:
98+
{
99+
__usage_error("unknown option supplied");
100+
}
71101
}
72102
}
73103
}
74104
}
75105

76-
return operation;
106+
return datetime;
107+
}
108+
109+
void SetDefaultTime(LPSYSTEMTIME pSystemTime)
110+
{
111+
GetLocalTime(pSystemTime);
112+
pSystemTime->wYear = 1999;
113+
pSystemTime->wMonth = 12;
114+
pSystemTime->wDay = 31;
115+
pSystemTime->wDayOfWeek = 5;
116+
pSystemTime->wHour = 0;
117+
pSystemTime->wMinute = 0;
118+
pSystemTime->wSecond = 0;
119+
pSystemTime->wMilliseconds = 0;
120+
}
121+
122+
void SetCustomTime(LPSYSTEMTIME pSystemTime, LPCSTR fmt)
123+
{
124+
if (strlen(fmt) != 14)
125+
{
126+
__usage_error("Invalid datetime format");
127+
}
128+
129+
for (int i = 0; i < 14; i++)
130+
{
131+
if ((fmt[i] < '0') || (fmt[i] > '9'))
132+
{
133+
__usage_error("Invalid character detected");
134+
}
135+
}
136+
137+
char buffer_wYear[5] = { 0 };
138+
char buffer_wMonth[3] = { 0 };
139+
char buffer_wDay[3] = { 0 };
140+
char buffer_wHour[3] = { 0 };
141+
char buffer_wMinute[3] = { 0 };
142+
char buffer_wSecond[3] = { 0 };
143+
144+
memcpy(buffer_wYear, fmt+0, sizeof(buffer_wYear)-1);
145+
memcpy(buffer_wMonth, fmt+4, sizeof(buffer_wMonth)-1);
146+
memcpy(buffer_wDay, fmt+6, sizeof(buffer_wDay)-1);
147+
memcpy(buffer_wHour, fmt+8, sizeof(buffer_wHour)-1);
148+
memcpy(buffer_wMinute, fmt+10, sizeof(buffer_wMinute)-1);
149+
memcpy(buffer_wSecond, fmt+12, sizeof(buffer_wSecond)-1);
150+
151+
sscanf(buffer_wYear, "%hu", &(pSystemTime->wYear));
152+
sscanf(buffer_wMonth, "%hu", &(pSystemTime->wMonth));
153+
sscanf(buffer_wDay, "%hu", &(pSystemTime->wDay));
154+
sscanf(buffer_wHour, "%hu", &(pSystemTime->wHour));
155+
sscanf(buffer_wMinute, "%hu", &(pSystemTime->wMinute));
156+
sscanf(buffer_wSecond, "%hu", &(pSystemTime->wSecond));
157+
sscanf(buffer_wYear, "%hu", &(pSystemTime->wYear));
77158
}
78159

79160
int main(int argc, char** argv)
@@ -83,10 +164,10 @@ int main(int argc, char** argv)
83164
SYSTEMTIME system_time;
84165
FILETIME file_time;
85166
LPCSTR file_name;
86-
LPCSTR datetime;
87-
BOOL operation;
167+
LPSTR datetime;
88168

89-
operation = parse_command_line(argc, argv);
169+
argv_0 = argv[0];
170+
datetime = parse_command_line(argc, argv);
90171

91172
for (int i = 1; i < argc; i++)
92173
{
@@ -101,16 +182,14 @@ int main(int argc, char** argv)
101182
fclose(fp);
102183
}
103184

104-
GetLocalTime(&system_time);
105-
106-
system_time.wYear = 1999;
107-
system_time.wMonth = 12;
108-
system_time.wDay = 31;
109-
system_time.wDayOfWeek = 5;
110-
system_time.wMilliseconds = 0;
111-
system_time.wMinute = 0;
112-
system_time.wHour = 0;
113-
system_time.wSecond = 0;
185+
if (operation & 1)
186+
{
187+
SetCustomTime(&system_time, datetime);
188+
}
189+
else
190+
{
191+
SetDefaultTime(&system_time);
192+
}
114193

115194
SystemTimeToFileTime(&system_time, &file_time);
116195
hFile = CreateFile(file_name, // File name
@@ -125,12 +204,12 @@ int main(int argc, char** argv)
125204
CloseHandle(hFile);
126205

127206
datetime = datetime ? datetime : "default";
128-
if (!(operation & OP_SILENT))
207+
if ((operation >> 31) & 1)
129208
{
130209
fprintf(stdout, "'%s' successfully processed to date-time: %s", argv[i], datetime);
131210
}
132211
}
133-
134212

213+
free(datetime);
135214
return 0;
136215
}

0 commit comments

Comments
 (0)