Skip to content

Commit

Permalink
Add an option for specifing service name
Browse files Browse the repository at this point in the history
* We can use another service to +10min * 3.
  • Loading branch information
updateing committed May 31, 2016
1 parent f454e78 commit 4b63016
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/myconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static const char *CREDIT_ADDITION = "V4 Algorithm by Hu Yunrui, new features by

#define ACCOUNT_SIZE 65 /* 用户名密码长度*/
#define NIC_SIZE 16 /* 网卡名最大长度 */
#define SERVICE_SIZE 127 /* 服务名最大长度 */
#define MAX_PATH 255 /* FILENAME_MAX */
#define D_TIMEOUT 8 /* 默认超时间隔 */
#define D_ECHOINTERVAL 30 /* 默认心跳间隔 */
Expand All @@ -47,6 +48,7 @@ static const char *CREDIT_ADDITION = "V4 Algorithm by Hu Yunrui, new features by
#define D_MAXRETRIES 0 /* 默认认证服务器无响应时允许重试的次数,0为无限 */
#define D_PROXYMODE 0 /* 默认禁用代理模式 */
#define D_SUCCESS_COUNT 1 /* 默认代理需求成功次数 */
#define D_SERVICENAME "internet" /* 默认要登录的服务名 */

#define ECHOFLAGS (ECHO|ECHOE|ECHOK|ECHONL) /* 控制台输入密码时的模式*/

Expand All @@ -72,6 +74,7 @@ char nic[NIC_SIZE] = ""; /* 发出认证数据包的网卡名(WAN) */
char nicLan[NIC_SIZE] = ""; /* 监听认证数据包的网卡名(LAN,仅在代理模式下使用) */
char dataFile[MAX_PATH] = ""; /* 数据文件 */
char dhcpScript[MAX_PATH] = ""; /* DHCP脚本 */
char serviceName[SERVICE_SIZE] = ""; /* 需要登陆到的服务名 */
u_int32_t ip = 0; /* 本机IP */
u_int32_t mask = 0; /* 子网掩码 */
u_int32_t gateway = 0; /* 网关 */
Expand Down Expand Up @@ -224,6 +227,7 @@ void initConfig(int argc, char **argv)
int saveFlag = 0; /* 是否需要保存参数 */
int exitFlag = 0; /* 0Nothing 1退出 2重启 */
int daemonMode = D_DAEMONMODE; /* 是否后台运行 */
void customizeServiceName(char* service); /* myconfig.c中用于更改服务名的函数 */

/* 只在标准输出上打印 */
printf(_("\n欢迎使用MentoHUST\t版本: %s\n"
Expand Down Expand Up @@ -297,6 +301,9 @@ void initConfig(int argc, char **argv)
}
if (dhcpScript[0] == '\0') /* 未填写DHCP脚本? */
strcpy(dhcpScript, D_DHCPSCRIPT);
if (serviceName[0] == '\0') /* 未填写服务名? */
strcpy(serviceName, D_SERVICENAME);
customizeServiceName(serviceName);
newBuffer();
printConfig();
if (fillHeader()==-1 || openPcap()==-1) { /* 获取IP、MAC,打开网卡 */
Expand Down Expand Up @@ -336,6 +343,7 @@ static int readConfigFile(int *daemonMode)
getString(buf, "MentoHUST", "Datafile", "", dataFile, sizeof(dataFile));
getString(buf, "MentoHUST", "DhcpScript", "", dhcpScript, sizeof(dhcpScript));
getString(buf, "MentoHUST", "Version", "", tmp, sizeof(tmp));
getString(buf, "MentoHUST", "ServiceName", D_SERVICENAME, serviceName, sizeof(serviceName));
if (strlen(tmp) >= 3) {
unsigned ver[2];
if (sscanf(tmp, "%u.%u", ver, ver+1)!=EOF && ver[0]!=0) {
Expand Down Expand Up @@ -411,6 +419,7 @@ static void readArg(char argc, char **argv, int *saveFlag, int *exitFlag, int *d
{ "proxy-require-success", required_argument, NULL, 'j' },
{ "decode-config", required_argument, NULL, 'q' },
{ "max-retries", required_argument, NULL, 0},
{ "service", required_argument, NULL, 0},
{ NULL, no_argument, NULL, 0 }
};

Expand Down Expand Up @@ -513,6 +522,8 @@ static void readArg(char argc, char **argv, int *saveFlag, int *exitFlag, int *d
#define IF_ARG(arg_name) (strcmp(longOpts[longIndex].name, arg_name) == 0)
if (IF_ARG("max-retries")) {
maxRetries = atoi(optarg);
} else if (IF_ARG("service")) {
COPY_ARG_TO(serviceName);
}
break;
default:
Expand Down Expand Up @@ -724,6 +735,7 @@ static void showHelp(const char *fileName)
#ifndef NO_GETOPT_LONG
/* 从这里开始就是必须使用长选项的参数了 */
"\t--max-retries 在得到认证成功或失败的结果前,最多重试的次数,0表示无限重试 [默认0]\n"
"\t--service 要登陆到的服务名 [默认internet]\n"
#endif
"例如:\t%s -u username -p password -n eth0 -i 192.168.0.1 -m 255.255.255.0 -g 0.0.0.0 -s 0.0.0.0 -o 0.0.0.0 -t 8 -e 30 -r 15 -a 0 -d 1 -b 0 -v 4.10 -f default.mpf -c dhclient\n"
"关于代理模式:此模式下MentoHUST将不会自己发起认证,而是修改LAN内捕获到的认证数据包的源MAC并转发至WAN,使得本机认证通过。\n"
Expand Down Expand Up @@ -785,6 +797,7 @@ static void printConfig()
print_log(_("** 用户名:\t%s\n"), userName);
/* printf("** 密码:\t%s\n", password); */
print_log(_("** 网卡: \t%s\n"), nic);
print_log(_("** 服务名:\t%s\n"), serviceName);
print_log(_("** 掉线后重连:\t%s\n"), restartOnLogOff ? "是" : "否");
} else {
print_log(_("** 已启用代理模式\n"));
Expand Down
61 changes: 46 additions & 15 deletions src/mystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ static const unsigned char pkt3[519] = {
0x00 /* . */
};

static unsigned char* pkt_start;
static unsigned char* pkt_identity;
static unsigned char* pkt_md5;

static void setTimer(unsigned interval); /* 设置定时器 */
static int renewIP(); /* 更新IP */
static void fillEtherAddr(u_int32_t protocol); /* 填充MAC地址和协议 */
Expand All @@ -282,6 +286,33 @@ static void setTimer(unsigned interval) /* 设置定时器 */
setitimer(ITIMER_REAL, &timer, NULL);
}

void customizeServiceName(char* service)
{
if (strncmp(service, "internet", 8) != 0) {
int serviceNameLen = strnlen(service, 128);

pkt_start = (unsigned char*)malloc(sizeof(pkt1));
pkt_identity = (unsigned char*)malloc(sizeof(pkt2));
pkt_md5 = (unsigned char*)malloc(sizeof(pkt3));

memmove(pkt_start, pkt1, sizeof(pkt1));
memmove(pkt_identity, pkt2, sizeof(pkt2));
memmove(pkt_md5, pkt3, sizeof(pkt3));

memset(pkt_start + 360, 0, 8);
memset(pkt_identity + 343, 0, 8);
memset(pkt_md5 + 360, 0, 8);

memmove(pkt_start + 360, service, serviceNameLen);
memmove(pkt_identity + 343, service, serviceNameLen);
memmove(pkt_md5 + 360, service, serviceNameLen);
} else {
pkt_start = pkt1;
pkt_identity = pkt2;
pkt_md5 = pkt3;
}
}

int switchState(int type)
{
if (state == type) /* 跟上次是同一状态? */
Expand Down Expand Up @@ -309,8 +340,8 @@ int switchState(int type)
return switchState(ID_ECHO);
}
if (maxRetries > 0 && ++continousRestartCount >= maxRetries) {
print_log(_("!! 已经重启%d次,达到预设上限,将退出认证!\n"), continousRestartCount);
exit(EXIT_FAILURE);
print_log(_("!! 已经重启%d次,达到预设上限,将退出认证!\n"), continousRestartCount);
exit(EXIT_FAILURE);
}
return restart();
}
Expand Down Expand Up @@ -369,10 +400,10 @@ static int renewIP()
{
setTimer(0); /* 取消定时器 */
print_log(_(">> 正在获取IP...\n"));
setreuid(0,0);
printf("%s\n", dhcpScript);
setreuid(0,0);
printf("%s\n", dhcpScript);
system(dhcpScript);
print_log(_(">> 操作结束。\n"));
print_log(_(">> 操作结束。\n"));
dhcpMode += 3; /* 标记为已获取,123变为456,5不需再认证*/
if (fillHeader() == -1)
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -414,8 +445,8 @@ static int sendStartPacket()
print_log(_(">> 寻找服务器...\n"));
//fillStartPacket();
fillEtherAddr(0x888E0101);
memcpy(sendPacket + 0x12, pkt1, sizeof(pkt1));
memcpy(sendPacket + 0xe2, computeV4(pad, 16), 0x80);
memcpy(sendPacket + 0x12, pkt_start, sizeof(pkt1));
memcpy(sendPacket + 0xe2, computeV4(pad, 16), 0x80);
setTimer(timeout);
}
return pcap_sendpacket(hPcap, sendPacket, 521);
Expand Down Expand Up @@ -452,8 +483,8 @@ static int sendIdentityPacket()
sendPacket[0x13] = capBuf[0x13];
sendPacket[0x16] = 0x01;
memcpy(sendPacket+0x17, userName, nameLen);
memcpy(sendPacket+0x17+nameLen, pkt2, sizeof(pkt2));
memcpy(sendPacket + 0xe7 + nameLen, computeV4(pad, 16), 0x80);
memcpy(sendPacket+0x17+nameLen, pkt_identity, sizeof(pkt2));
memcpy(sendPacket + 0xe7 + nameLen, computeV4(pad, 16), 0x80);
setTimer(timeout);
}
return pcap_sendpacket(hPcap, sendPacket, 536);
Expand Down Expand Up @@ -492,11 +523,11 @@ static int sendChallengePacket()
memcpy(sendPacket+0x18, checkPass(capBuf[0x13], capBuf+0x18, capBuf[0x17]), 16);
memcpy(sendPacket+0x28, userName, nameLen);

memcpy(sendPacket+0x28+nameLen, pkt3, sizeof(pkt3));
memcpy(sendPacket + 0x90 + nameLen, computePwd(capBuf+0x18), 0x10);
//memcpy(sendPacket + 0xa0 +nameLen, fillBuf + 0x68, fillSize-0x68);
memcpy(sendPacket + 0x108 + nameLen, computeV4(capBuf+0x18, capBuf[0x17]), 0x80);
//sendPacket[0x77] = 0xc7;
memcpy(sendPacket+0x28+nameLen, pkt_md5, sizeof(pkt3));
memcpy(sendPacket + 0x90 + nameLen, computePwd(capBuf+0x18), 0x10);
//memcpy(sendPacket + 0xa0 +nameLen, fillBuf + 0x68, fillSize-0x68);
memcpy(sendPacket + 0x108 + nameLen, computeV4(capBuf+0x18, capBuf[0x17]), 0x80);
//sendPacket[0x77] = 0xc7;
setTimer(timeout);
}
return pcap_sendpacket(hPcap, sendPacket, 569);
Expand Down Expand Up @@ -544,7 +575,7 @@ static int sendLogoffPacket()
memcpy(sendPacket+0x12, fillBuf, fillSize);
return pcap_sendpacket(hPcap, sendPacket, 0x3E8);
#else
return 0;
return 0;
#endif
}

Expand Down

0 comments on commit 4b63016

Please sign in to comment.