GithubHelp home page GithubHelp logo

packet-injector's Introduction

Packet-Injection

detect certain packet and inject forward or backward

  1. detect certain packet : ex) HTTP GET
if (tcp.phdr->tcp_dport == TCP_PORT_HTTP) {
    if (tcp.pdat.length && !strncmp((char *)tcp.pdat.data, "GET", 3)) {
        char *tmp = strchr((char *)tcp.pdat.data, '\n');

        if (tmp) *tmp = '\0';
        std::cout << "[*] blocked : " << tcp.pdat.data << std::endl;

        injectForward(ip, tcp);
        injectBackward(eth, ip, tcp);
    }
}
  1. inject packet backward or forward : ex) HTTP 302 redirect, fin or rst flag packet
int PacketInjector::injectForward(IPv4& ip, TCP& tcp) {
    u_int32_t seqtmp = tcp.phdr->tcp_seq_num;
    tcp.phdr->tcp_seq_num = htonl(ntohl(seqtmp) + tcp.pdat.length);

    int ip_len = setProperty(ip, tcp, TCP_FLAG_RST, "");
    int total_len = ETHER_HEAD_LEN + ip_len;
    int result = pcap_sendpacket(handle, packet, total_len);

    tcp.phdr->tcp_seq_num = seqtmp;
    tcp.phdr->tcp_flags ^= TCP_FLAG_RST;

    return result;
}
int PacketInjector::injectBackward(Ethernet& eth, IPv4& ip, TCP& tcp) {
  for (int i = 0; i < ETHER_ADDR_LEN; ++i) {
    u_int8_t etmp = eth.phdr->ether_dhost[i];
    eth.phdr->ether_dhost[i] = eth.phdr->ether_shost[i];
    eth.phdr->ether_shost[i] = etmp;
  }

  struct in_addr itmp = ip.phdr->ip_dst;
  ip.phdr->ip_dst = ip.phdr->ip_src;
  ip.phdr->ip_src = itmp;

  u_int16_t ptmp = tcp.phdr->tcp_dport;
  tcp.phdr->tcp_dport = tcp.phdr->tcp_sport;
  tcp.phdr->tcp_sport = ptmp;

  u_int32_t atmp = tcp.phdr->tcp_ack_num;
  tcp.phdr->tcp_ack_num = htonl(ntohl(tcp.phdr->tcp_seq_num) + tcp.pdat.length);
  tcp.phdr->tcp_seq_num = atmp;

  int ip_len = setProperty(ip, tcp, TCP_FLAG_FIN, BLOCK_MSG);
  int total_len = ETHER_HEAD_LEN + ip_len;
  int result = pcap_sendpacket(handle, packet, total_len);

  return result;
}

packet-injector's People

Contributors

revsic avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.