Skip to content

Commit

Permalink
style: add log
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Sep 12, 2021
1 parent 6fe0b26 commit 327f312
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/ua2f.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,10 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) {
bool noUA = false;
char *ip;
uint16_t port = 0;

char addcmd[50];


debugflag = 0;


if (nfq_nlmsg_parse(nlh, attr) < 0) {
perror("problems parsing");
return MNL_CB_ERROR;
Expand Down Expand Up @@ -285,34 +282,27 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) {
tcppklen = nfq_tcp_get_payload_len(tcppkhdl, pktb); //获取 tcp长度

if (tcppkpayload) {
char *uapointer = memncasemem(tcppkpayload, tcppklen, "\r\nUser-Agent:", 13);
/* if (uapointer) {
uapointer = memmem(tcppkpayload, tcppklen, "\r\nUser-Agent:", 13);
if (!uapointer) {
uapointer = memmem(tcppkpayload, tcppklen, "\r\nUser-agent:", 13);
}
} else {
uapointer = memmem(tcppkpayload, tcppklen, "\r\nuser-agent:", 13);
}*/
char *uapointer = memncasemem(tcppkpayload, tcppklen, "\r\nUser-Agent: ", 14); // 找到指向 \r 的指针

if (uapointer) {
uaoffset = uapointer - tcppkpayload + 14;
uaoffset = uapointer - tcppkpayload + 14; // 应该指向 UA 的第一个字符

if (uaoffset >= tcppklen) {
syslog(LOG_WARNING, "Offset overflow");
syslog(LOG_WARNING, "User-Agent position overflow, may cause by TCP Segment Reassembled.");
pktb_free(pktb);
return MNL_CB_OK;
}

char *uaStartPointer = uapointer + 14;
for (int i = 0; i < tcppklen - uaoffset - 2; ++i) {
if (*(uapointer + 14 + i) == '\r') {
if (*(uaStartPointer + i) == '\r') {
ualength = i;
break;
}
}

if (ualength + uaoffset > tcppklen) {
syslog(LOG_WARNING, "UA overflow");
syslog(LOG_ERR, "UA overflow, this is an unexpected error."); // 不应该出现,出现说明指针越界了
pktb_free(pktb);
return MNL_CB_OK;
}
Expand All @@ -321,6 +311,7 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) {
UAcount++; //记录修改包的数量
noUA = false;
} else {
syslog(LOG_ERR, "Mangle packet failed.");
pktb_free(pktb);
return MNL_CB_ERROR;
}
Expand Down Expand Up @@ -443,7 +434,7 @@ int main(int argc, char *argv[]) {

str = malloc(sizeof_buf);
memset(str, 'F', sizeof_buf);
memcpy(str,"Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)",47);
memcpy(str, "Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)", 47);

nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_number);
nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_BIND);
Expand Down

0 comments on commit 327f312

Please sign in to comment.