[180] | 1 | /* SAMHAIN file system integrity testing */
|
---|
| 2 | /* Copyright (C) 2008 Rainer Wichmann */
|
---|
| 3 | /* */
|
---|
| 4 | /* This program is free software; you can redistribute it */
|
---|
| 5 | /* and/or modify */
|
---|
| 6 | /* it under the terms of the GNU General Public License as */
|
---|
| 7 | /* published by */
|
---|
| 8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
| 9 | /* (at your option) any later version. */
|
---|
| 10 | /* */
|
---|
| 11 | /* This program is distributed in the hope that it will be useful, */
|
---|
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
| 14 | /* GNU General Public License for more details. */
|
---|
| 15 | /* */
|
---|
| 16 | /* You should have received a copy of the GNU General Public License */
|
---|
| 17 | /* along with this program; if not, write to the Free Software */
|
---|
| 18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 19 |
|
---|
| 20 | #include "config_xor.h"
|
---|
| 21 |
|
---|
| 22 | #include <stdio.h>
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | #include <string.h>
|
---|
| 25 | #include <sys/types.h>
|
---|
| 26 | #include <unistd.h>
|
---|
| 27 |
|
---|
| 28 | #ifdef HAVE_DIRENT_H
|
---|
| 29 | #include <dirent.h>
|
---|
| 30 | #define NAMLEN(dirent) sl_strlen((dirent)->d_name)
|
---|
| 31 | #else
|
---|
| 32 | #define dirent direct
|
---|
| 33 | #define NAMLEN(dirent) (dirent)->d_namlen
|
---|
| 34 | #ifdef HAVE_SYS_NDIR_H
|
---|
| 35 | #include <sys/ndir.h>
|
---|
| 36 | #endif
|
---|
| 37 | #ifdef HAVE_SYS_DIR_H
|
---|
| 38 | #include <sys/dir.h>
|
---|
| 39 | #endif
|
---|
| 40 | #ifdef HAVE_NDIR_H
|
---|
| 41 | #include <ndir.h>
|
---|
| 42 | #endif
|
---|
| 43 | #endif
|
---|
| 44 | #define NEED_ADD_DIRENT
|
---|
| 45 |
|
---|
| 46 | #if defined(SH_USE_PORTCHECK) && (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
|
---|
| 47 |
|
---|
| 48 | #if defined(__linux__)
|
---|
| 49 |
|
---|
| 50 | #include "samhain.h"
|
---|
| 51 | #include "sh_error_min.h"
|
---|
| 52 | #include "sh_utils.h"
|
---|
| 53 | #include "sh_pthread.h"
|
---|
| 54 |
|
---|
| 55 | #define FIL__ _("sh_port2proc.c")
|
---|
| 56 |
|
---|
| 57 | static size_t sh_minpid = 0x0001;
|
---|
| 58 | static size_t sh_maxpid = 0x8000;
|
---|
| 59 |
|
---|
| 60 | #ifndef HAVE_LSTAT
|
---|
| 61 | #define lstat(x,y) stat(x,y)
|
---|
| 62 | #endif /* HAVE_LSTAT */
|
---|
| 63 |
|
---|
| 64 | #if defined(S_IFLNK) && !defined(S_ISLNK)
|
---|
| 65 | #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
|
---|
| 66 | #else
|
---|
| 67 | #if !defined(S_ISLNK)
|
---|
| 68 | #define S_ISLNK(mode) (0)
|
---|
| 69 | #endif
|
---|
| 70 | #endif
|
---|
| 71 |
|
---|
| 72 | struct sock_store {
|
---|
| 73 | unsigned long sock;
|
---|
| 74 | size_t pid;
|
---|
| 75 | char * path;
|
---|
| 76 | char * user;
|
---|
| 77 | struct sock_store * next;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | /* /proc:
|
---|
| 81 | * linux: /proc/pid/exe
|
---|
| 82 | * freebsd: /proc/pid/file
|
---|
| 83 | * solaris10: /proc/pid/path/a.out
|
---|
| 84 | */
|
---|
| 85 | static void get_user_and_path (struct sock_store * add)
|
---|
| 86 | {
|
---|
| 87 | extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
|
---|
| 88 |
|
---|
| 89 | char path[128];
|
---|
| 90 | char * buf;
|
---|
| 91 | struct stat sbuf;
|
---|
| 92 | int len;
|
---|
| 93 | char * tmp;
|
---|
| 94 |
|
---|
| 95 | sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) add->pid);
|
---|
| 96 |
|
---|
| 97 | if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
|
---|
| 98 | {
|
---|
| 99 | goto linkread;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) add->pid);
|
---|
| 103 |
|
---|
| 104 | if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
|
---|
| 105 | {
|
---|
| 106 | goto linkread;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) add->pid);
|
---|
| 110 |
|
---|
| 111 | if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
|
---|
| 112 | {
|
---|
| 113 | goto linkread;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | return;
|
---|
| 117 |
|
---|
| 118 | linkread:
|
---|
| 119 |
|
---|
| 120 | buf = SH_ALLOC(PATH_MAX);
|
---|
| 121 | len = readlink(path, buf, PATH_MAX); /* flawfinder: ignore */
|
---|
| 122 | len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
|
---|
| 123 |
|
---|
| 124 | if (len > 0)
|
---|
| 125 | {
|
---|
| 126 | buf[len] = '\0';
|
---|
| 127 | add->path = buf;
|
---|
| 128 | }
|
---|
| 129 | else
|
---|
| 130 | {
|
---|
| 131 | SH_FREE(buf);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | add->user = SH_ALLOC(USER_MAX);
|
---|
| 135 | tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, add->user, USER_MAX);
|
---|
| 136 |
|
---|
| 137 | if (!tmp)
|
---|
| 138 | sl_snprintf (add->user, USER_MAX, "%ld", (unsigned long) sbuf.st_uid);
|
---|
| 139 |
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | #if defined(__linux__)
|
---|
| 144 | #define PROC_PID_MAX _("/proc/sys/kernel/pid_max")
|
---|
| 145 |
|
---|
| 146 | static int proc_max_pid (size_t * procpid)
|
---|
| 147 | {
|
---|
| 148 | char * ret;
|
---|
| 149 | unsigned long pid;
|
---|
| 150 | FILE * fd;
|
---|
| 151 | char str[128];
|
---|
| 152 | char * ptr;
|
---|
| 153 |
|
---|
| 154 | SL_ENTER(_("proc_max_pid"));
|
---|
| 155 |
|
---|
| 156 | if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
|
---|
| 157 | {
|
---|
| 158 | if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
|
---|
| 159 | {
|
---|
| 160 | str[0] = '\0';
|
---|
| 161 | ret = fgets(str, 128, fd);
|
---|
| 162 | if (ret && *str != '\0')
|
---|
| 163 | {
|
---|
| 164 | pid = strtoul(str, &ptr, 0);
|
---|
| 165 | if (*ptr == '\0' || *ptr == '\n')
|
---|
| 166 | {
|
---|
| 167 | fclose(fd);
|
---|
| 168 | *procpid = (size_t) pid;
|
---|
| 169 | SL_RETURN(0, _("proc_max_pid"));
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | fclose(fd);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | SL_RETURN((-1), _("proc_max_pid"));
|
---|
| 176 | }
|
---|
| 177 | #else
|
---|
| 178 | static int proc_max_pid(size_t * procpid)
|
---|
| 179 | {
|
---|
| 180 | *procpid = sh_maxpid;
|
---|
| 181 | return 0;
|
---|
| 182 | }
|
---|
| 183 | #endif
|
---|
| 184 |
|
---|
| 185 | static struct sock_store * socklist = NULL;
|
---|
| 186 |
|
---|
| 187 | static void del_sock_all()
|
---|
| 188 | {
|
---|
| 189 | struct sock_store * del = socklist;
|
---|
| 190 |
|
---|
| 191 | while (del)
|
---|
| 192 | {
|
---|
| 193 | socklist = del->next;
|
---|
| 194 | if (del->path)
|
---|
| 195 | SH_FREE(del->path);
|
---|
| 196 | if (del->user)
|
---|
| 197 | SH_FREE(del->user);
|
---|
| 198 | SH_FREE(del);
|
---|
| 199 | del = socklist;
|
---|
| 200 | }
|
---|
| 201 | socklist = NULL;
|
---|
| 202 | return;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | static void add_sock(unsigned long sock, size_t pid)
|
---|
| 206 | {
|
---|
| 207 | struct sock_store * add = SH_ALLOC(sizeof(struct sock_store));
|
---|
| 208 |
|
---|
| 209 | add->sock = sock;
|
---|
| 210 | add->pid = pid;
|
---|
| 211 | add->path = NULL;
|
---|
| 212 | add->user = NULL;
|
---|
| 213 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 214 | get_user_and_path(add);
|
---|
| 215 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 216 | add->next = socklist;
|
---|
| 217 | socklist = add;
|
---|
| 218 | return;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | static void check_and_add_sock(char * fbuf, size_t pid)
|
---|
| 222 | {
|
---|
| 223 | if (0 == strncmp(_("socket:["), fbuf, 8))
|
---|
| 224 | {
|
---|
| 225 | char * end;
|
---|
| 226 | unsigned long sock;
|
---|
| 227 | size_t len = strlen(fbuf);
|
---|
| 228 | if (fbuf[len-1] == ']')
|
---|
| 229 | fbuf[len-1] = '\0';
|
---|
| 230 | sock = strtoul(&fbuf[8], &end, 0);
|
---|
| 231 | if (*end == '\0' && fbuf[8] != '\0')
|
---|
| 232 | {
|
---|
| 233 | add_sock(sock, pid);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | static void fetch_socks(size_t pid)
|
---|
| 239 | {
|
---|
| 240 | char path[128];
|
---|
| 241 | DIR * dir;
|
---|
| 242 | sl_snprintf(path, sizeof(path), _("/proc/%lu/fd"), (unsigned long) pid);
|
---|
| 243 |
|
---|
| 244 | dir = opendir(path);
|
---|
| 245 | if (dir)
|
---|
| 246 | {
|
---|
| 247 | struct dirent *entry;
|
---|
| 248 | while (NULL != (entry = readdir(dir)))
|
---|
| 249 | {
|
---|
| 250 | char fpath[384];
|
---|
| 251 | char fbuf[64];
|
---|
| 252 | int ret;
|
---|
| 253 | /* /proc/PID/fd/N-> socket:[15713] */
|
---|
| 254 | sl_snprintf(fpath, sizeof(fpath), _("%s/%s"), path, entry->d_name);
|
---|
| 255 | ret = readlink(fpath, fbuf, sizeof(fbuf)-1); /* flawfinder: ignore */
|
---|
| 256 | if (ret > 0)
|
---|
| 257 | {
|
---|
| 258 | fbuf[ret] = '\0';
|
---|
| 259 | check_and_add_sock(fbuf, pid);
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | closedir(dir);
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | int sh_port2proc_prepare()
|
---|
| 267 | {
|
---|
| 268 | size_t i;
|
---|
| 269 |
|
---|
| 270 | if (0 != proc_max_pid(&sh_maxpid))
|
---|
| 271 | {
|
---|
| 272 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 273 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 274 | _("Failed to detect max_pid"),
|
---|
| 275 | _("sh_port2proc"));
|
---|
| 276 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 277 | SL_RETURN ((-1), _("sh_port2proc"));
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /* Delete old socket list and re-create it
|
---|
| 281 | */
|
---|
| 282 | del_sock_all();
|
---|
| 283 |
|
---|
| 284 | for (i = sh_minpid; i < sh_maxpid; ++i)
|
---|
| 285 | {
|
---|
| 286 | fetch_socks(i);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | return 0;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | #include <sys/socket.h>
|
---|
| 293 | #include <netinet/in.h>
|
---|
| 294 | #include <arpa/inet.h>
|
---|
| 295 |
|
---|
| 296 | char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
|
---|
| 297 | char * user, size_t userlen)
|
---|
| 298 | {
|
---|
| 299 | FILE * fd;
|
---|
| 300 |
|
---|
| 301 | if (proto == IPPROTO_TCP)
|
---|
| 302 | fd = fopen("/proc/net/tcp", "r");
|
---|
| 303 | else
|
---|
| 304 | fd = fopen("/proc/net/udp", "r");
|
---|
| 305 |
|
---|
| 306 | if (fd)
|
---|
| 307 | {
|
---|
| 308 | int n, iface, port, inode;
|
---|
| 309 | char line[512];
|
---|
| 310 |
|
---|
| 311 | while (NULL != fgets(line, sizeof(line), fd))
|
---|
| 312 | {
|
---|
| 313 |
|
---|
| 314 | if (4 == sscanf(line,
|
---|
| 315 | "%d: %X:%X %*X:%*X %*X %*X:%*X %*X:%*X %*X %*d %*d %d %*s",
|
---|
| 316 | &n, &iface, &port, &inode))
|
---|
| 317 | {
|
---|
| 318 | struct in_addr haddr;
|
---|
| 319 | haddr.s_addr = (unsigned long)iface;
|
---|
| 320 |
|
---|
| 321 | if (haddr.s_addr == saddr->s_addr && port == sport)
|
---|
| 322 | {
|
---|
| 323 | struct sock_store * new = socklist;
|
---|
| 324 |
|
---|
| 325 | while (new)
|
---|
| 326 | {
|
---|
| 327 | if ((unsigned int)inode == new->sock)
|
---|
| 328 | {
|
---|
| 329 | fclose(fd);
|
---|
| 330 | if (new->path)
|
---|
| 331 | {
|
---|
| 332 | if (new->user)
|
---|
| 333 | sl_strlcpy(user, new->user, userlen);
|
---|
| 334 | else
|
---|
| 335 | sl_strlcpy(user, "-", userlen);
|
---|
| 336 | return sh_util_strdup(new->path);
|
---|
| 337 | }
|
---|
| 338 | goto err_out;
|
---|
| 339 | }
|
---|
| 340 | new = new->next;
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | fclose(fd);
|
---|
| 346 | }
|
---|
| 347 | err_out:
|
---|
| 348 | sl_strlcpy(user, "0", userlen);
|
---|
| 349 | return sh_util_strdup("-");
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | #else /* !defined(__linux__) */
|
---|
| 353 |
|
---|
| 354 | char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
|
---|
| 355 | char * user, size_t userlen)
|
---|
| 356 | {
|
---|
| 357 | (void) proto;
|
---|
| 358 | (void) saddr;
|
---|
| 359 | (void) sport;
|
---|
| 360 |
|
---|
| 361 | sl_strlcpy(user, "-", userlen);
|
---|
| 362 | return sh_util_strdup("-");
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | int sh_port2proc_prepare()
|
---|
| 366 | {
|
---|
| 367 | return 0;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | #endif
|
---|
| 371 |
|
---|
| 372 | #endif /* defined(SH_USE_PORTCHECK) */
|
---|