1 /*****************************************************************************
2 * Copyright 2005 Daniel Ferullo *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
16 *****************************************************************************/
20 * @author Daniel Ferullo (ferullo@cmu.edu)
22 * @brief functions to spoof/forge network packets
26 #include "spoof_private.h"
31 errorcode
spoof(tcp_packet_info_t
*tcp_hdr
, char *device
, void *payload
,
32 unsigned long payload_len
, short ttl
){
34 /* declare local variables */
38 char errbuf
[LIBNET_ERRBUF_SIZE
];
39 unsigned char tcp_flags
;
41 /* error check arguments */
42 CHECK_NOT_NULL(tcp_hdr
,ERROR_NULL_ARG_1
);
43 CHECK_NOT_NULL(device
,ERROR_NULL_ARG_2
);
44 if ( (payload
==NULL
) && (payload_len
!=0))
49 /* set the tcp_flags */
51 tcp_flags
|= ( (tcp_hdr
->syn_flag
==FLAG_SET
) ? TH_SYN
: 0);
52 tcp_flags
|= ( (tcp_hdr
->ack_flag
==FLAG_SET
) ? TH_ACK
: 0);
54 /* Initialize the library. Root priviledges are required. */
56 LIBNET_RAW4
, /* injection type */
57 device
, /* network interface */
61 DEBUG(DBG_SPOOF
,"SPOOF:libnet_init() failed\n");
65 /* make sure to put ports, seq_num, ack_num and window in host byte
66 * order because libnet doesn't want them in network byte order. */
68 PORT_2HBO(tcp_hdr
->s_port
), /* source port */
69 PORT_2HBO(tcp_hdr
->d_port
), /* destination port */
70 SEQ_NUM_2HBO(tcp_hdr
->seq_num
), /* sequence number */
71 SEQ_NUM_2HBO(tcp_hdr
->ack_num
), /* acknowledgement number */
72 tcp_flags
, /* control flags */
73 WINDOW_2HBO(tcp_hdr
->window
), /* window size */
75 0, /* urgent pointer */
76 LIBNET_TCP_H
+ payload_len
, /* TCP packet size */
77 payload
, /* payload */
78 payload_len
, /* payload size */
79 lib
, /* libnet handle */
84 DEBUG(DBG_SPOOF
,"SPOOF:can't build TCP header\n");
89 t
= libnet_build_ipv4(
90 LIBNET_IPV4_H
+LIBNET_TCP_H
+payload_len
, /* length */
95 IPPROTO_TCP
, /* protocol */
97 tcp_hdr
->s_addr
, /* source IP */
98 tcp_hdr
->d_addr
, /* destination IP */
100 0, /* payload size */
101 lib
, /* libnet handle */
106 DEBUG(DBG_SPOOF
,"SPOOF:can't build IP header\n");
111 /* Write it to the wire. */
112 c
= libnet_write(lib
);
115 DEBUG(DBG_SPOOF
,"SPOOF:write error\n");