1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* Copyright (c) 2002, 2003, 2004 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Copyright (c) 1999, 2000 Dug Song <dugsong@monkey.org>
* All rights reserved, all wrongs reversed.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/param.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/queue.h>
#include <sys/tree.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <pcap.h>
#include <dumbnet.h>
#include <event.h>
#include "honeyd.h"
int
pcap_dloff(pcap_t *pd)
{
int offset = -1;
switch (pcap_datalink(pd)) {
case DLT_EN10MB:
offset = 14;
break;
case DLT_IEEE802:
offset = 22;
break;
case DLT_FDDI:
offset = 21;
break;
#ifdef DLT_PPP
case DLT_PPP:
offset = 24;
break;
#endif
#ifdef DLT_LINUX_SLL
case DLT_LINUX_SLL:
offset = 16;
break;
#endif
#ifdef DLT_LOOP
case DLT_LOOP:
#endif
case DLT_NULL:
offset = 4;
break;
default:
warnx("unsupported datalink type");
break;
}
return (offset);
}
char *
strrpl(char *str, size_t size, char *match, char *value)
{
char *p, *e;
int len, rlen;
p = str;
e = p + strlen(p);
len = strlen(match);
/* Try to match against the variable */
while ((p = strchr(p, match[0])) != NULL) {
if (!strncmp(p, match, len) && !isalnum(p[len]))
break;
p += len;
if (p >= e)
return (NULL);
}
if (p == NULL)
return (NULL);
rlen = strlen(value);
if (strlen(str) - len + rlen > size)
return (NULL);
memmove(p + rlen, p + len, strlen(p + len) + 1);
memcpy(p, value, rlen);
return (p);
}
/*
* Checks if <addr> is contained in the network specified by <net>
*/
int
addr_contained(struct addr *net, struct addr *addr)
{
struct addr tmp;
tmp = *net;
tmp.addr_bits = IP_ADDR_BITS;
if (addr_cmp(&tmp, addr) > 0)
return (0);
addr_bcast(net, &tmp);
tmp.addr_bits = IP_ADDR_BITS;
if (addr_cmp(&tmp, addr) < 0)
return (0);
return (1);
}
char *
strnsep(char **line, char *delim)
{
char *ret, *p;
if (line == NULL)
return (NULL);
ret = *line;
if (ret == NULL)
return (NULL);
p = strpbrk(ret, delim);
if (p == NULL) {
*line = NULL;
return (ret);
}
*line = p + strspn(p, delim);
*p = '\0';
return (ret);
}
#ifndef HAVE_FGETLN
char *
fgetln(FILE *stream, size_t *len)
{
static char buf[1024];
if (fgets(buf, sizeof(buf), stream) == NULL)
return (NULL);
*len = strlen(buf);
return (buf);
}
#endif
/* Either connect or bind */
int
make_socket_ai(int (*f)(int, const struct sockaddr *, socklen_t), int type,
struct addrinfo *ai)
{
struct linger linger;
int fd, on = 1;
/* Create listen socket */
fd = socket(AF_INET, type, 0);
if (fd == -1) {
warn("socket");
return (-1);
}
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
warn("fcntl(O_NONBLOCK)");
goto out;
}
if (fcntl(fd, F_SETFD, 1) == -1) {
warn("fcntl(F_SETFD)");
goto out;
}
if (type == SOCK_STREAM) {
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(void *)&on, sizeof(on));
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(void *) &on, sizeof(on));
#ifdef SO_REUSEPORT
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT,
(void *) &on, sizeof(on));
#endif
linger.l_onoff = 1;
linger.l_linger = 5;
setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
}
if ((f)(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
if (errno != EINPROGRESS) {
warn("%s", __func__);
goto out;
}
}
return (fd);
out:
close(fd);
return (-1);
}
int
make_socket(int (*f)(int, const struct sockaddr *, socklen_t), int type,
char *address, uint16_t port)
{
struct addrinfo ai, *aitop;
char strport[NI_MAXSERV];
int fd;
memset(&ai, 0, sizeof (ai));
ai.ai_family = AF_INET;
ai.ai_socktype = type;
ai.ai_flags = f != connect ? AI_PASSIVE : 0;
snprintf(strport, sizeof (strport), "%d", port);
if (getaddrinfo(address, strport, &ai, &aitop) != 0) {
warn("getaddrinfo");
return (-1);
}
fd = make_socket_ai(f, type, aitop);
freeaddrinfo(aitop);
return (fd);
}
#define DIFF(a,b) do { \
if ((a) < (b)) return -1; \
else if ((a) > (b)) return 1; \
} while (0)
int
conhdr_compare(struct tuple *a, struct tuple *b)
{
DIFF(a->ip_src, b->ip_src);
DIFF(a->ip_dst, b->ip_dst);
DIFF(a->sport, b->sport);
DIFF(a->dport, b->dport);
return (0);
}
char *
honeyd_contoa(const struct tuple *hdr)
{
static char buf[128];
char asrc[24], adst[24];
struct addr src, dst;
u_short sport, dport;
addr_pack(&src, ADDR_TYPE_IP, IP_ADDR_BITS, &hdr->ip_src, IP_ADDR_LEN);
addr_pack(&dst, ADDR_TYPE_IP, IP_ADDR_BITS, &hdr->ip_dst, IP_ADDR_LEN);
/* For a local connection switch the address around */
if (hdr->local) {
struct addr tmp;
tmp = src;
src = dst;
dst = tmp;
sport = hdr->dport;
dport = hdr->sport;
} else {
sport = hdr->sport;
dport = hdr->dport;
}
addr_ntop(&src, asrc, sizeof(asrc));
addr_ntop(&dst, adst, sizeof(adst));
snprintf(buf, sizeof(buf), "(%s:%d - %s:%d)",
asrc, sport, adst, dport);
return (buf);
}
|