forked from baiyongrui/ruijie_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mentohust.c
282 lines (231 loc) · 6.6 KB
/
mentohust.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
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
/*
* memtohust
* 原作者:HustMoon@BYHH
* 邮箱:www.ehust@gmail.com
*
* ruijie_helper
* 此项目是在memtohust基础上修改
* 作者:baiyongrui
* 邮箱:baiyongrui@icloud.com
*/
#include <tchar.h>
#include <Winsock2.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <mmsystem.h>
#include <Tlhelp32.h>
#include <shlwapi.h>
#include <Psapi.h>
#include <pcap.h>
#include "myconfig.h"
#include "mystate.h"
#include "myfunc.h"
#include "myini.h"
#include <signal.h>
#include <string.h>
#include <locale.h>
//#include <stdlib.h>
extern pcap_t *hPcap;
extern volatile int state;
extern u_char *fillBuf;
extern const u_char *capBuf;
extern unsigned startMode, dhcpMode, maxFail;
extern u_char destMAC[];
extern MMRESULT timer_id;
extern short needSaveNic;
extern WCHAR wNic[];
extern void getStringByKeyName(LPWSTR key, LPWSTR buf, DWORD bufSize);
extern void writeStringForKeyName(LPWSTR key, LPWSTR value);
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo;
static LPCWSTR path = L"C:\\Program Files\\锐捷网络\\Ruijie Supplicant\\RuijieSupplicant.exe";
//static TCHAR realPath[MAX_PATH];
static DWORD pathSize = MAX_PATH;
static TCHAR realPath[MAX_PATH] = {0};
#ifndef NO_ARP
extern u_int32_t rip, gateway;
extern u_char gateMAC[];
#endif
static void exit_handle(void); /* 退出回调 */
static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char *buf); /* pcap_loop回调 */
static void showRuijieMsg(const u_char *buf, unsigned bufLen); /* 显示锐捷服务器提示信息 */
static DWORD findAndKill8021X();
void sig_handle(int sig); /* 信号回调 */
int main(int argc, char **argv)
{
atexit(exit_handle);
setlocale(LC_ALL,"chs");
// PROCESS_INFORMATION ProcessInfo;
// STARTUPINFO StartupInfo;
getStringByKeyName(L"RjPath",realPath,MAX_PATH);
if (wcslen(realPath) != 0 || wcscmp(realPath,L"") != 0) {
wprintf(L"Found %ls! Ready to Launch.\n",realPath);
ZeroMemory(&StartupInfo,sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo;
if (0 == CreateProcess(realPath,NULL,NULL,NULL,FALSE,NULL,NULL,NULL,&StartupInfo,&ProcessInfo))
{
wprintf(L"Failed to open %ls!\n",path);
findRuijieSupplicant();
}
}
else {
findRuijieSupplicant();
}
initConfig(argc, argv);
if (-1 == pcap_loop(hPcap, -1, pcap_handle, NULL)) { /* 开始捕获数据包 */
wprintf(L"!! 捕获数据包失败,请检查网络连接!\n");
}
exit(EXIT_FAILURE);
}
static void exit_handle(void)
{
//if (state != ID_DISCONNECT)
// switchState(ID_DISCONNECT);
// 暂不做断开处理
if (hPcap != NULL)
pcap_close(hPcap);
if (fillBuf != NULL)
free(fillBuf);
if(timer_id > 0)
timeKillEvent(timer_id);
}
static DWORD findAndKill8021X()
{
TCHAR *p = L"8021x.exe";
HANDLE hSnapshot;
PROCESSENTRY32 lppe;
//创建系统快照
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); //#include<Tlhelp32.h>
if (hSnapshot == NULL)
return 0;
//初始化 lppe 的大小
lppe.dwSize = sizeof(lppe);
//查找第一个进程
if (!Process32First(hSnapshot, &lppe))
return 0;
do
{
if(StrCmp(lppe.szExeFile, p) == 0)//#include<shlwapi.h>
{
printf("Found 8021x.exe!Ready to kill.\n");
if(TerminateProcess(OpenProcess(PROCESS_TERMINATE,FALSE,lppe.th32ProcessID),0))
printf("Ruijie has been slain!!!\n");
//return lppe.th32ProcessID;
return 1;
}
}
while (Process32Next(hSnapshot, &lppe)); //查找下一个进程
return 1;
}
static DWORD findRuijieSupplicant()
{
TCHAR *p = L"8021x.exe";
TCHAR *replaceName = L"RuijieSupplicant.exe";
HANDLE hSnapshot;
PROCESSENTRY32 lppe;
printf("Waiting RuijieSupplicant process...\n");
//创建系统快照
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); //#include<Tlhelp32.h>
if (hSnapshot == NULL)
return 0;
//初始化 lppe 的大小
lppe.dwSize = sizeof(lppe);
while (1) {
//查找第一个进程
if (!Process32First(hSnapshot, &lppe))
return 0;
do
{
if(StrCmp(lppe.szExeFile, p) == 0)//#include<shlwapi.h>
{
if(QueryFullProcessImageName(OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,lppe.th32ProcessID),0,realPath,(PDWORD)&pathSize))
{
wprintf(L"Found %ls! Save to config.ini\n",realPath);
wcscpy(wcsstr(realPath,p),replaceName);
writeStringForKeyName(L"RjPath",realPath);
}
return 1;
}
}
while (Process32Next(hSnapshot, &lppe)); //查找下一个进程
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
}
return 1;
}
void sig_handle(int sig)
{
if (-1 == switchState(state))
{
pcap_breakloop(hPcap);
wprintf(L"!! 发送数据包失败, 请检查网络连接!\n");
exit(EXIT_FAILURE);
}
//if (sig == SIGALRM) /* 定时器 */
//{
// if (-1 == switchState(state))
// {
// pcap_breakloop(hPcap);
// printf("!! 发送数据包失败, 请检查网络连接!\n");
// exit(EXIT_FAILURE);
// }
//}
//else /* 退出 */
//{
// pcap_breakloop(hPcap);
// exit(EXIT_SUCCESS);
//}
}
static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char *buf)
{
if (buf[0x0c]==0x88 && buf[0x0d]==0x8e) {
if (buf[0x0F]==0x00 && buf[0x12]==0x03) { /* 认证成功 */
printf(">> Authentication succeed!\n");
findAndKill8021X();
if (needSaveNic) {
writeStringForKeyName(L"Nic",wNic);
needSaveNic = 0;
}
getEchoKey(buf);
showRuijieMsg(buf, h->caplen);
switchState(ID_ECHO);
}
}
}
static void showRuijieMsg(const u_char *buf, unsigned bufLen)
{
//WCHAR *serverMsgW;
char *serverMsg;
int length = buf[0x1b];
if (length > 0) {
for (serverMsg=(char *)(buf+0x1c); *serverMsg=='\r'||*serverMsg=='\n'; serverMsg++,length--);
if (strlen(serverMsg) < length)
length = strlen(serverMsg);
//if (length>0 && (serverMsg=gbk2utf(serverMsg, length))!=NULL) {
if (length > 0) {
//serverMsgW = (WCHAR *)malloc((sizeof(WCHAR) * length) + 1);
//memset(serverMsgW,'\0',sizeof(serverMsgW));
//MultiByteToWideChar(0,0,serverMsg,strlen(serverMsg),serverMsgW,length/2);
if (strlen(serverMsg)) {
printf("$$ 系统提示:\t%s\n", serverMsg);
}
//free(serverMsgW);
}
}
if ((length=0x1c+buf[0x1b]+0x69+39) < bufLen) {
serverMsg=(char *)(buf+length);
if (buf[length-1]-2 > bufLen-length)
length = bufLen - length;
else
length = buf[length-1]-2;
for (; *serverMsg=='\r'||*serverMsg=='\n'; serverMsg++,length--);
if (length > 0) {
//serverMsgW = (WCHAR *)malloc((sizeof(WCHAR) * length) + 1);
//memset(serverMsgW,'\0',sizeof(serverMsgW));
//MultiByteToWideChar(0,0,serverMsg,strlen(serverMsg),serverMsgW,(length/2)+1);
if (strlen(serverMsg)) {
printf("$$ 计费提示:\t%s\n", serverMsg);
}
//free(serverMsgW);
}
}
}