00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "spoof.h"
00026 #include "spoof_private.h"
00027 #include <libnet.h>
00028 #include "debug.h"
00029 #include "peerdef.h"
00030
00031 errorcode spoof(tcp_packet_info_t *tcp_hdr, char *device, void *payload,
00032 unsigned long payload_len, short ttl){
00033
00034
00035 int c;
00036 libnet_t *lib;
00037 libnet_ptag_t t;
00038 char errbuf[LIBNET_ERRBUF_SIZE];
00039 unsigned char tcp_flags;
00040
00041
00042 CHECK_NOT_NULL(tcp_hdr,ERROR_NULL_ARG_1);
00043 CHECK_NOT_NULL(device,ERROR_NULL_ARG_2);
00044 if ( (payload==NULL) && (payload_len!=0))
00045 return ERROR_ARG_3;
00046
00047
00048
00049
00050 tcp_flags = 0;
00051 tcp_flags |= ( (tcp_hdr->syn_flag==FLAG_SET) ? TH_SYN : 0);
00052 tcp_flags |= ( (tcp_hdr->ack_flag==FLAG_SET) ? TH_ACK : 0);
00053
00054
00055 lib = libnet_init(
00056 LIBNET_RAW4,
00057 device,
00058 errbuf);
00059
00060 if (lib == NULL) {
00061 DEBUG(DBG_SPOOF,"SPOOF:libnet_init() failed\n");
00062 return ERROR_1;
00063 }
00064
00065
00066
00067 t = libnet_build_tcp(
00068 PORT_2HBO(tcp_hdr->s_port),
00069 PORT_2HBO(tcp_hdr->d_port),
00070 SEQ_NUM_2HBO(tcp_hdr->seq_num),
00071 SEQ_NUM_2HBO(tcp_hdr->ack_num),
00072 tcp_flags,
00073 WINDOW_2HBO(tcp_hdr->window),
00074 0,
00075 0,
00076 LIBNET_TCP_H + payload_len,
00077 payload,
00078 payload_len,
00079 lib,
00080 0
00081 );
00082
00083 if (t == -1) {
00084 DEBUG(DBG_SPOOF,"SPOOF:can't build TCP header\n");
00085 libnet_destroy(lib);
00086 return ERROR_2;
00087 }
00088
00089 t = libnet_build_ipv4(
00090 LIBNET_IPV4_H+LIBNET_TCP_H+payload_len,
00091 0,
00092 242,
00093 0,
00094 ttl,
00095 IPPROTO_TCP,
00096 0,
00097 tcp_hdr->s_addr,
00098 tcp_hdr->d_addr,
00099 NULL,
00100 0,
00101 lib,
00102 0
00103 );
00104
00105 if (t == -1) {
00106 DEBUG(DBG_SPOOF,"SPOOF:can't build IP header\n");
00107 libnet_destroy(lib);
00108 return ERROR_3;
00109 }
00110
00111
00112 c = libnet_write(lib);
00113
00114 if (c == -1) {
00115 DEBUG(DBG_SPOOF,"SPOOF:write error\n");
00116 libnet_destroy(lib);
00117 return ERROR_4;
00118 }
00119
00120 libnet_destroy(lib);
00121 return SUCCESS;
00122 }
00123