summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/modules
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/b-scan/tmp/modules
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/b-scan/tmp/modules')
-rw-r--r--other/b-scan/tmp/modules/Makefile22
-rw-r--r--other/b-scan/tmp/modules/mod_banner.c365
-rw-r--r--other/b-scan/tmp/modules/mod_bind.c302
-rw-r--r--other/b-scan/tmp/modules/mod_httpd.c188
-rw-r--r--other/b-scan/tmp/modules/mod_ping.c205
-rw-r--r--other/b-scan/tmp/modules/mod_snmp.c899
6 files changed, 1981 insertions, 0 deletions
diff --git a/other/b-scan/tmp/modules/Makefile b/other/b-scan/tmp/modules/Makefile
new file mode 100644
index 0000000..ccac359
--- /dev/null
+++ b/other/b-scan/tmp/modules/Makefile
@@ -0,0 +1,22 @@
1CC=gcc
2COPT=-Wall -ggdb -shared -fPIC -I../include
3DEFS=`libnet-config --defines` -DUSE_LIBNET #-DDEBUG
4LIBS=
5TARGETS=mod_snmp mod_banner mod_httpd mod_ping mod_bind
6
7# HPUX 10.20
8# DEFS=`libnet-config --defines` -DUSE_LIBNET -DHP10 #-DDEBUG
9
10all:
11 for trgt in $(TARGETS); do \
12 $(CC) $(COPT) $(DEFS) -o $$trgt.so $$trgt.c; \
13 done
14
15
16clean:
17 for trgt in $(TARGETS); do \
18 rm -f $$trgt.so; \
19 done
20 rm -f core *~
21
22
diff --git a/other/b-scan/tmp/modules/mod_banner.c b/other/b-scan/tmp/modules/mod_banner.c
new file mode 100644
index 0000000..6f02a39
--- /dev/null
+++ b/other/b-scan/tmp/modules/mod_banner.c
@@ -0,0 +1,365 @@
1/*
2 * mod_example.c
3 * Example module for bscan.
4 */
5
6#include <bscan/bscan.h>
7#include <bscan/module.h>
8#include <bscan/dcd_icmp.h>
9#include <bscan/system.h>
10#include <stdio.h>
11#include <unistd.h>
12
13
14#ifndef MOD_NAME
15#define MOD_NAME "mod_banner"
16#endif
17
18#define SCN_NVT 0x00000001
19#define SCN_HALFOPEN 0x00000002
20#define SCN_XMAS 0x00000004
21#define SCN_FIN 0x00000008
22#define SCN_NULL 0x00000010
23#define SCN_PUSH 0x00000020
24#define SCN_LETOPEN 0x00000040
25#define SCN_NOSCAN 0x00000080
26#define SCN_NOICMP 0x00000100
27
28
29static int isinit=0;
30/*
31 * some variables from the binary-process
32 */
33extern int dlt_len;
34extern u_char *align_buf;
35extern unsigned short ip_options;
36extern struct ip *ip;
37extern struct Ether_header *eth;
38extern u_int plen, pcaplen;
39
40
41struct _mopt
42{
43 u_int scanflags;
44 uint8_t th_flags;
45} static mopt;
46
47
48/*
49 * static functions prototypes
50 */
51static int mdo_opt(int, char **, struct _opt *);
52static void init_vars(struct _opt *);
53static int process_rcv(struct _opt *);
54static void handle_icmp ();
55
56
57
58/*
59 * print out usage informations
60 */
61void
62musage()
63{
64 printf ("\n"MOD_NAME"\n");
65 printf (" -o <source port>, default 20\n");
66 printf (" -p <port to scan>, default 21\n");
67 printf (" -a grab all data and let the connecten established\n");
68 printf (" -n NVT terminal support (use this for telnetd-scans)\n");
69 printf (" -I dont report ICMP-errors\n");
70 printf (" -q quite, dont send out any initial packages [first-pkg]\n");
71 printf (" "MOD_NAME" only reports ICMP errors and doesn't start\n");
72 printf (" scanning. It will still simulate the tcp-stack.\n");
73 printf
74 (" -sS,-sF,-sX,-sN,-sP TCP SYN stealth, Fin, Xmas, Null, !Push scan\n");
75
76}
77
78
79/*
80 * return 0 on success, != 0 on failure
81 */
82int
83init(char **modname, int argc, char *argv[], struct _opt *opt)
84{
85#ifdef DEBUG
86 printf("MODULE INIT\n");
87#endif
88 if (isinit)
89 return(-1);
90
91 *modname = MOD_NAME;
92 isinit = 1;
93 init_vars(opt);
94
95 if (mdo_opt(argc, argv, opt) != 0)
96 return(-1);
97
98 return(0);
99}
100
101/*
102 * fini-routine. called on cleanup
103 */
104int
105fini()
106{
107#ifdef DEBUG
108 printf("MODULE FINI\n");
109#endif
110 return(0);
111}
112
113
114/*
115 *
116 */
117int
118callmdl(int entry, struct _opt *opt)
119{
120#ifdef DEBUG
121 printf("MODULE CALLMDL\n");
122#endif
123 if (entry == MOD_FIRSTPKG)
124 {
125 if (mopt.scanflags & SCN_NOSCAN)
126 return(RMOD_SKIP);
127
128 add_tcphdr(opt->packet + ETH_SIZE + IP_SIZE, &opt->nt, mopt.th_flags, 0, NULL, NULL);
129 add_iphdr (opt->packet + ETH_SIZE, IPPROTO_TCP, &opt->nt, TCP_SIZE);
130 opt->pkg_len = TCP_SIZE + IP_SIZE;
131 return(RMOD_OK);
132 }
133
134 if (entry == MOD_RCV)
135 process_rcv(opt);
136
137 return(RMOD_OK);
138}
139
140
141/*
142 ***********************************************************
143 * Our OWN/static functions for THIS module *
144 ***********************************************************
145 */
146
147/*
148 * initialize all local variables.
149 * We use some 'unused' variables of the masterprogramm
150 */
151static void
152init_vars(struct _opt *opt)
153{
154 opt->nt.sport = htons(20); /* not used by master programm */
155 opt->nt.dport = htons(21); /* not used by master programm */
156
157 mopt.scanflags = 0;
158 mopt.th_flags = TH_SYN;
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, "Iqans:p:o:")) != -1)
174 {
175 switch (c)
176 {
177 case 'I':
178 mopt.scanflags |= SCN_NOICMP;
179 break;
180 case 'q':
181 mopt.scanflags |= SCN_NOSCAN;
182 break;
183 case 'p':
184 opt->nt.dport = htons(strtoul (optarg, (char **) NULL, 10));
185 break;
186 case 'o':
187 opt->nt.sport = htons(strtoul (optarg, (char **) NULL, 10));
188 break;
189 case 'a':
190 mopt.scanflags |= SCN_LETOPEN;
191 break;
192 case 'n':
193 mopt.scanflags |= SCN_NVT; /* NVT parsing */
194 break;
195 case 's':
196 switch (optarg[0])
197 {
198 case 'S':
199 mopt.scanflags |= SCN_HALFOPEN;
200 mopt.th_flags = TH_SYN;
201 break;
202 case 'X':
203 mopt.scanflags |= SCN_XMAS;
204 mopt.th_flags = (TH_FIN | TH_PUSH | TH_URG);
205 break;
206 case 'F':
207 mopt.scanflags |= SCN_FIN;
208 mopt.th_flags = TH_FIN;
209 break;
210 case 'N':
211 mopt.scanflags |= SCN_NULL;
212 mopt.th_flags = 0;
213 break;
214 case 'P':
215 mopt.scanflags |= SCN_PUSH;
216 break;
217 default:
218 fprintf(stderr, "unrecognized option -s%c\n", optarg[0]);
219 return(-1);
220 }
221 break;
222 case ':':
223 fprintf(stderr, "missing parameter\n");
224 return(-1);
225 default:
226 return(-1);
227 }
228 }
229
230 if ((mopt.scanflags & SCN_NVT) && !(mopt.scanflags & SCN_LETOPEN))
231 fprintf(stderr, "WARNING: NVT used without -a. This is probably not what you want!\n");
232
233 return(0);
234}
235
236
237/*
238 * vrfy the icmp packet and print out 'human readable'-icmp code
239 * dest-unreachable packages only!
240 */
241static void
242handle_icmp (int offset)
243{
244 struct icmp *icmp = (struct icmp *) (align_buf + offset);
245 struct ip *icmpip;
246
247 if (plen < offset + sizeof (*icmp) + dlt_len)
248 return;
249
250 icmpip = (struct ip *) (align_buf + offset + ICMP_HDRSIZE);
251
252 printf ("%s (%d/%d) %s", int_ntoa (icmpip->ip_dst.s_addr),
253 icmp->icmp_type, icmp->icmp_code,
254 icmp_str (icmp->icmp_type, icmp->icmp_code));
255 printf ("(from %s)\n", int_ntoa (ip->ip_src.s_addr));
256}
257
258
259/*
260 * process incoming packets
261 * IP-packet is verified and variables valid (ip_options, *ip)
262 */
263static int
264process_rcv(struct _opt *opt)
265{
266 struct tcphdr *tcp;
267 unsigned short tcp_options = 0;
268 u_char *data = NULL;
269 u_char *ans = NULL; /* tcpdata answer */
270 u_char prefix[32];
271 int data_len = 0;
272 uint ans_len = 0;
273 uint res_len = 0;
274 uint tcphdr_len = 0;
275 uint iphdr_len = 0;
276
277
278 if (ip->ip_p == IPPROTO_ICMP)
279 {
280 opt->snarf.icmp_c++; /* for statistics */
281 if (!(mopt.scanflags & SCN_NOICMP))
282 handle_icmp (sizeof (*ip) + ip_options);
283
284 return(0);
285 }
286
287 if (ip->ip_p != IPPROTO_TCP)
288 return(0);
289
290 iphdr_len = sizeof(*ip) + ip_options;
291 tcp = (struct tcphdr *) (align_buf + iphdr_len);
292
293 if (vrfy_tcp(tcp, plen - dlt_len - iphdr_len , &tcp_options) != 0)
294 return(0);
295
296 if (tcp->th_flags & TH_RST)
297 {
298 opt->snarf.refused_c++;
299 printf ("%s:%d refused\n", int_ntoa (ip->ip_src.s_addr),
300 ntohs (tcp->th_sport));
301 }
302 else if (tcp->th_flags & TH_FIN)
303 {
304 opt->snarf.close_c++;
305 answer_tcp (opt->sox, ip, tcp, TH_FIN | TH_ACK, NULL, 0);
306 printf ("%s:%d connection closed by foreig host\n",
307 int_ntoa (ip->ip_src.s_addr), ntohs (tcp->th_sport));
308 }
309 else if ((tcp->th_flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK))
310 {
311
312 if (mopt.scanflags & SCN_HALFOPEN)
313 {
314 if (!(mopt.scanflags & SCN_LETOPEN))
315 answer_tcp (opt->sox, ip, tcp, TH_ACK | TH_RST, NULL, 0);
316 return(0);
317 }
318 else
319 answer_tcp (opt->sox, ip, tcp, TH_ACK, NULL, 0);
320
321 opt->snarf.open_c++;
322 printf ("%s:%d open\n", int_ntoa (ip->ip_src.s_addr),
323 ntohs (tcp->th_sport));
324 }
325
326 /* banner scanner output */
327 tcphdr_len = sizeof(*tcp) + tcp_options;
328 if (plen - dlt_len > ntohs(ip->ip_len))
329 data_len = ntohs(ip->ip_len) - iphdr_len - tcphdr_len;
330 else
331 data_len = plen - dlt_len - iphdr_len - tcphdr_len;
332
333 if (data_len <= 0)
334 return(0);
335
336 if ( (data = alloca(data_len + 1)) == NULL)
337 return(-1);
338
339 memcpy(data, align_buf + iphdr_len + tcphdr_len, data_len);
340
341 if (mopt.scanflags & SCN_NVT)
342 {
343 if ((ans = alloca(data_len)) == NULL)
344 return(-1);
345
346 decode_nvt(data, data_len, ans, &ans_len, data, &res_len);
347 data_len = res_len; /* we wrote everything into data */
348 }
349
350 snprintf(prefix, sizeof(prefix) - 1, "%s:%d ", int_ntoa(ip->ip_src),
351 ntohs (tcp->th_sport));
352 save_write(stdout, prefix, data, data_len);
353
354 if (tcp->th_flags & TH_RST) /* data_len != 0 */
355 return(0); /* evil peer resetted our connection */
356
357 /* close after first data package or ack last RST if data_len >0 */
358 if (!(mopt.scanflags & SCN_LETOPEN) || (tcp->th_flags & TH_RST))
359 answer_tcp (opt->sox, ip, tcp, TH_ACK | TH_RST, ans, ans_len);
360 else
361 answer_tcp (opt->sox, ip, tcp, TH_ACK, ans, ans_len);
362
363
364 return(0);
365}
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
diff --git a/other/b-scan/tmp/modules/mod_httpd.c b/other/b-scan/tmp/modules/mod_httpd.c
new file mode 100644
index 0000000..275125c
--- /dev/null
+++ b/other/b-scan/tmp/modules/mod_httpd.c
@@ -0,0 +1,188 @@
1/*
2 * mod_example.c
3 * Example module for bscan.
4 */
5
6#include <bscan/bscan.h>
7#include <bscan/module.h>
8#include <stdio.h>
9
10
11#ifndef MOD_NAME
12#define MOD_NAME "mod_httpd"
13#endif
14
15#define HEADREQ "HEAD / HTTP/1.0\r\n\r\n"
16
17
18static int isinit=0;
19/*
20 * some variables from the binary-process
21 */
22extern int dlt_len;
23extern u_char *align_buf;
24extern unsigned short ip_options;
25extern struct ip *ip;
26extern struct Ether_header *eth;
27extern u_int plen, pcaplen;
28
29struct _mopt
30{
31 unsigned short int dport; /* dport in NBO */
32 char *request; /* the http-request */
33} static mopt;
34
35/*
36 * static functions prototypes
37 */
38static int mdo_opt(int, char **, struct _opt *);
39static void init_vars(struct _opt *);
40static int process_rcv(struct _opt *);
41
42/*
43 * print out usage informations
44 */
45void
46musage()
47{
48 printf ("\n"MOD_NAME"\n");
49 printf (" -p <port>, default 80\n");
50 printf (" -r <request>, default 'HEAD / HTTP/1.0'\n");
51}
52
53
54/*
55 * return 0 on success, != 0 on failure
56 */
57int
58init(char **modname, int argc, char *argv[], struct _opt *opt)
59{
60#ifdef DEBUG
61 printf("MODULE INIT\n");
62#endif
63 if (isinit)
64 return(-1);
65
66 *modname = MOD_NAME;
67 isinit = 1;
68 init_vars(opt);
69
70 if (mdo_opt(argc, argv, opt) != 0)
71 return(-1);
72
73 return(0);
74}
75
76/*
77 * fini-routine. called on cleanup
78 */
79int
80fini()
81{
82#ifdef DEBUG
83 printf("MODULE FINI\n");
84#endif
85 return(0);
86}
87
88
89/*
90 *
91 */
92int
93callmdl(int entry, struct _opt *opt)
94{
95#ifdef DEBUG
96 printf("MODULE CALLMDL\n");
97#endif
98 if (entry == MOD_FIRSTPKG)
99 return(RMOD_SKIP);
100
101 if (entry == MOD_RCV)
102 process_rcv(opt);
103
104 return(RMOD_OK);
105}
106
107
108/*
109 ***********************************************************
110 * Our OWN/static functions for THIS module *
111 ***********************************************************
112 */
113
114/*
115 * initialize all local variables.
116 * We use some 'unused' variables of the masterprogramm
117 */
118static void
119init_vars(struct _opt *opt)
120{
121 mopt.dport = htons(80);
122 mopt.request = HEADREQ;
123}
124
125
126/*
127 * LOCAL/STATIC function, only available in the module
128 * return 0 on success, != 0 on failure
129 */
130static int
131mdo_opt(int argc, char *argv[], struct _opt *opt)
132{
133 extern char *optarg;
134 /*extern int optind, opterr, optopt;*/
135 int c;
136
137 while ((c = getopt (argc, argv, "p:r:")) != -1)
138 {
139 switch (c)
140 {
141 case 'p':
142 mopt.dport = htons(strtoul (optarg, (char **) NULL, 10));
143 break;
144 case 'r':
145 mopt.request = optarg;
146 fprintf(stderr, MOD_NAME ": requesting \"%s\"\n", optarg);
147 break;
148 case ':':
149 fprintf(stderr, "missing parameter\n");
150 return(-1);
151 default:
152 return(-1);
153 }
154 }
155 return(0);
156}
157
158
159/*
160 * process incoming packets
161 * IP-packet is verified and variables valid (ip_options, *ip)
162 */
163static int
164process_rcv(struct _opt *opt)
165{
166 struct tcphdr *tcp;
167 unsigned short tcp_options = 0;
168 uint iphdr_len = 0;
169
170
171 if (ip->ip_p != IPPROTO_TCP)
172 return(0);
173
174 iphdr_len = sizeof(*ip) + ip_options;
175 tcp = (struct tcphdr *) (align_buf + iphdr_len);
176
177 if (vrfy_tcp(tcp, plen - dlt_len - iphdr_len, &tcp_options) != 0)
178 return(0);
179
180 if (tcp->th_sport != mopt.dport)
181 return(0);
182
183 if ((tcp->th_flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK))
184 answer_tcp (opt->sox, ip, tcp, TH_ACK | TH_PUSH, mopt.request, strlen(mopt.request));
185
186 return(0);
187}
188
diff --git a/other/b-scan/tmp/modules/mod_ping.c b/other/b-scan/tmp/modules/mod_ping.c
new file mode 100644
index 0000000..73a90a4
--- /dev/null
+++ b/other/b-scan/tmp/modules/mod_ping.c
@@ -0,0 +1,205 @@
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_ping"
15#endif
16
17static int process_rcv(struct _opt *);
18
19static int isinit=0;
20/*
21 * some variables from the binary-process
22 */
23extern int dlt_len;
24extern u_char *align_buf;
25extern unsigned short ip_options;
26extern struct ip *ip;
27extern struct Ether_header *eth;
28extern u_int plen, pcaplen;
29extern struct timeval *pts;
30
31struct _mopt
32{
33 int type;
34 int size;
35} static mopt;
36
37/*
38 * static functions prototypes
39 */
40static int mdo_opt(int, char **, struct _opt *);
41static void init_vars(struct _opt *);
42
43/*
44 * print out usage informations
45 */
46void
47musage()
48{
49 printf ("\n"MOD_NAME"\n");
50 printf (" -t (echo|time) -s <size>, size of ping-data [default 56].\n");
51}
52
53
54/*
55 * return 0 on success, != 0 on failure
56 */
57int
58init(char **modname, int argc, char *argv[], struct _opt *opt)
59{
60#ifdef DEBUG
61 printf("MODULE INIT\n");
62#endif
63 if (isinit)
64 return(-1);
65
66 *modname = MOD_NAME;
67 isinit = 1;
68 init_vars(opt);
69
70 if (mdo_opt(argc, argv, opt) != 0)
71 return(-1);
72
73 return(0);
74}
75
76/*
77 * fini-routine. called on cleanup
78 */
79int
80fini()
81{
82#ifdef DEBUG
83 printf("MODULE FINI\n");
84#endif
85 return(0);
86}
87
88
89/*
90 * Module entry point [entry]
91 * RMOD_OK: everything allright. send the packet out [if first]
92 * or do nothing [MOD_RCV].
93 * RMOD_SKIP: proceed with next IP without sending out the packet.
94 */
95int
96callmdl(int entry, struct _opt *opt)
97{
98#ifdef DEBUG
99 printf("MODULE CALLMDL\n");
100#endif
101 if (entry == MOD_FIRSTPKG)
102 {
103 add_icmpping (opt->packet + ETH_SIZE + IP_SIZE, mopt.size, mopt.type);
104 add_iphdr (opt->packet + ETH_SIZE, IPPROTO_ICMP, &opt->nt, ICMP_SIZE + mopt.size);
105 opt->pkg_len = IP_SIZE + ICMP_SIZE + mopt.size;
106 return(RMOD_OK);
107 }
108
109 if (entry == MOD_RCV)
110 process_rcv(opt);
111
112 return(RMOD_OK);
113}
114
115
116/*
117 ***********************************************************
118 * Our OWN/static functions for THIS module *
119 ***********************************************************
120 */
121
122/*
123 * initialize all local variables.
124 * We use some 'unused' variables of the masterprogramm
125 */
126static void
127init_vars(struct _opt *opt)
128{
129 mopt.size = ICMP_ECHO;
130 mopt.size = 56;
131}
132
133
134/*
135 * LOCAL/STATIC function, only available in the module
136 * return 0 on success, != 0 on failure
137 */
138static int
139mdo_opt(int argc, char *argv[], struct _opt *opt)
140{
141 extern char *optarg;
142 /*extern int optind, opterr, optopt;*/
143 int c;
144
145 while ((c = getopt (argc, argv, "t:s:")) != -1)
146 {
147 switch (c)
148 {
149 case 't':
150 if (strcasecmp (optarg, "echo") == 0)
151 mopt.type = ICMP_ECHO;
152 else if (strcasecmp (optarg, "time") == 0)
153 mopt.type = ICMP_TSTAMP;
154 else
155 return (-1);
156 break;
157 case 's':
158 mopt.size = atoi(optarg);
159 break;
160 case ':':
161 fprintf(stderr, "missing parameter\n");
162 return(-1);
163 default:
164 return(-1);
165 }
166 }
167 return(0);
168}
169
170
171/*
172 * handle incoming icmp ECHO_REPLY packages
173 */
174static int
175process_rcv(struct _opt *opt)
176{
177 struct icmp *icmp;
178 struct timeval now;
179 double rrt;
180
181 if (ip->ip_p != IPPROTO_ICMP)
182 return(0);
183
184 if (plen < dlt_len + IP_SIZE + ip_options + sizeof(*icmp))
185 return(0); /* invalid size */
186
187 icmp = (struct icmp *) (align_buf + IP_SIZE + ip_options);
188
189// if ((icmp->icmp_type != 0) || (icmp->icmp_code != 0))
190// return(0);
191
192 memcpy(&now, pts, sizeof(now));
193 time_diff((struct timeval *)icmp->icmp_dun.id_data, &now);
194 rrt = now.tv_sec * 1000.0 + now.tv_usec / 1000.0;
195
196 printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.3f ms\n",
197 (int)(plen - dlt_len - IP_SIZE - ip_options),
198 int_ntoa(ip->ip_src.s_addr), icmp->icmp_hun.ih_idseq.icd_seq,
199 ip->ip_ttl, rrt);
200
201 return(0);
202
203}
204
205
diff --git a/other/b-scan/tmp/modules/mod_snmp.c b/other/b-scan/tmp/modules/mod_snmp.c
new file mode 100644
index 0000000..db56455
--- /dev/null
+++ b/other/b-scan/tmp/modules/mod_snmp.c
@@ -0,0 +1,899 @@
1/*
2 * SimpleNetworkManagementProtocol module for bscan.
3 * RFC 1157
4 * <buggy/lame implementation>
5 *
6 * ###fixme todo: port to sparc
7 */
8
9#include <bscan/bscan.h>
10#include <bscan/module.h>
11#include <bscan/system.h>
12#include <stdio.h>
13
14
15#ifndef MOD_NAME
16#define MOD_NAME "mod_snmp"
17#endif
18
19#define SNMP_DFLT_REQOBJ "1.3.6.1.2.1.1"
20
21#define MAX_VALSTRLEN 512
22
23#define MOPT_NOOBJ 0x01
24#define MOPT_STRIP 0x02
25
26#define SNMP_GET 0xa0
27#define SNMP_GET_NEXT 0xa1
28#define SNMP_SET 0xa3
29#define ASN_SUBCAT 0x30 /* BER encoding, sub-categorie */
30
31#ifndef ASN_INTEGER
32#define ASN_INTEGER ((u_char)0x02)
33#endif
34#define ASN_INTEGERSTR "INTEGER"
35#ifndef ASN_BIT_STR
36#define ASN_BIT_STR ((u_char)0x03)
37#endif
38#define ASN_BIT_STRSTR "BITSTRING"
39#ifndef ASN_OCTET_STR
40#define ASN_OCTET_STR ((u_char)0x04)
41#endif
42#define ASN_OCTET_STRSTR "STRING"
43#ifndef ASN_NULL
44#define ASN_NULL ((u_char)0x05)
45#endif
46#define ASN_NULLSTR "NULL"
47#ifndef ASN_OBJECT_ID
48#define ASN_OBJECT_ID ((u_char)0x06)
49#endif
50#define ASN_OBJECT_IDSTR "OBJ"
51#ifndef ASN_APPLICATION
52#define ASN_APPLICATION ((u_char)0x40)
53#endif
54#ifndef ASN_LONG_LEN
55#define ASN_LONG_LEN ((u_char)0x80)
56#endif
57#define ASN_APPLICATIONSTR "APPLICATION"
58#ifndef ASN_IPADDRESS
59#define ASN_IPADDRESS (ASN_APPLICATION | 0)
60#endif
61#define ASN_IPADDRESSSTR "IPADDR"
62#ifndef ASN_UNSIGNED
63#define ASN_UNSIGNED (ASN_APPLICATION | 2)
64#endif
65#define ASN_UNSIGNEDSTR ASN_INTEGERSTR
66#ifndef ASN_TIMETICKS
67#define ASN_TIMETICKS (ASN_APPLICATION | 3)
68#endif
69#define ASN_TIMETICKSSTR "TIMETICKS"
70#ifndef ASN_COUNTER
71#define ASN_COUNTER (ASN_APPLICATION | 1)
72#endif
73#define ASN_COUNTERSTR "COUNTER"
74
75#define SNMP_ERR_WRONGTYPE (7)
76#define SNMP_ERR_WRONGLENGTH (8)
77#define SNMP_ERR_WRONGENCODING (9)
78#define SNMP_ERR_WRONGVALUE (10)
79#define SNMP_ERR_NOCREATION (11)
80#define SNMP_ERR_INCONSISTENTVALUE (12)
81#define SNMP_ERR_RESOURCEUNAVAILABLE (13)
82#define SNMP_ERR_COMMITFAILED (14)
83#define SNMP_ERR_UNDOFAILED (15)
84#define SNMP_ERR_AUTHORIZATIONERROR (16)
85#define SNMP_ERR_NOTWRITABLE (17)
86
87char *snmp_error[] = {
88 "NO ERROR",
89 "TOO BIG",
90 "NO SUCH NAME",
91 "BAD VALUE",
92 "READONLY",
93 "GENERELL ERROR",
94 "NO ACCESS",
95 "WRONG TYPE",
96 "WRONG LENGTH",
97 "WRONG ENCODING",
98 "WRONG VALUE",
99 "NO CREATION",
100 "INCONSISTENT VALUE",
101 "RESOURCE UNAVAILABLE",
102 "COMMIT FAILED",
103 "UNDO FAILED",
104 "AUTORISATION ERROR",
105 "NOT WRITEABLE",
106 "INCONSISTENT NAME" };
107
108#define MAX_SNMP_ERR 18
109
110
111
112#define ADD_DATA(ptr, in, len, totlen) memcpy(ptr, in, len);\
113 *totlen = *totlen + len;
114
115#define min(a,b) ((a)<(b)?(a):(b))
116
117
118static int isinit=0;
119/*
120 * some variables from the binary-process
121 */
122extern int dlt_len;
123extern u_char *align_buf;
124extern unsigned short ip_options;
125extern struct ip *ip;
126extern struct Ether_header *eth;
127extern u_int plen, pcaplen;
128extern struct timeval *pts;
129
130struct _pdu
131{
132 u_char *varbuf;
133 int varlen;
134};
135
136struct _mopt
137{
138 char *community;
139 u_char flags;
140 u_char snmptor; /* type of request */
141 struct _pdu pdu;
142} static mopt;
143
144struct _pdunfo
145{
146 u_char *community;
147 u_char *pdu_type;
148 u_char *error_status;
149 u_char *error_idx;
150} static pdunfo;
151
152
153/*
154 * static functions prototypes
155 */
156static int mdo_opt(int, char **, struct _opt *);
157static void init_vars(struct _opt *);
158static int process_rcv(struct _opt *);
159static int add_snmp (u_char *, int *);
160static int add_snmpreq (u_char *, int *, u_char, u_char *, struct _pdu *);
161static int add_snmp_var(struct _pdu *, u_char *);
162static int build_snmp_objreq(u_char *, u_char *,u_char *, u_char, u_char *);
163static int str2asnv(u_char *, u_char, u_char *, u_char *);
164static int str2objid(u_char *, u_char *, u_char *);
165
166
167/*
168 * print out usage informations
169 */
170void
171musage()
172{
173 printf ("\n"MOD_NAME"\n");
174 printf ("snmp module\n");
175 printf (" -p <port>, destination port, default 161\n");
176 printf (" -o <port>, source port, default 53\n");
177 printf (" -r <request[:type[:value]]>, default '1.3.6.1.2.1.1 (system)'\n");
178 printf (" -c <community name>, default 'public'\n");
179 printf (" -g <requesttype>, (get|getnext|set), default 'getnext'\n");
180 printf (" -s strip unprintable characters from STRING types, and output as text\n");
181 printf (" -q dont print out OBJ-types\n");
182 printf ("\n");
183 printf ("type: i: INTEGER, s: STRING, n: NULLOBJ, o: OBJID\n");
184 printf (" t: TIMETICKS, a: IPADDRESS, b: BITSTRING, c: COUNTER\n");
185 printf ("\n");
186 printf ("you can request multiple objects with one request:\n");
187 printf ("-g get -r 1.3.6.1.2.1.1.1.0 -r 1.3.6.1.2.1.1.4.0 -r 1.3.6.1.2.1.1.5.0\n");
188}
189
190
191/*
192 * return 0 on success, != 0 on failure
193 */
194int
195init(char **modname, int argc, char *argv[], struct _opt *opt)
196{
197#ifdef DEBUG
198 printf("MODULE INIT\n");
199#endif
200 if (isinit)
201 return(-1);
202
203 *modname = MOD_NAME;
204 isinit = 1;
205 init_vars(opt);
206
207 if (mdo_opt(argc, argv, opt) != 0)
208 return(-1);
209
210 return(0);
211}
212
213/*
214 * fini-routine. called on cleanup
215 */
216int
217fini()
218{
219#ifdef DEBUG
220 printf("MODULE FINI\n");
221#endif
222 return(0);
223}
224
225
226/*
227 * Module entry point [entry]
228 * RMOD_OK: everything allright. send the packet out [if first]
229 * or do nothing [MOD_RCV].
230 * RMOD_SKIP: proceed with next IP without sending out the packet.
231 * RMOD_ERROR: failed to create packet.
232 * RMOD_ABRT: critical failure, abort!
233 */
234int
235callmdl(int entry, struct _opt *opt)
236{
237#ifdef DEBUG
238 printf("MODULE CALLMDL\n");
239#endif
240 if (entry == MOD_FIRSTPKG)
241 {
242 add_snmp(opt->packet+ ETH_SIZE + IP_SIZE + UDP_SIZE, &opt->pkg_len);
243 add_udphdr (opt->packet+ ETH_SIZE+ IP_SIZE, &opt->nt, opt->pkg_len);
244 add_iphdr (opt->packet + ETH_SIZE, IPPROTO_UDP, &opt->nt, opt->pkg_len + UDP_SIZE);
245 opt->pkg_len += UDP_SIZE + IP_SIZE;
246
247 return(RMOD_OK);
248 }
249
250 if (entry == MOD_RCV)
251 process_rcv(opt);
252
253 return(RMOD_OK);
254}
255
256
257/*
258 ***********************************************************
259 * Our OWN/static functions for THIS module *
260 ***********************************************************
261 */
262
263/*
264 * initialize all local variables.
265 * We use some 'unused' variables of the masterprogramm
266 */
267static void
268init_vars(struct _opt *opt)
269{
270 opt->nt.sport = htons(32770);
271 opt->nt.dport = htons(161);
272 mopt.flags = 0;
273 mopt.community = "public";
274 mopt.snmptor = SNMP_GET_NEXT;
275}
276
277
278/*
279 * LOCAL/STATIC function, only available in the module
280 * return 0 on success, != 0 on failure
281 */
282static int
283mdo_opt(int argc, char *argv[], struct _opt *opt)
284{
285 extern char *optarg;
286 /*extern int optind, opterr, optopt;*/
287 int c;
288
289 while ((c = getopt (argc, argv, "qsg:c:r:p:o:")) != -1)
290 {
291 switch (c)
292 {
293 case 'q':
294 mopt.flags |= MOPT_NOOBJ;
295 break;
296 case 's':
297 mopt.flags |= MOPT_STRIP;
298 break;
299 case 'p':
300 opt->nt.dport = htons(atoi(optarg));
301 break;
302 case 'o':
303 opt->nt.sport = htons(atoi(optarg));
304 break;
305 case 'r':
306 add_snmp_var(&mopt.pdu, optarg);
307 break;
308 case 'c':
309 mopt.community = optarg;
310 break;
311 case 'g':
312 if (strcasecmp (optarg, "get") == 0)
313 mopt.snmptor = SNMP_GET;
314 else if (strcasecmp (optarg, "getnext") == 0)
315 mopt.snmptor = SNMP_GET_NEXT;
316 else if (strcasecmp (optarg, "set") == 0)
317 mopt.snmptor = SNMP_SET;
318 else
319 return (-1);
320 break;
321 case ':':
322 fprintf(stderr, "missing parameter\n");
323 return(-1);
324 default:
325 return(-1);
326 }
327 }
328
329 if (mopt.pdu.varbuf == NULL)
330 add_snmp_var(&mopt.pdu, SNMP_DFLT_REQOBJ);
331
332 return(0);
333}
334
335
336static u_char
337strtoasn_vtype(int src)
338{
339
340 src = tolower(src);
341
342 switch (src)
343 {
344 case 'i':
345 return(ASN_INTEGER);
346 case 's':
347 return(ASN_OCTET_STR);
348 case 'n':
349 return(ASN_NULL);
350 case 'o':
351 return(ASN_OBJECT_ID);
352 case 't':
353 return(ASN_TIMETICKS);
354 case 'b':
355 return(ASN_BIT_STR);
356 case 'a':
357 return(ASN_IPADDRESS);
358 case 'u':
359 return(ASN_UNSIGNED);
360 case 'c':
361 return(ASN_COUNTER);
362 default:
363 fprintf(stderr, "WARNING: unsupported value-type.\n");
364 return(src);
365 }
366
367 return(ASN_NULL);
368}
369
370/*
371 * add variable to our variable-queue
372 * input: <objid-dodded notation>:<value type>:<value>-format
373 * return 0 on success, !=0 on parse error etc
374 */
375static int
376add_snmp_var(struct _pdu *pdu, u_char *src)
377{
378 u_char *request;
379 u_char vtype = 5;
380 u_char *value = NULL;
381 u_char *ptr = NULL;
382 u_char reqlen=0;
383
384 if (pdu->varbuf == NULL)
385 {
386 pdu->varbuf = calloc(1, 1024);
387 pdu->varlen = 0;
388 }
389
390 request = src;
391
392 if ( (ptr = strchr(src, ':')) != NULL)
393 *ptr++ = '\0';
394
395 src = ptr;
396 if (ptr != NULL)
397 {
398 if ( (ptr = strchr(src, ':')) != NULL)
399 {
400 *ptr++ = '\0';
401 if (strlen(ptr) > 0)
402 value = ptr;
403 }
404 vtype = strtoasn_vtype(*src);
405 }
406
407 if (build_snmp_objreq(pdu->varbuf + pdu->varlen, &reqlen, request, vtype, value) != 0)
408 {
409 fprintf(stderr, "WARNING: error while parsing reqOBJ\n");
410 return(-1);
411 }
412
413 pdu->varlen += reqlen;
414
415 return(0);
416}
417
418/*
419 * convert OBJ-ID and build the snmp-request packet
420 * save the work and reuse [better performance, the snmp
421 * packet is always the same].
422 * return 0 on success
423 */
424static int
425add_snmp(u_char *ptr, int *lenptr)
426{
427 static u_char *buf = NULL;
428 static int buflen;
429
430 if (buf != NULL) /* fastmode, use old copy of previous build snmppkg */
431 {
432 *lenptr = buflen;
433 memcpy(ptr, buf, buflen);
434 return(0);
435 }
436
437 if (buf == NULL)
438 buf = malloc(1024);
439
440 add_snmpreq (ptr, lenptr, mopt.snmptor, mopt.community, &mopt.pdu);
441
442 memcpy(buf, ptr, *lenptr); /* for later reuse */
443 buflen = *lenptr;
444
445 return(0);
446}
447
448/*
449 * convert snmp obj in dotted-notation, 0-terminated string to obj-id in asn.1
450 */
451static int
452str2objid(u_char *dst, u_char *retlen, u_char *src)
453{
454 u_char *dotptr;
455 u_char len;
456
457 if (strncmp(src, "1.3.6.1.", 8) != 0)
458 return(-1); /* snmp only , iso.org.dot.internet.* */
459
460 src += 8; /* we know the first 8 bytes. */
461
462 memcpy(dst, "\x2b\x06\x01", 3); /* yepp, we know this. "1.3.6.1" */
463 dst += 3;
464 dotptr = src;
465 len = 3; /* 2b-06-01 */
466 while ( (dotptr = strchr (src, '.')) != NULL)
467 {
468 *dotptr = '\0';
469 *dst++ = (u_char)atoi(src);
470 src = ++dotptr;
471 len++;
472 }
473 if (strlen(src) > 0)
474 {
475 *dst++ = (u_char)atoi(src);
476 len++;
477 }
478
479 *retlen = len;
480 return(0);
481}
482
483
484/*
485 * convert input to ASN.1 BER encodet <obj-id><value>
486 * dst: guess what.
487 * ptr: snmp obj in dotted-notation (1.3.6.1.2.1.1....), 0-terminated string
488 * tov: type of value, ASN.1 encodet (int, 8octet string, ...)
489 * value: the value (string, 0-terminated)
490 * return: 0 on success
491 */
492static int
493build_snmp_objreq(u_char *dst, u_char *retlen, u_char *src, u_char tov,
494 u_char *value)
495{
496 u_char *srcw;
497 u_char vlen = 0;
498 u_char *sublen;
499 u_char *subsublen;
500 u_char *subvlen;
501 u_char len;
502
503 srcw = alloca(strlen(src)+1);
504 memcpy(srcw, src, strlen(src)+1);
505 if (strlen(srcw) <= 4)
506 return(-1);
507
508 *dst++ = ASN_SUBCAT;
509 sublen = dst++; /* we set the length later coz we dont know it yet */
510
511 *dst++ = 0x06; /* OBJ-ID */
512 subsublen = dst++; /* and this also not */
513
514 if (str2objid(dst, &len, srcw) != 0)
515 return(-1);
516
517 *subsublen = len; /* length of obj */
518 dst += len;
519
520 *dst++ = tov; /* type of value */
521 subvlen = dst++; /* we set this later..we dont know the length yet */
522 str2asnv(value, tov, dst, &vlen);
523
524 *subvlen = vlen; /* length of value */
525
526 *sublen = len + vlen + 4; /* length of <obj-id><tov:value> */
527
528 *retlen = len + vlen + 6;
529 return(0);
530}
531
532
533/*
534 * convert 0-terminated string value to asn encodet value
535 * return 0 on success
536 * on return the length of rvalue < value.
537 * input: value [0 terminated string]
538 * tov [asn]-type of value
539 * output: rvalue [non 0-terminated string with asn-value]
540 * vlen [length of rvalue]
541 * return 0 on success
542 * attention: we use full integers (length=4, not reduced in length
543 * to the minimum size). the snmp-lib reduces the length of an
544 * integer to the minimum size...but this is not a must
545 */
546static int
547str2asnv(u_char *value, u_char tov, u_char *rvalue, u_char *vlen)
548{
549 unsigned long int ltmp;
550
551 if (rvalue == NULL)
552 return(0); /* yes, NULL is allowed */
553 if ((rvalue != NULL) && (vlen == NULL))
554 return(-1);
555
556 switch(tov)
557 {
558 case ASN_INTEGER:
559 ltmp = htonl(strtol(value, NULL, 10));
560 memcpy(rvalue, &ltmp, sizeof(ltmp));
561 *vlen = sizeof(ltmp);
562 break;
563 case ASN_UNSIGNED:
564 case ASN_COUNTER:
565 case ASN_TIMETICKS:
566 ltmp = htonl(strtoul(value, NULL, 10));
567 memcpy(rvalue, &ltmp, sizeof(ltmp));
568 *vlen = (sizeof(ltmp));
569 break;
570 case ASN_IPADDRESS:
571 ltmp = inet_addr(value);
572 memcpy(rvalue, &ltmp, sizeof(ltmp));
573 *vlen = sizeof(ltmp);
574 break;
575 case ASN_OBJECT_ID:
576 str2objid(rvalue, vlen, value);
577 break;
578 case ASN_OCTET_STR:
579 memcpy(rvalue, value, strlen(value));
580 *vlen = strlen(value);
581 break;
582 case ASN_NULL:
583 *vlen = 0;
584 break;
585 default:
586 *vlen = 0;
587 fprintf(stderr, "WARNING, unsupported value type !\n");
588 }
589
590 return(0);
591}
592
593/*
594 * add snmp request
595 * build the entire SNMP packet [version, community, pdu, id, error, idx, ..]
596 * req: objs + values [BER encodet, already with header and \x30 sub start]
597 * reqlen: len of entire req [objs + values]
598 * tor: type of request [get, getnext, set]
599 */
600static int
601add_snmpreq (u_char *pkt, int *len, u_char tor, u_char *community, struct _pdu *pdu)
602{
603 int le = 0;
604
605 *(pkt + le++) = ASN_SUBCAT;
606 *(pkt + le++) = (u_char)(pdu->varlen + strlen(community) + 18);
607 ADD_DATA(pkt + le, "\x02\x01\x00\x04" , 4, &le); /* SNMPv1 + STRINGOBJ*/
608 *(pkt + le++) = (u_char)strlen(community);
609 ADD_DATA(pkt + le, community, strlen(community), &le);
610 *(pkt + le++) = tor; /* PDU GET-NEXTrequest */
611 /* lenof(<req-id> + <err-state> + <err-idx> + <SUB> <obj+values>) */
612 *(pkt + le++) = (u_char)(pdu->varlen + 11);
613 /* <req-id> + <err-state> + <err-idx> <SUB-OBJ> */
614 ADD_DATA(pkt + le, "\x02\x01\x01\x02\x01\00\x02\x01\x00\x30", 10, &le);
615 *(pkt + le++) = (u_char)(pdu->varlen);
616 ADD_DATA(pkt + le, pdu->varbuf, pdu->varlen, &le);
617
618 *len = le;
619 return(0);
620}
621
622
623/*
624 * convert asn encoded obj to string
625 * input: string of <type of value><length of value><value>
626 * datalen: max len i'm allowed to read from "ptr --> ..."
627 * return:
628 * prefix is the string representation of the value-type e.g. "OCTET-STRING"..
629 * and '<trunc>' if value got truncated.
630 * is < 64 chars...
631 * val: the value (0-terminated string)
632 * return 0 on success
633 * 1 if truncated (value-length > size of snmp or val to small)
634 * -1 on error
635 */
636static int
637asn2str(u_char *ptr, u_char *prefix, u_char *val, unsigned int datalen)
638{
639 unsigned long int len, day, hour, min, sec, msec;
640 u_char *ptr2;
641 u_char vlen = *(ptr+1); /* saved value length */
642 u_char tov = *ptr;
643 unsigned long int ltmp;
644 int i, slen = 0;
645 u_char buf[128];
646
647 if (vlen > datalen-2)
648 len = datalen-2;
649 else
650 len = vlen;
651
652 *val = '\0';
653 *prefix = '\0';
654
655 switch(tov)
656 {
657 case ASN_IPADDRESS:
658 if (len > sizeof(ltmp))
659 len = sizeof(ltmp);
660
661 ptr2 = (u_char *)&ltmp;
662 memcpy(ptr2 + sizeof(ltmp) - len, ptr+2, len);
663 sprintf(val, "%s", int_ntoa(ltmp));
664 strcpy(prefix, ASN_IPADDRESSSTR);
665 break;
666 case ASN_NULL:
667 strcpy(prefix, ASN_NULLSTR);
668 break;
669 case ASN_TIMETICKS:
670 if (len > sizeof(ltmp))
671 len = sizeof(ltmp);
672
673 ltmp = 0;
674 ptr2 = (u_char *)&ltmp;
675 memcpy(ptr2 + sizeof(ltmp) - len, ptr+2, len);
676 ltmp = ntohl(ltmp);
677 day = ltmp / 8640000;
678 hour = (ltmp % 8640000) / 360000;
679 min = (ltmp % 360000) / 6000;
680 sec = (ltmp % 6000) / 100;
681 msec = (ltmp % 100);
682 sprintf(val, "(%lu) %d days, %2.2d:%2.2d:%2.2d.%d", ltmp,
683 (int)day, (int)hour, (int)min, (int)sec, (int)msec);
684 if (tov == ASN_TIMETICKS)
685 strcpy(prefix, ASN_TIMETICKSSTR);
686 break;
687 case ASN_INTEGER:
688 case ASN_UNSIGNED:
689 case ASN_COUNTER:
690 ltmp = 0;
691 if (len > sizeof(ltmp))
692 len = sizeof(ltmp);
693
694 ptr2 = (u_char *)&ltmp;
695 memcpy(ptr2 + sizeof(ltmp) - len, ptr+2, len);
696
697 if (tov == ASN_INTEGER)
698 sprintf(val, "%lu", (unsigned long int)ntohl(ltmp));
699 else
700 sprintf(val, "%ld", (long int)ntohl(ltmp));
701
702 if (tov == ASN_INTEGER)
703 strcpy(prefix, ASN_INTEGERSTR);
704 if (tov == ASN_UNSIGNED)
705 strcpy(prefix, ASN_UNSIGNEDSTR);
706 if (tov == ASN_COUNTER)
707 strcpy(prefix, ASN_COUNTERSTR);
708 break;
709 case ASN_OCTET_STR:
710 if (isprintdata(ptr+2, len))
711 {
712 if (len > MAX_VALSTRLEN-1)
713 len = MAX_VALSTRLEN-1;
714 memcpy(val, ptr+2, len);
715 val[len] = '\0';
716 } else if ((mopt.flags & MOPT_STRIP) == MOPT_STRIP) {
717 dat2strip(val, MAX_VALSTRLEN, ptr+2, len);
718 } else {
719 dat2hexstr(val, MAX_VALSTRLEN, ptr+2, len);
720 }
721
722 strcpy(prefix, ASN_OCTET_STRSTR);
723 break;
724 case ASN_OBJECT_ID:
725 i = 0;
726 slen = 0;
727 while(i < len)
728 {
729 if (*(ptr+2+i) == 0x2b)
730 strcpy(buf, "1.3."); /* substituate shorts */
731 else
732 snprintf(buf, sizeof(buf), "%d.", *(ptr+2+i));
733
734 buf[sizeof(buf)-1] = '\0';
735 slen = strlen(val) + strlen(buf);
736
737 if (slen < MAX_VALSTRLEN -1)
738 strcat(val, buf);
739 else
740 break;
741
742 i++;
743 }
744 val[slen-1] = '\0'; /* remove last '.' */
745 strcpy(prefix, ASN_OBJECT_IDSTR);
746 break;
747 default:
748 dat2hexstr(val, MAX_VALSTRLEN, ptr+2, len);
749 break;
750 }
751
752 if (*prefix == '\0')
753 strcpy(prefix, "UNKNOWN");
754
755 if (vlen > len)
756 {
757 strcat(prefix, "<trunc>");
758 return(1);
759 }
760
761 return(0);
762}
763
764/*
765 * return TypeOfValue and let next point to the next asn encodet obj
766 * ptr: pointer to asn.1 encodet obj
767 * len: returns the length of the OLD value + suffix [length we skipped]
768 * next: returns a pointer to the next obj.
769 */
770static u_char
771asn_getnext(u_char *ptr, u_char *len, u_char **next)
772{
773 u_char vlen = *(ptr+1);
774
775 if (*ptr == ASN_SUBCAT)
776 {
777 if (vlen & ASN_LONG_LEN)
778 vlen = vlen & ~ASN_LONG_LEN;
779 else
780 vlen = 0;
781 }
782
783 *next = ptr + vlen + 2;
784
785 *len = vlen + 2;
786 return(**next);
787}
788
789#define RETURN_ILLLEN(x,y,r) if (x >= y) return(r);
790
791/*
792 * handle incoming snmp answers, answer with 'next' if not last record
793 */
794static int
795process_rcv(struct _opt *opt)
796{
797 struct udphdr *udp;
798 u_char *ptr;
799 int len;
800 int snmplen;
801 uint iphdr_len = 0;
802 u_char objlen;
803 u_char buf[MAX_VALSTRLEN];
804 u_char prefix[32];
805 u_char buf2[MAX_VALSTRLEN + 32];
806 u_char asnprefix[128];
807 u_char c;
808
809 if (ip->ip_p != IPPROTO_UDP)
810 return(0);
811
812 iphdr_len = IP_SIZE + ip_options;
813 if (plen < dlt_len + iphdr_len + sizeof(*udp))
814 return(-1); /* invalid size */
815
816 udp = (struct udphdr *) (align_buf + iphdr_len);
817 ptr = (u_char *) (align_buf + iphdr_len + sizeof(*udp));
818 snmplen = plen - dlt_len - iphdr_len - sizeof(*udp);
819 len = 0;
820
821 /*
822 * we dont check the value of the ASN_SUBCAT-length coz many buggy
823 * hosts out there return a wrong length [0xff, 0x80, ...]
824 */
825
826 ptr += 2; len += 3; /* pointing on the 3rd element */
827 RETURN_ILLLEN(len, snmplen, 0);
828
829 asn_getnext(ptr, &objlen, &ptr); /* skip version, get community */
830 len += objlen;
831 RETURN_ILLLEN(len, snmplen, 0);
832 pdunfo.community = ptr;
833
834 asn_getnext(ptr, &objlen, &ptr); /* skip community, get pdu */
835 len += objlen;
836 RETURN_ILLLEN(len, snmplen, 0);
837 pdunfo.pdu_type = ptr;
838 ptr += 2;
839 len += 2; /* skip pdu, get reqid */
840 RETURN_ILLLEN(len, snmplen, 0);
841
842 asn_getnext(ptr, &objlen, &ptr); /* skip reqid, get errorstatus */
843 len += objlen;
844 RETURN_ILLLEN(len, snmplen, 0);
845 pdunfo.error_status = ptr;
846
847 asn_getnext(ptr, &objlen, &ptr); /* skip errorstatus, get erroridx */
848 len += objlen;
849 RETURN_ILLLEN(len, snmplen, 0);
850 pdunfo.error_idx = ptr;
851
852 asn_getnext(ptr, &objlen, &ptr); /* skip erroridx, get */
853 len += objlen;
854 RETURN_ILLLEN(len, snmplen, 0);
855
856 asn_getnext(ptr, &objlen, &ptr); /* we reached the SUB section */
857 len += objlen;
858 RETURN_ILLLEN(len, snmplen, 0);
859
860 snprintf(prefix, sizeof(prefix) - 1, "%s:%d ", int_ntoa(ip->ip_src),
861 ntohs(udp->uh_sport));
862
863 c = *(pdunfo.error_status+2);
864 if (c != 0)
865 {
866 if (c < MAX_SNMP_ERR)
867 {
868 printf("%s%s (%d)\n", prefix, snmp_error[c],
869 *(pdunfo.error_idx+2));
870 } else {
871 printf("%s UNKNOWN ERROR\n", prefix);
872 }
873 }
874
875 while (1)
876 {
877 asn_getnext(ptr, &objlen, &ptr);
878 if (objlen == 0)
879 return(0);
880
881 len += objlen;
882 RETURN_ILLLEN(len, snmplen, 0);
883 if (*ptr == ASN_SUBCAT)
884 continue;
885
886 if ((mopt.flags & MOPT_NOOBJ) && (*ptr == ASN_OBJECT_ID))
887 continue;
888
889 asn2str(ptr, asnprefix, buf, snmplen - len + 1);
890 snprintf(buf2, sizeof(buf2)-1, "%s %s", asnprefix, buf);
891 buf2[sizeof(buf2)-1] = '\0';
892 save_write(stdout, prefix, buf2, strlen(buf2));
893 }
894
895 return(0);
896
897}
898
899