From 327f3129256c29cafdd2b7216e6dac0bdfa39a3f Mon Sep 17 00:00:00 2001 From: Zxilly Date: Sun, 12 Sep 2021 09:07:36 +0800 Subject: [PATCH] style: add log --- src/ua2f.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/ua2f.c b/src/ua2f.c index 6851a8db..c8fe25e3 100644 --- a/src/ua2f.c +++ b/src/ua2f.c @@ -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; @@ -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; } @@ -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; } @@ -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);