项目作者: revsic

项目描述 :
Detect HTTP packet and inject redirect packet
高级语言: C++
项目地址: git://github.com/revsic/PacketInjector.git
创建时间: 2016-08-17T12:57:22Z
项目社区:https://github.com/revsic/PacketInjector

开源协议:MIT License

下载


Packet-Injection

Detect specific packet and inject forward or backward

  1. Detect specific packet : ex) HTTP GET

    1. if (tcp.phdr->tcp_dport == TCP_PORT_HTTP) {
    2. if (tcp.pdat.length && !strncmp((char *)tcp.pdat.data, "GET", 3)) {
    3. char *tmp = strchr((char *)tcp.pdat.data, '\n');
    4. if (tmp) *tmp = '\0';
    5. std::cout << "[*] blocked : " << tcp.pdat.data << std::endl;
    6. injectForward(ip, tcp);
    7. injectBackward(eth, ip, tcp);
    8. }
    9. }
  2. inject forward or backward : ex) HTTP 302 redirect, fin or rst flag packet

    1. int PacketInjector::injectForward(IPv4& ip, TCP& tcp) {
    2. u_int32_t seqtmp = tcp.phdr->tcp_seq_num;
    3. tcp.phdr->tcp_seq_num = htonl(ntohl(seqtmp) + tcp.pdat.length);
    4. int ip_len = setProperty(ip, tcp, TCP_FLAG_RST, "");
    5. int total_len = ETHER_HEAD_LEN + ip_len;
    6. int result = pcap_sendpacket(handle, packet, total_len);
    7. tcp.phdr->tcp_seq_num = seqtmp;
    8. tcp.phdr->tcp_flags ^= TCP_FLAG_RST;
    9. return result;
    10. }
  1. int PacketInjector::injectBackward(Ethernet& eth, IPv4& ip, TCP& tcp) {
  2. for (int i = 0; i < ETHER_ADDR_LEN; ++i) {
  3. u_int8_t etmp = eth.phdr->ether_dhost[i];
  4. eth.phdr->ether_dhost[i] = eth.phdr->ether_shost[i];
  5. eth.phdr->ether_shost[i] = etmp;
  6. }
  7. struct in_addr itmp = ip.phdr->ip_dst;
  8. ip.phdr->ip_dst = ip.phdr->ip_src;
  9. ip.phdr->ip_src = itmp;
  10. u_int16_t ptmp = tcp.phdr->tcp_dport;
  11. tcp.phdr->tcp_dport = tcp.phdr->tcp_sport;
  12. tcp.phdr->tcp_sport = ptmp;
  13. u_int32_t atmp = tcp.phdr->tcp_ack_num;
  14. tcp.phdr->tcp_ack_num = htonl(ntohl(tcp.phdr->tcp_seq_num) + tcp.pdat.length);
  15. tcp.phdr->tcp_seq_num = atmp;
  16. int ip_len = setProperty(ip, tcp, TCP_FLAG_FIN, BLOCK_MSG);
  17. int total_len = ETHER_HEAD_LEN + ip_len;
  18. int result = pcap_sendpacket(handle, packet, total_len);
  19. return result;
  20. }