-
Notifications
You must be signed in to change notification settings - Fork 30
/
hackmon.c
165 lines (141 loc) · 3.92 KB
/
hackmon.c
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
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <daniel@roe.ch>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
/*
* Monitoring core for specific techniques.
*/
#include "hackmon.h"
#include "work.h"
#include "atomic.h"
#include <strings.h>
#include <assert.h>
static config_t *config;
static uint64_t events_recvd; /* number of events received */
static uint64_t events_procd; /* number of events processed */
static atomic64_t ooms; /* counts events impaired due to OOM */
setstr_t *suppress_process_access_by_subject_ident;
setstr_t *suppress_process_access_by_subject_path;
static void process_access_free(process_access_t *);
static int process_access_work(process_access_t *);
static process_access_t *
process_access_new() {
process_access_t *pa;
pa = malloc(sizeof(*pa));
if (!pa)
return NULL;
bzero(pa, sizeof(*pa));
pa->hdr.code = LOGEVT_PROCESS_ACCESS;
pa->hdr.le_work = (__typeof__(pa->hdr.le_work))process_access_work;
pa->hdr.le_free = (__typeof__(pa->hdr.le_free))process_access_free;
return pa;
}
static void
process_access_free(process_access_t *pa) {
if (pa->subject_image_exec)
image_exec_free(pa->subject_image_exec);
if (pa->object_image_exec)
image_exec_free(pa->object_image_exec);
free(pa);
}
/*
* Executed by worker thread.
*
* Returns -1 if this work item should not be logged, 0 otherwise.
*/
static int
process_access_work(process_access_t *pa) {
if (pa->subject_image_exec && image_exec_match_suppressions(
pa->subject_image_exec,
suppress_process_access_by_subject_ident,
suppress_process_access_by_subject_path))
return -1;
return 0;
}
static void
log_event_process_access(struct timespec *tv,
audit_proc_t *subject,
audit_proc_t *object, /* may be NULL */
pid_t objectpid,
const char *method) {
process_access_t *pa;
pa = process_access_new();
if (!pa) {
atomic64_inc(&ooms);
return;
}
pa->subject_image_exec = image_exec_by_pid(subject->pid, tv);
pa->object_image_exec = image_exec_by_pid(objectpid, tv);
pa->subject = *subject;
if (object) {
pa->object = *object;
} else {
pa->objectpid = objectpid;
}
pa->method = method;
pa->hdr.tv = *tv;
work_submit(pa);
}
static void
hackmon_process_access(struct timespec *tv,
audit_proc_t *subject,
audit_proc_t *object, /* may be NULL */
pid_t objectpid,
const char *method) {
events_recvd++;
if (objectpid <= 0)
return;
if (subject->pid == objectpid)
return;
events_procd++;
log_event_process_access(tv, subject, object, objectpid, method);
}
/*
* Called for task_for_pid invocations.
*/
void
hackmon_taskforpid(struct timespec *tv,
audit_proc_t *subject,
audit_proc_t *object, /* may be NULL */
pid_t objectpid) {
hackmon_process_access(tv, subject, object, objectpid, "task_for_pid");
}
/*
* Called for ptrace invocations.
*/
void
hackmon_ptrace(struct timespec *tv,
audit_proc_t *subject,
audit_proc_t *object, /* may be NULL */
pid_t objectpid) {
hackmon_process_access(tv, subject, object, objectpid, "ptrace");
}
void
hackmon_init(config_t *cfg) {
config = cfg;
ooms = 0;
events_recvd = 0;
events_procd = 0;
suppress_process_access_by_subject_ident =
&cfg->suppress_process_access_by_subject_ident;
suppress_process_access_by_subject_path =
&cfg->suppress_process_access_by_subject_path;
}
void
hackmon_fini(void) {
if (!config)
return;
config = NULL;
}
void
hackmon_stats(hackmon_stat_t *st) {
assert(st);
st->recvd = events_recvd;
st->procd = events_procd;
st->ooms = (uint64_t)ooms;
}