summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/modules/mod_bind.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/b-scan/tmp/modules/mod_bind.c')
-rw-r--r--other/b-scan/tmp/modules/mod_bind.c302
1 files changed, 302 insertions, 0 deletions
diff --git a/other/b-scan/tmp/modules/mod_bind.c b/other/b-scan/tmp/modules/mod_bind.c
new file mode 100644
index 0000000..2a09f49
--- /dev/null
+++ b/other/b-scan/tmp/modules/mod_bind.c
@@ -0,0 +1,302 @@
1/*
2 * ping-module for bscan.
3 * IDEA: add record-route and source-route feature
4 * and -p pattern [where can we save our time-struct then ?
5 */
6
7#include <bscan/bscan.h>
8#include <bscan/module.h>
9#include <bscan/system.h>
10#include <stdio.h>
11
12
13#ifndef MOD_NAME
14#define MOD_NAME "mod_bind"
15#endif
16
17/*
18 * this is our query. This is a DNS-formated string
19 * <length1><string1><length2><string2><0>
20 */
21#define DNSTXTREQ "\007version\004bind"
22
23static int process_rcv(struct _opt *);
24static void add_dnshdr(unsigned char *);
25static void add_dnstxthdr(unsigned char *, char *, u_int *);
26
27static int isinit=0;
28/*
29 * some variables from the binary-process
30 */
31extern int dlt_len;
32extern u_char *align_buf;
33extern unsigned short ip_options;
34extern struct ip *ip;
35extern struct Ether_header *eth;
36extern u_int plen, pcaplen;
37extern struct timeval *pts;
38
39
40struct _dnshdr
41{
42 u_short id; /* DNS packet ID */
43 u_short flags; /* DNS flags */
44 u_short num_q; /* Number of questions */
45 u_short num_answ_rr; /* Number of answer resource records */
46 u_short num_auth_rr; /* Number of authority resource records */
47 u_short num_addi_rr; /* Number of additional resource records */
48};
49
50struct _dnsanswr
51{
52 u_short type;
53 u_short class;
54 u_short ttl1;
55 u_short ttl2;
56 u_short len;
57};
58
59
60
61/*
62 * static functions prototypes
63 */
64static int mdo_opt(int, char **, struct _opt *);
65static void init_vars(struct _opt *);
66
67/*
68 * print out usage informations
69 */
70void
71musage()
72{
73 printf ("\n"MOD_NAME"\n");
74 printf ("verson.bind chaos txt module\n");
75 printf (" -p <port>, destination port, default 53\n");
76 printf (" -o <port>, source port, default 53\n");
77}
78
79
80/*
81 * return 0 on success, != 0 on failure
82 */
83int
84init(char **modname, int argc, char *argv[], struct _opt *opt)
85{
86#ifdef DEBUG
87 printf("MODULE INIT\n");
88#endif
89 if (isinit)
90 return(-1);
91
92 *modname = MOD_NAME;
93 isinit = 1;
94 init_vars(opt);
95
96 if (mdo_opt(argc, argv, opt) != 0)
97 return(-1);
98
99 return(0);
100}
101
102/*
103 * fini-routine. called on cleanup
104 */
105int
106fini()
107{
108#ifdef DEBUG
109 printf("MODULE FINI\n");
110#endif
111 return(0);
112}
113
114
115/*
116 * Module entry point [entry]
117 * RMOD_OK: everything allright. send the packet out [if first]
118 * or do nothing [MOD_RCV].
119 * RMOD_SKIP: proceed with next IP without sending out the packet.
120 */
121int
122callmdl(int entry, struct _opt *opt)
123{
124#ifdef DEBUG
125 printf("MODULE CALLMDL\n");
126#endif
127 if (entry == MOD_FIRSTPKG)
128 {
129 add_dnstxthdr (opt->packet + ETH_SIZE + IP_SIZE + UDP_SIZE + sizeof(struct _dnshdr), DNSTXTREQ, &opt->pkg_len);
130 add_dnshdr (opt->packet + ETH_SIZE + IP_SIZE + UDP_SIZE);
131 add_udphdr (opt->packet + ETH_SIZE + IP_SIZE, &opt->nt, opt->pkg_len + sizeof(struct _dnshdr));
132 add_iphdr (opt->packet + ETH_SIZE, IPPROTO_UDP, &opt->nt, opt->pkg_len + UDP_SIZE + sizeof(struct _dnshdr));
133 opt->pkg_len += IP_SIZE + UDP_SIZE + sizeof(struct _dnshdr);
134 return(RMOD_OK);
135 }
136
137 if (entry == MOD_RCV)
138 process_rcv(opt);
139
140 return(RMOD_OK);
141}
142
143
144/*
145 ***********************************************************
146 * Our OWN/static functions for THIS module *
147 ***********************************************************
148 */
149
150/*
151 * initialize all local variables.
152 * We use some 'unused' variables of the masterprogramm
153 */
154static void
155init_vars(struct _opt *opt)
156{
157 opt->nt.sport = htons(53);
158 opt->nt.dport = htons(53);
159}
160
161
162/*
163 * LOCAL/STATIC function, only available in the module
164 * return 0 on success, != 0 on failure
165 */
166static int
167mdo_opt(int argc, char *argv[], struct _opt *opt)
168{
169 extern char *optarg;
170 /*extern int optind, opterr, optopt;*/
171 int c;
172
173 while ((c = getopt (argc, argv, "p:o:")) != -1)
174 {
175 switch (c)
176 {
177 case 'p':
178 opt->nt.dport = htons(atoi(optarg));
179 break;
180 case 'o':
181 opt->nt.sport = htons(atoi(optarg));
182 break;
183 case ':':
184 fprintf(stderr, "missing parameter\n");
185 return(-1);
186 default:
187 return(-1);
188 }
189 }
190 return(0);
191}
192
193
194/*
195 * add a DNS header
196 */
197static void
198add_dnshdr(unsigned char *pkt)
199{
200 struct _dnshdr *dnshdr = (struct _dnshdr *)pkt;
201
202 dnshdr->id = htons(6); /* could be random */
203 dnshdr->flags = htons(0x0100); /* do query recursivly */
204 dnshdr->num_q = htons(1);
205 dnshdr->num_answ_rr = 0;
206 dnshdr->num_auth_rr = 0;
207 dnshdr->num_addi_rr = 0;
208/* add request here. class TXT etc */
209}
210
211/*
212 * add DNS-TXT header here
213 * returns length in *len
214 */
215static void
216add_dnstxthdr(unsigned char *pkt, char *name, u_int *len)
217{
218 u_short *type;
219 u_short *class;
220
221 if (name == NULL)
222 return; /* nah! specifiy "". we need \0 termination */
223
224 memcpy(pkt, name, strlen(name)+1);
225 type = (u_short *)(pkt + strlen(name) + 1);
226 class = (u_short *)(pkt + strlen(name) + 1 + sizeof(*class));
227
228 *type = htons(0x10); /* TEXT string */
229 *class = htons(0x03); /* chaos */
230 *len = strlen(name) + 1 + sizeof(*type) + sizeof(*class);
231}
232
233
234/*
235 * handle incoming DNS udp answers
236 */
237static int
238process_rcv(struct _opt *opt)
239{
240 struct _dnshdr *dns;
241 struct _dnsanswr *dnsanswr;
242 struct udphdr *udp;
243 char *ptr;
244 char buf[128];
245 int len, dnstxtlen;
246 uint iphdr_len = 0;
247
248 if (ip->ip_p != IPPROTO_UDP)
249 return(0);
250
251 iphdr_len = IP_SIZE + ip_options;
252 if (plen < dlt_len + iphdr_len + sizeof(*udp) + sizeof(*dns))
253 return(-1); /* invalid size */
254
255 dns = (struct _dnshdr *) (align_buf + iphdr_len + sizeof(*udp));
256 if (ntohs(dns->flags) & 0x000F) /* dns-error? query refused ? */
257 return(-1);
258
259 ptr = (char *) (align_buf + iphdr_len + sizeof(*udp) + sizeof(*dns));
260 len = dlt_len + iphdr_len + sizeof(*udp) + sizeof(*dns);
261
262 while (len++ < plen)
263 if (*ptr++ == '\0')
264 break;
265
266 if (len >= plen)
267 return(-1);
268
269 len += 4;
270 ptr += 4;
271
272 while (len++ < plen) /* skip VERSION.BIND answer string */
273 if (*ptr++ == '\0')
274 break;
275
276 len += sizeof(*dnsanswr);
277 if (len >= plen)
278 return(-1);
279
280 dnsanswr = (struct _dnsanswr *) (ptr);
281 dnstxtlen = ntohs(dnsanswr->len);
282 if (len + dnstxtlen > plen)
283 return(0);
284
285 if ((dnstxtlen == 0) || (dnstxtlen > 128))
286 return(-1);
287
288 memcpy(buf, ptr + sizeof(*dnsanswr) +1, dnstxtlen - 1);
289 buf[dnstxtlen - 1] = '\0';
290
291 ptr = buf; /* evil hax0rs sending messed up strings ? */
292 while (*++ptr != '\0')
293 if (!isprint((int)*ptr))
294 *ptr = '_';
295
296 printf("%s VERSION.BIND. \"%s\"\n", int_ntoa(ip->ip_src.s_addr), buf);
297
298 return(0);
299
300}
301
302