3 * $Id: mallory.c,v 1.3 2004/04/03 12:35:13 mdw Exp $
5 * An evil proxy for TrIPE
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 2004/04/03 12:35:13 mdw
33 * Support elliptic curve key exchange.
35 * Revision 1.2 2003/10/15 09:31:06 mdw
36 * Make forking work properly.
38 * Revision 1.1 2001/06/19 22:11:14 mdw
39 * The beginnings of a malicious proxy for TrIPE.
43 /*----- Header files ------------------------------------------------------*/
54 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
64 #include <mLib/alloc.h>
65 #include <mLib/dstr.h>
66 #include <mLib/fdflags.h>
67 #include <mLib/mdwopt.h>
68 #include <mLib/quis.h>
69 #include <mLib/report.h>
74 #include <catacomb/buf.h>
76 #include <catacomb/key.h>
78 #include <catacomb/mp.h>
79 #include <catacomb/mprand.h>
80 #include <catacomb/dh.h>
82 #include <catacomb/noise.h>
83 #include <catacomb/rand.h>
84 #include <catacomb/rc4.h>
87 /*----- Data structures ---------------------------------------------------*/
96 typedef struct filter
{
99 void (*func
)(struct filter */
*f*/
, const octet */
*buf*/
, size_t /*sz*/);
103 typedef struct qnode
{
108 /*----- Static variables --------------------------------------------------*/
110 #define PKBUFSZ 65536
112 static sel_state sel
;
113 static peer peers
[2];
114 static unsigned npeer
= 0;
115 static key_file keys
;
118 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
119 #define RND(i) (rng->ops->range(rng, (i)))
121 /*----- Peer management ---------------------------------------------------*/
123 static void dopacket(int fd
, unsigned mode
, void *vv
)
127 int r
= read(fd
, buf
, sizeof(buf
));
129 printf("recv from `%s'\n", p
->name
);
134 static void addpeer(unsigned ac
, char **av
)
136 key_packstruct kps
[DH_PUBFETCHSZ
];
139 struct sockaddr_in sin
;
146 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
148 die(1, "enough peers already");
150 p
->name
= xstrdup(av
[0]);
151 kp
= key_fetchinit(dh_pubfetch
, kps
, &p
->kpub
);
152 e
= key_fetchbyname(kp
, &keys
, av
[0]);
155 die(1, "key_fetch `%s': %s", av
[0], key_strerror(e
));
156 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
157 die(1, "socket: %s", strerror(errno
));
158 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
159 memset(&sin
, 0, sizeof(sin
));
160 sin
.sin_family
= AF_INET
;
161 sin
.sin_addr
.s_addr
= INADDR_ANY
;
162 sin
.sin_port
= htons(atoi(av
[1]));
163 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
164 die(1, "bind: %s", strerror(errno
));
165 memset(&sin
, 0, sizeof(sin
));
166 sin
.sin_family
= AF_INET
;
167 if ((h
= gethostbyname(av
[2])) == 0)
168 die(1, "gethostbyname `%s'", av
[2]);
169 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
170 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
)))
171 die(1, "setsockopt: %s", strerror(errno
));
172 memcpy(&sin
.sin_addr
, h
->h_addr
, sizeof(sin
.sin_addr
));
173 sin
.sin_port
= htons(atoi(av
[3]));
174 if (connect(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
175 die(1, "connect: %s", strerror(errno
));
176 sel_initfile(&sel
, &p
->sf
, fd
, SEL_READ
, dopacket
, p
);
180 /*----- Fork filter -------------------------------------------------------*/
182 typedef struct forknode
{
183 struct forknode
*next
;
187 typedef struct forkfilt
{
192 static void dofork(filter
*f
, const octet
*buf
, size_t sz
)
194 forkfilt
*ff
= f
->state
;
199 for (fn
= ff
->fn
; fn
; fn
= fn
->next
) {
200 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
201 PASS(fn
->f
, buf
, sz
);
203 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
204 PASS(f
->next
, buf
, sz
);
207 static void addfork(filter
*f
, unsigned ac
, char **av
)
211 die(1, "syntax: filt:fork:NAME");
212 ff
= CREATE(forkfilt
);
213 ff
->name
= xstrdup(av
[0]);
219 static void nextfork(unsigned ac
, char **av
)
228 die(1, "syntax: next:NAME:...");
229 for (i
= 0; i
< 2; i
++) {
231 for (f
= p
->f
; f
; f
= f
->next
) {
232 if (f
->func
!= dofork
)
235 for (j
= 0; j
< ac
; j
++) {
236 if (strcmp(av
[j
], ff
->name
) == 0)
241 fn
= CREATE(forknode
);
242 for (ffn
= &ff
->fn
; *ffn
; ffn
= &(*ffn
)->next
)
252 /*----- Corrupt filter ----------------------------------------------------*/
254 typedef struct corrupt
{
258 static void docorrupt(filter
*f
, const octet
*buf
, size_t sz
)
260 corrupt
*c
= f
->state
;
264 while (!RND(c
->p_corrupt
)) {
265 puts("corrupt packet");
266 b
[RND(sz
)] ^= RND(256);
268 PASS(f
->next
, b
, sz
);
271 static void addcorrupt(filter
*f
, unsigned ac
, char **av
)
275 die(1, "syntax: filt:corrupt[:PCORRUPT]");
278 c
->p_corrupt
= atoi(av
[0]);
285 /*----- Delay filter ------------------------------------------------------*/
287 typedef struct delaynode
{
297 typedef struct delay
{
305 static void dtimer(struct timeval
*tv
, void *vv
);
307 static void dinsert(delaynode
*dn
)
311 unsigned long tdelta
= RND(dn
->d
->t
);
312 gettimeofday(&tv
, 0);
313 TV_ADDL(&tv
, &tv
, 0, tdelta
);
315 sel_addtimer(&sel
, &dn
->tm
, &tv
, dtimer
, dn
);
317 for (ta
= tb
= sel
.timers
; ta
; ta
= ta
->next
) {
318 ta
= ta
->next
; if (!ta
) break; assert(ta
!= tb
);
319 ta
= ta
->next
; if (!ta
) break; assert(ta
!= tb
);
322 printf(" delay %lu usecs", tdelta
);
325 static void dsend(delaynode
*dn
, unsigned force
)
329 fputs(" send...\n", stdout
);
331 PASS(d
->f
->next
, dn
->buf
, dn
->sz
);
332 fputs("delay ...", stdout
);
341 sel_rmtimer(&ddn
->tm
);
342 sel_addtimer(&sel
, &dn
->tm
, &ddn
->tm
.tv
, dtimer
, dn
);
349 printf(" move id %u from slot %u to slot %u", ddn
->seq
, ddn
->i
, dn
->i
);
351 { unsigned i
; for (i
= 0; i
< d
->n
; i
++) assert(d
->q
[i
].buf
); }
352 fputs(" remove", stdout
);
356 static void dtimer(struct timeval
*tv
, void *vv
)
359 printf("delay timer peer `%s' id %u slot %u",
360 dn
->d
->f
->p_from
->name
, dn
->seq
, dn
->i
);
362 dsend(dn
, RND(dn
->d
->p_replay
));
366 static void dodelay(filter
*f
, const octet
*buf
, size_t sz
)
370 static unsigned seq
= 0;
372 fputs("delay", stdout
);
373 if (d
->n
== d
->max
) {
374 dn
= &d
->q
[RND(d
->n
)];
375 printf(" force uid %u", dn
->seq
);
376 sel_rmtimer(&dn
->tm
);
383 printf(" new id %u in slot %u", dn
->seq
, dn
->i
);
384 dn
->buf
= xmalloc(sz
);
386 memcpy(dn
->buf
, buf
, sz
);
391 static void adddelay(filter
*f
, unsigned ac
, char **av
)
396 if (ac
< 1 || ac
> 3)
397 die(1, "syntax: filt:delay:QLEN[:MILLIS:PREPLAY]");
399 d
->max
= atoi(av
[0]);
401 d
->t
= strtoul(av
[1], 0, 10);
406 d
->p_replay
= atoi(av
[2]);
410 d
->q
= xmalloc(d
->max
* sizeof(delaynode
));
414 for (i
= 0; i
< d
->max
; i
++) {
422 /*----- Filters -----------------------------------------------------------*/
424 static void dosend(filter
*f
, const octet
*buf
, size_t sz
)
426 printf("send to `%s'\n", f
->p_to
->name
);
427 write(f
->p_to
->sf
.fd
, buf
, sz
);
430 static void addsend(filter
*f
, unsigned ac
, char **av
)
433 die(1, "syntax: filt:send");
437 const struct filtab
{
439 void (*func
)(filter */
*f*/
, unsigned /*ac*/, char **/
*av*/
);
443 { "delay", adddelay
},
444 { "corrupt", addcorrupt
},
448 static void dofilter(peer
*from
, peer
*to
, unsigned ac
, char **av
)
450 filter
**ff
, *f
= CREATE(filter
);
451 const struct filtab
*ft
;
453 die(1, "syntax: {l,r,}filt:NAME:...");
458 for (ff
= &from
->f
; *ff
; ff
= &(*ff
)->next
)
461 for (ft
= filtab
; ft
->name
; ft
++) {
462 if (strcmp(av
[0], ft
->name
) == 0) {
463 ft
->func(f
, ac
- 1, av
+ 1);
467 die(1, "unknown filter `%s'", av
[0]);
470 /*----- Flooding ----------------------------------------------------------*/
472 typedef struct flood
{
480 static void setflood(flood
*f
);
482 static void floodtimer(struct timeval
*tv
, void *vv
)
494 rng
->ops
->fill(rng
, buf
, sz
);
497 puts("flood packet");
498 PASS(f
->p
->f
, buf
, sz
);
502 static void setflood(flood
*f
)
505 gettimeofday(&tv
, 0);
506 TV_ADDL(&tv
, &tv
, 0, RND(f
->t
));
507 sel_addtimer(&sel
, &f
->tm
, &tv
, floodtimer
, f
);
510 static void doflood(peer
*p
, unsigned ac
, char **av
)
514 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
518 f
->type
= strtoul(av
[0], 0, 16);
533 /*----- Configuration commands --------------------------------------------*/
535 static void parse(char *p
);
537 static void addflood(unsigned ac
, char **av
) {
538 doflood(&peers
[0], ac
, av
);
539 doflood(&peers
[1], ac
, av
);
541 static void addlflood(unsigned ac
, char **av
) {
542 doflood(&peers
[0], ac
, av
);
544 static void addrflood(unsigned ac
, char **av
) {
545 doflood(&peers
[1], ac
, av
);
548 static void addfilter(unsigned ac
, char **av
) {
549 dofilter(&peers
[0], &peers
[1], ac
, av
);
550 dofilter(&peers
[1], &peers
[0], ac
, av
);
552 static void addlfilter(unsigned ac
, char **av
) {
553 dofilter(&peers
[0], &peers
[1], ac
, av
);
555 static void addrfilter(unsigned ac
, char **av
) {
556 dofilter(&peers
[1], &peers
[0], ac
, av
);
559 static void include(unsigned ac
, char **av
)
564 die(1, "syntax: include:FILE:...");
566 if ((fp
= fopen(*av
, "r")) == 0)
567 die(1, "fopen `%s': %s", *av
, strerror(errno
));
568 while (dstr_putline(&d
, fp
) != EOF
) {
577 const struct cmdtab
{
579 void (*func
)(unsigned /*ac*/, char **/
*av*/
);
582 { "include", include
},
583 { "filt", addfilter
},
584 { "lfilt", addlfilter
},
585 { "rfilt", addrfilter
},
586 { "next", nextfork
},
587 { "flood", addflood
},
588 { "lflood", addlflood
},
589 { "rflood", addrflood
},
595 static void parse(char *p
)
599 const struct cmdtab
*ct
;
607 } while (p
&& c
< AVMAX
- 1);
609 for (ct
= cmdtab
; ct
->name
; ct
++) {
610 if (strcmp(ct
->name
, v
[0]) == 0) {
611 ct
->func(c
- 1, v
+ 1);
615 die(1, "unknown command `%s'", v
[0]);
618 /*----- Main driver -------------------------------------------------------*/
620 static void version(FILE *fp
)
622 pquis(fp
, "$, TrIPE version " VERSION
"\n");
625 static void usage(FILE *fp
)
627 pquis(fp
, "Usage: $ [-k keyring] directive...\n");
630 static void help(FILE *fp
)
637 int main(int argc
, char *argv
[])
639 const char *kfname
= "keyring.pub";
648 static const struct option opt
[] = {
649 { "help", 0, 0, 'h' },
650 { "version", 0, 0, 'v' },
651 { "usage", 0, 0, 'u' },
652 { "keyring", OPTF_ARGREQ
, 0, 'k' },
655 if ((i
= mdwopt(argc
, argv
, "hvuk:", opt
, 0, 0, 0)) < 0)
679 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
680 rand_seed(RAND_GLOBAL
, 160);
681 rand_get(RAND_GLOBAL
, buf
, sizeof(buf
));
682 rng
= rc4_rand(buf
, sizeof(buf
));
684 if (key_open(&keys
, kfname
, KOPEN_READ
, key_moan
, 0))
685 die(1, "couldn't open `%s': %s", kfname
, strerror(errno
));
686 for (i
= optind
; i
< argc
; i
++)
689 die(1, "need two peers");
696 /*----- That's all, folks -------------------------------------------------*/