-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmycheck.c
233 lines (211 loc) · 5.63 KB
/
mycheck.c
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
/* -*- Mode: C; tab-width: 4; -*- */
/*
* Copyright (C) 2009, HustMoon Studio
*
* 文件名称:mycheck.c
* 摘 要:客户端校验算法
* 作 者:kkHAIKE
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mycheck.h"
#include "myini.h"
#include "md5.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static BYTE *bin_8021x = NULL;
static DWORD size_8021x;
#ifdef WORDS_BIGENDIAN
WORD ltobs(WORD x) {
return ((x & 0xff) << 8) | ((x & 0xff00) >> 8);
}
DWORD ltobl(DWORD x) {
return ((x & 0xff) << 24) |\
((x & 0xff00) << 8) |\
((x & 0xff0000) >> 8) |\
((x & 0xff000000) >> 24);
}
#endif
void hex_to_str(const BYTE *a, char *b, int hexsize, int upper) {
static const BYTE hex[][17]={"0123456789ABCDEF", "0123456789abcdef"};
BYTE *q = (BYTE *)b;
int i;
for (i=0; i<hexsize; i++) {
*q = hex[upper][a[i]>>4]; q++;
*q = hex[upper][a[i]&0xf]; q++;
}
*q = 0;
}
static BYTE *ReadCode(const char *file, DWORD *size) {
BYTE *data = NULL;
int i;
FILE *fp;
PPE_HEADER_MAP hpe;
if ((fp=fopen(file, "rb")) == NULL)
goto fileError;
data = (BYTE *)malloc(0x1000);
if (fread(data, 0x1000, 1, fp) < 1)
goto fileError;
hpe = (PPE_HEADER_MAP)(data + LTOBL(((PIMAGE_DOS_HEADER)data)->e_lfanew));
for (i=0; i<LTOBS(hpe->_head.NumberOfSections); i++) {
if (LTOBL(hpe->section_header[i].Characteristics) & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE)) {
fseek(fp, LTOBL(hpe->section_header[i].PointerToRawData), SEEK_SET);
*size = LTOBL(hpe->section_header[i].SizeOfRawData);
free(data);
data = (BYTE *)malloc(*size);
if (fread(data, *size, 1, fp) < 1)
goto fileError;
fclose(fp);
return data;
}
}
fileError:
if (fp != NULL)
fclose(fp);
if (data != NULL)
free(data);
return NULL;
}
static BYTE *ReadCode2(const char *dataFile, DWORD *size) {
BYTE Buf[16], *buf=Buf;
FILE *fp = NULL;
if ((fp=fopen(dataFile, "rb")) == NULL
|| fread(buf, 16, 1, fp ) < 1)
goto fileError;
*size = LTOBL(*(UINT4 *)buf ^ *(UINT4 *)(buf + 4));
if ((int)*size <= 0)
goto fileError;
buf = (BYTE *)malloc(*size+0x100);
if (fread(buf, *size, 1, fp) < 1) {
free(buf);
goto fileError;
}
fclose(fp);
return buf;
fileError:
if (fp != NULL)
fclose(fp);
return NULL;
}
static void decode_dat(BYTE *src, BYTE *dst, int src_len, int dst_len) {
BYTE tmp[0x8000], *sp, *dp, *s_end = src+src_len+1, *d_end = dst+dst_len, s, d;
DWORD i, m = 0, n = 0;
memset(tmp, 0x20, sizeof(tmp));
for (i=0; i<src_len; i++) {
src[i] = 255 - src[i];
}
for (sp=src, dp=dst, s=*sp++; sp<s_end; s=*sp++) {
for (i=0; i<8; i++) {
d = 1<<i;
if ((s&d) == 0) {
d = *sp++;
if (sp >= s_end)
return;
tmp[(m<<7)^n] = d;
} else {
d = tmp[(m<<7)^n];
}
*dp++ = d;
if (dp >= d_end) {
return;
}
m = n;
n = d;
}
}
}
int decodeConfig(const char *file, BYTE *dbuf, int dsize) {
char *sbuf;
int ssize = loadFile(&sbuf, file);
if (ssize < 0)
return -1;
decode_dat((BYTE *)sbuf, dbuf, ssize, dsize);
free(sbuf);
return 0;
}
void check_free() {
if (bin_8021x) {
free(bin_8021x);
bin_8021x = NULL;
}
}
int check_init(const char *dataFile) {
char name[0x100];
char *p;
check_free();
strcpy(name, dataFile);
if ((p=strrchr(name, '/')+1) == (void *)1)
p = name;
strcpy(p, "8021x.exe");
if ((bin_8021x=ReadCode(name, &size_8021x)) == NULL
&& (bin_8021x=ReadCode2(dataFile, &size_8021x)) == NULL)
return -1;
return 0;
}
void V2_check(const BYTE *seed, char *final_str) {
int i, size = size_8021x / 8;
BYTE table[144], *md5Dig, *b8021x = (BYTE *)malloc(size+16);
memcpy(b8021x, seed, 16);
for (i=0; i<8; i++) {
memcpy(b8021x+16, bin_8021x+size*i, size);
md5Dig = ComputeHash(b8021x, size+16);
table[18*i] = seed[2*i];
memcpy(table+18*i+1, md5Dig, 16);
table[18*i+17] = seed[2*i+1];
}
free(b8021x);
md5Dig = ComputeHash(table, 144);
hex_to_str(md5Dig, final_str, 16, 1);
}
DWORD getVer(const char *file) {
FILE *fp;
BYTE *data = NULL;
int i, j;
DWORD size, VirtualAddress;
PPE_HEADER_MAP hpe;
PIMAGE_RESOURCE_DIRECTORY prd;
PIMAGE_RESOURCE_DATA_ENTRY prde;
PVS_VERSIONINFO pvs;
if ((fp=fopen(file, "rb")) == NULL)
goto fileError;
data = (BYTE *)malloc(0x1000);
if (fread(data, 0x1000, 1, fp) < 1)
goto fileError;
hpe = (PPE_HEADER_MAP)(data + LTOBL(((PIMAGE_DOS_HEADER)data)->e_lfanew));
for (i=LTOBS(hpe->_head.NumberOfSections)-1; i>=0; i--) {
if (strcmp(hpe->section_header[i].Name, ".rsrc") == 0) {
fseek(fp, LTOBL(hpe->section_header[i].PointerToRawData), SEEK_SET);
size = LTOBL(hpe->section_header[i].SizeOfRawData);
VirtualAddress = LTOBL(hpe->section_header[i].VirtualAddress);
free(data);
data = (BYTE *)malloc(size);
if (fread(data, size, 1, fp) < 1)
goto fileError;
prd = (PIMAGE_RESOURCE_DIRECTORY)data;
for (j=0; j<LTOBS(prd->NumberOfIdEntries); j++) {
prd->DirectoryEntries[j].Name = LTOBL(prd->DirectoryEntries[j].Name);
if (prd->DirectoryEntries[j].Id==16 && prd->DirectoryEntries[j].NameIsString==0) {
prd->DirectoryEntries[j].OffsetToData = LTOBL(prd->DirectoryEntries[j].OffsetToData);
prd = (PIMAGE_RESOURCE_DIRECTORY)(data+prd->DirectoryEntries[j].OffsetToDirectory);
prd->DirectoryEntries[0].OffsetToData = LTOBL(prd->DirectoryEntries[0].OffsetToData);
prd = (PIMAGE_RESOURCE_DIRECTORY)(data+prd->DirectoryEntries[0].OffsetToDirectory);
prde = (PIMAGE_RESOURCE_DATA_ENTRY)(data+LTOBL(prd->DirectoryEntries[0].OffsetToData));
pvs = (PVS_VERSIONINFO)(data+LTOBL(prde->OffsetToData)-VirtualAddress);
size = pvs->Value.dwFileVersionMS;
fclose(fp);
free(data);
return size;
}
}
goto fileError;
}
}
fileError:
if (fp != NULL)
fclose(fp);
if (data != NULL)
free(data);
return -1;
}