1 /*
2 * $Id: rpc_udp.c,v 1.2 2004/01/03 20:31:01 mike Exp $
3 *
4 * libnet 1.1
5 * Build an RPC UDP packet
6 *
7 * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 #if (HAVE_CONFIG_H)
34 #include "../include/config.h"
35 #endif
36 #include "./libnet_test.h"
37
38
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42 int c;
43 char *cp;
44 libnet_t *l;
45 libnet_ptag_t t;
46 libnet_ptag_t ip;
47 libnet_ptag_t udp;
48 char *payload;
49 u_short payload_s;
50 u_long src_ip, dst_ip;
51 u_short src_prt, dst_prt;
52 char errbuf[LIBNET_ERRBUF_SIZE];
53
54 printf("libnet 1.1 packet shaping: RPC + UDP + IP options[raw]\n");
55
56 /*
57 * Initialize the library. Root priviledges are required.
58 */
59 l = libnet_init(
60 LIBNET_RAW4, /* injection type */
61 NULL, /* network interface */
62 errbuf); /* errbuf */
63
64 if (l == NULL)
65 {
66 fprintf(stderr, "libnet_init() failed: %s\n", errbuf);
67 exit(EXIT_FAILURE);
68 }
69
70 src_ip = 0;
71 dst_ip = 0;
72 src_prt = 0;
73 dst_prt = 0;
74 payload = NULL;
75 payload_s = 0;
76 udp = 0;
77 while ((c = getopt(argc, argv, "d:s:p:")) != EOF)
78 {
79 switch (c)
80 {
81 /*
82 * We expect the input to be of the form `ip.ip.ip.ip.port`. We
83 * point cp to the last dot of the IP address/port string and
84 * then seperate them with a NULL byte. The optarg now points to
85 * just the IP address, and cp points to the port.
86 */
87 case 'd':
88 if (!(cp = strrchr(optarg, '.')))
89 {
90 usage(argv[0]);
91 }
92 *cp++ = 0;
93 dst_prt = (u_short)atoi(cp);
94 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
95 {
96 fprintf(stderr, "Bad destination IP address: %s\n", optarg); exit(EXIT_FAILURE);
97 }
98 break;
99 case 's':
100 if (!(cp = strrchr(optarg, '.')))
101 {
102 usage(argv[0]);
103 }
104 *cp++ = 0;
105 src_prt = (u_short)atoi(cp);
106 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
107 {
108 fprintf(stderr, "Bad source IP address: %s\n", optarg);
109 exit(EXIT_FAILURE);
110 }
111 break;
112 case 'p':
113 payload = optarg;
114 payload_s = strlen(payload);
115 break;
116 default:
117 exit(EXIT_FAILURE);
118 }
119 }
120
121 if (!src_ip || !src_prt || !dst_ip || !dst_prt)
122 {
123 usage(argv[0]);
124 exit(EXIT_FAILURE);
125 }
126
127 t = libnet_build_rpc_call(
128 0,
129 0x10e70082,
130 0x000186a0,
131 2,
132 4,
133 0,
134 0,
135 NULL,
136 0,
137 0,
138 NULL,
139 payload,
140 payload_s,
141 l,
142 0);
143 if (t == -1)
144 {
145 fprintf(stderr, "Can't build RPC header: %s\n", libnet_geterror(l));
146 goto bad;
147 }
148
149 udp = libnet_build_udp(
150 src_prt, /* source port */
151 dst_prt, /* destination port */
152 LIBNET_UDP_H + 40 +payload_s, /* packet length */
153 0, /* checksum */
154 NULL, /* payload */
155 0, /* payload size */
156 l, /* libnet handle */
157 udp); /* libnet id */
158 if (udp == -1)
159 {
160 fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
161 goto bad;
162 }
163
164 ip = libnet_build_ipv4(
165 LIBNET_IPV4_H + 40 + payload_s + LIBNET_UDP_H, /* length */
166 0, /* TOS */
167 242, /* IP ID */
168 0, /* IP Frag */
169 64, /* TTL */
170 IPPROTO_UDP, /* protocol */
171 0, /* checksum */
172 src_ip,
173 dst_ip,
174 NULL, /* payload */
175 0, /* payload size */
176 l, /* libnet handle */
177 0); /* libnet id */
178 if (ip == -1)
179 {
180 fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
181 goto bad;
182 }
183
184 /*
185 * Write it to the wire.
186 */
187 c = libnet_write(l);
188 if (c == -1)
189 {
190 fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
191 goto bad;
192 }
193 else
194 {
195 fprintf(stderr, "Wrote %d byte RPC UDP packet; check the wire.\n", c);
196 }
197
198 libnet_destroy(l);
199 return (EXIT_SUCCESS);
200 bad:
201 libnet_destroy(l);
202 return (EXIT_FAILURE);
203 }
204
205 void
usage(char * name)206 usage(char *name)
207 {
208 fprintf(stderr,
209 "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
210 " [-p payload]\n",
211 name);
212 }
213 /* EOF */
214