Skip to content

Commit bdc3c17

Browse files
committed
Create repository Packet-Injector
0 parents  commit bdc3c17

12 files changed

+336
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
http_inject

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CC = g++
2+
TARGET = http_inject
3+
OBJECTS = Protocol/Tcp.o Protocol/IPv4.o Protocol/Ethernet.o PacketInjector.o main.o
4+
5+
all : $(TARGET)
6+
7+
$(TARGET): $(OBJECTS)
8+
$(CC) -o $@ $^ -lpcap
9+
10+
clean:
11+
rm -rf *.o Protocol/*.o http_inejct

PacketInjector.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "PacketInjector.h"
2+
3+
PacketInjector::PacketInjector(pcap_t* p_handle) : handle(p_handle)
4+
{}
5+
6+
int PacketInjector::run(const unsigned char* p_packet) {
7+
packet = p_packet;
8+
Ethernet eth = Ethernet(packet);
9+
10+
if (eth.phdr->ether_type == ETHER_TYPE_IPv4) {
11+
IPv4 ip = IPv4(packet + ETHER_HEAD_LEN);
12+
13+
if (ip.phdr->ip_protocol == IP_PRTCL_TCP) {
14+
int ip_len = ntohs(ip.phdr->ip_len);
15+
int ip_hdrlen = ip.phdr->ip_header_len << 2;
16+
17+
TCP tcp = TCP(packet + ETHER_HEAD_LEN + ip_hdrlen, ip_len - ip_hdrlen);
18+
if (tcp.phdr->tcp_dport == TCP_PORT_HTTP) {
19+
if (tcp.pdat.length && !strncmp((char *)tcp.pdat.data, "GET", 3)) {
20+
char *tmp = strchr((char *)tcp.pdat.data, '\n');
21+
if (tmp) *tmp = '\0';
22+
std::cout << "[*] blocked : " << tcp.pdat.data << std::endl;
23+
24+
int result = sendBackwardFin(eth, ip, tcp);
25+
if (result == -1) {
26+
std::cout << "[*] err : " << pcap_geterr(handle) << std::endl;
27+
return -1;
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
int PacketInjector::sendForwardFin(IPv4& ip, TCP& tcp) {
36+
int tcp_hdrlen = tcp.phdr->tcp_header_len << 2;
37+
tcp.phdr->tcp_flags |= 1;
38+
tcp.phdr->tcp_seq_num = htonl(ntohl(tcp.phdr->tcp_seq_num) + tcp.pdat.length);
39+
40+
int ip_hdrlen = ip.phdr->ip_header_len << 2;
41+
int ip_len = ip_hdrlen + tcp_hdrlen + strlen(BLOCK_MSG);
42+
ip.phdr->ip_len = htons(ip_len);
43+
strcpy((char *)tcp.pdat.data, BLOCK_MSG);
44+
tcp.pdat.length = strlen(BLOCK_MSG);
45+
46+
ip.makeChecksum();
47+
tcp.makeChecksum(ip);
48+
49+
int total_len = ETHER_HEAD_LEN + ip_len;
50+
int result = pcap_sendpacket(handle, packet, total_len);
51+
52+
return result;
53+
}
54+
55+
int PacketInjector::sendBackwardFin(Ethernet& eth, IPv4& ip, TCP& tcp) {
56+
for (int i = 0; i < ETHER_ADDR_LEN; ++i) {
57+
u_int8_t etmp = eth.phdr->ether_dhost[i];
58+
eth.phdr->ether_dhost[i] = eth.phdr->ether_shost[i];
59+
eth.phdr->ether_shost[i] = etmp;
60+
}
61+
62+
struct in_addr itmp = ip.phdr->ip_dst;
63+
ip.phdr->ip_dst = ip.phdr->ip_src;
64+
ip.phdr->ip_src = itmp;
65+
66+
u_int16_t ptmp = tcp.phdr->tcp_dport;
67+
tcp.phdr->tcp_dport = tcp.phdr->tcp_sport;
68+
tcp.phdr->tcp_sport = ptmp;
69+
70+
tcp.phdr->tcp_flags |= TCP_FLAG_FIN;
71+
u_int32_t atmp = tcp.phdr->tcp_ack_num;
72+
tcp.phdr->tcp_ack_num = htonl(ntohl(tcp.phdr->tcp_seq_num) + tcp.pdat.length);
73+
tcp.phdr->tcp_seq_num = atmp;
74+
75+
int tcp_hdrlen = tcp.phdr->tcp_header_len << 2;
76+
int ip_hdrlen = ip.phdr->ip_header_len << 2;
77+
int ip_len = ip_hdrlen + tcp_hdrlen + strlen(BLOCK_MSG);
78+
ip.phdr->ip_len = htons(ip_len);
79+
80+
strcpy((char *)tcp.pdat.data, BLOCK_MSG);
81+
tcp.pdat.length = strlen(BLOCK_MSG);
82+
83+
ip.makeChecksum();
84+
tcp.makeChecksum(ip);
85+
86+
int total_len = ETHER_HEAD_LEN + ip_len;
87+
int result = pcap_sendpacket(handle, packet, total_len);
88+
89+
return result;
90+
}

PacketInjector.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef PACKET_INJECTOR_H
2+
#define PACKET_INJECTOR_H
3+
4+
#include <iostream>
5+
#include <string.h>
6+
#include <pcap.h>
7+
#include "Protocol/Ethernet.h"
8+
#include "Protocol/IPv4.h"
9+
#include "Protocol/Tcp.h"
10+
11+
#define BLOCK_MSG "HTTP/1.1 302 Found\nLocation: https://en.wikipedia.org/wiki/HTTP_302\n"
12+
#define TCP_FLAG_FIN 1
13+
14+
class PacketInjector {
15+
private:
16+
pcap_t* handle;
17+
const unsigned char* packet;
18+
19+
public:
20+
PacketInjector(pcap_t* p_handle);
21+
22+
int run(const unsigned char* p_packet);
23+
int sendForwardFin(IPv4& ip, TCP& tcp);
24+
int sendBackwardFin(Ethernet& eth, IPv4& ip, TCP& tcp);
25+
};
26+
27+
#endif

Protocol/Ethernet.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "Ethernet.h"
2+
3+
Ethernet::Ethernet(const u_char* packet) {
4+
phdr = (Ethernet_Header *)packet;
5+
}

Protocol/Ethernet.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef ETHERNET_H
2+
#define ETHERNET_H
3+
4+
#include <arpa/inet.h>
5+
6+
#define ETHER_ADDR_LEN 6
7+
#define ETHER_HEAD_LEN 14
8+
#define ETHER_TYPE_IPv4 htons(0x0800)
9+
10+
class Ethernet {
11+
private:
12+
struct Ethernet_Header {
13+
u_int8_t ether_dhost[ETHER_ADDR_LEN];
14+
u_int8_t ether_shost[ETHER_ADDR_LEN];
15+
u_int16_t ether_type;
16+
};
17+
18+
public:
19+
Ethernet_Header* phdr;
20+
Ethernet(const u_char* packet);
21+
};
22+
23+
#endif

Protocol/IPv4.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "IPv4.h"
2+
3+
IPv4::IPv4(const u_char* packet) {
4+
phdr = (IPv4_Header *)packet;
5+
}
6+
7+
void IPv4::makeChecksum() {
8+
int chksum = 0;
9+
unsigned short *shorter = (unsigned short *)phdr;
10+
phdr->ip_checksum = 0;
11+
12+
int len = phdr->ip_header_len << 1;
13+
14+
for (int i = 0; i < len; ++i) {
15+
chksum += shorter[i];
16+
}
17+
18+
chksum = (chksum >> 16) + (chksum & 0xFFFF);
19+
chksum += (chksum >> 16);
20+
21+
chksum ^= 0xFFFF;
22+
phdr->ip_checksum = chksum;
23+
}

Protocol/IPv4.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef IPV4_H
2+
#define IPV4_H
3+
4+
#include <sys/types.h>
5+
#include <arpa/inet.h>
6+
#define IP_PRTCL_TCP 6
7+
8+
class IPv4 {
9+
private:
10+
struct IPv4_Header {
11+
u_int8_t ip_header_len:4;
12+
u_int8_t ip_version:4;
13+
u_int8_t ip_type;
14+
u_int16_t ip_len;
15+
u_int16_t ip_id;
16+
u_int16_t ip_offset;
17+
u_int8_t ip_ttl;
18+
u_int8_t ip_protocol;
19+
u_int16_t ip_checksum;
20+
struct in_addr ip_src, ip_dst;
21+
};
22+
23+
public:
24+
IPv4_Header* phdr;
25+
IPv4(const u_char* packet);
26+
27+
void makeChecksum();
28+
};
29+
30+
#endif

Protocol/Tcp.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "Tcp.h"
2+
#include <stdio.h>
3+
4+
TCP::TCP(const u_char *packet, int seg_len) {
5+
phdr = (TCP_Header *)packet;
6+
7+
int tcp_hdrlen = phdr->tcp_header_len << 2;
8+
pdat.length = seg_len - tcp_hdrlen;
9+
pdat.data = packet + tcp_hdrlen;
10+
}
11+
12+
void TCP::makeChecksum(IPv4& ip) {
13+
int i, chksum = 0;
14+
int dlen = phdr->tcp_header_len << 1;
15+
unsigned short *shorter = (unsigned short *)phdr;
16+
17+
phdr->tcp_checksum = 0;
18+
for (i = 0; i < dlen; ++i) {
19+
chksum += shorter[i];
20+
}
21+
22+
dlen = pdat.length >> 1;
23+
shorter = (unsigned short *)pdat.data;
24+
25+
for (i = 0; i < dlen; ++i) {
26+
chksum += shorter[i];
27+
}
28+
29+
if (pdat.length & 1) {
30+
chksum += shorter[i] & 0x00ff;
31+
}
32+
33+
shorter = (unsigned short *)&ip.phdr->ip_src;
34+
chksum += shorter[0] + shorter[1];
35+
36+
shorter = (unsigned short *)&ip.phdr->ip_dst;
37+
chksum += shorter[0] + shorter[1];
38+
39+
chksum += htons(6);
40+
chksum += htons((phdr->tcp_header_len << 2) + pdat.length);
41+
42+
chksum = (chksum >> 16) + (chksum & 0xFFFF);
43+
chksum += (chksum >> 16);
44+
45+
chksum ^= 0xFFFF;
46+
phdr->tcp_checksum = chksum;
47+
48+
}

Protocol/Tcp.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef TCP_H
2+
#define TCP_H
3+
4+
#include <arpa/inet.h>
5+
#include <string.h>
6+
#include "IPv4.h"
7+
8+
#define TCP_PORT_HTTP htons(80)
9+
10+
class TCP {
11+
private:
12+
struct TCP_Header {
13+
u_int16_t tcp_sport, tcp_dport;
14+
u_int32_t tcp_seq_num;
15+
u_int32_t tcp_ack_num;
16+
u_int8_t tcp_reserved:4;
17+
u_int8_t tcp_header_len:4;
18+
u_int8_t tcp_flags;
19+
u_int16_t tcp_window;
20+
u_int16_t tcp_checksum;
21+
u_int16_t tcp_urgent;
22+
};
23+
24+
struct TCP_Data {
25+
u_int32_t length;
26+
const u_int8_t *data;
27+
};
28+
29+
public:
30+
TCP_Header *phdr;
31+
TCP_Data pdat;
32+
TCP(const u_char *packet, int seg_len);
33+
34+
void makeChecksum(IPv4& ip);
35+
};
36+
37+
#endif

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Packet-Injection
2+
3+
detect HTTP GET packet and inject packets

main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <pcap.h>
3+
#include "PacketInjector.h"
4+
5+
int main (int argc, char** argv) {
6+
char errbuf[PCAP_ERRBUF_SIZE];
7+
char *dev = pcap_lookupdev(errbuf);
8+
9+
if (dev == NULL) {
10+
std::cout << "[*] Couldn't find default device : " << errbuf << std::endl;
11+
return -1;
12+
}
13+
14+
pcap_t *handle = pcap_open_live(dev, BUFSIZ, 1, 0, errbuf);
15+
if (handle == NULL) {
16+
std::cout << "[*] Couldn't open device : " << errbuf << std::endl;
17+
return -1;
18+
}
19+
20+
struct pcap_pkthdr *header;
21+
const unsigned char *packet;
22+
PacketInjector pi = PacketInjector(handle);
23+
24+
while (1) {
25+
int ret = pcap_next_ex(handle, &header, &packet);
26+
27+
if (ret == 0) continue;
28+
else if (ret < 0) {
29+
std::cout << "[*] Couldn't receive packets." << std::endl;
30+
return -1;
31+
}
32+
33+
pi.run(packet);
34+
}
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)