-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-security.c
366 lines (339 loc) · 10.5 KB
/
core-security.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <ftw.h>
#include "core-list.h"
#include "core-output.h"
#include "core-misc.h"
#include "core-security.h"
#include "core-environ.h"
#include "base64.h" // NibbleAndAHalf project
char *specialfiles[ ] = { ".bash_history",".mysql_history",".htpasswd",".rhosts", NULL };
int index_list;
void SEC_MainHandler() {
OutputHandler(OUTPUT_FORMAT,SECTION,"%s","Security Informations");
// - Hids and Nids Presence
SEC_AslrCheck(); // Stack Randomization state
SEC_SuidDump(); // Suid Dumpable State
SEC_MinAddr(); // Minimal Address Map Value
SEC_AsciiArmor(); // ASCII ARMOR is in place?
SEC_CheckPatch(); // Selinux, Apparmor, GrSec, Pax
SEC_NxCheck(); // Nx Flag Check
SEC_SshKeys(); // base64 hashed SSH Keys
SEC_FindFiles("/",0,NULL); // List all suid files
if(getpid()!=0 && getgid()!=0){ // Skip when root
SEC_FindFiles("/",3,NULL); // List all writable *
SEC_FindFiles("/",2,NULL); // List all writable *
}
SEC_InterestingFiles(); // List all interesting files
}
int SEC_AsciiArmor() {
static const char *query[ ] = { "libc", NULL };
char *end=NULL;
char string[MAXBLEN],Mapped[MIDBLEN];
int found;
FILE *fp;
fp=fopen(SELFMAPS,"r");
if (!fp)
return(-1);
for(found=0;;){
end = fgets(string,MAXBLEN,fp);
if (end == NULL) break;
if(strstr(string,query[0])!= NULL) {
snprintf(Mapped,MIDBLEN,"%s",MI_GetField(string,1,' '));
if(!strncmp(Mapped,"00",2))
found=1;
}
}
fclose(fp);
if(!found)
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","ASCII-Armor: Not Present");
else
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","ASCII-Armor: Present");
return 0;
}
int SEC_SshKeys() {
struct node *a;
int encoded_len,file_size,buf_size,read;
unsigned char *input;
char *encoded_data,*buf;
FILE *fd;
struct stat attr;
OutputHandler(OUTPUT_FORMAT,TITLE,"%s","Hashed SSH Keys");
LST_InitHeader();
index_list=0;
SEC_FindFiles("/",S_FILES,SSHURSAKEY); //SEC_FindFiles feeds the linked list
SEC_FindFiles("/",S_FILES,SSHUDSAKEY);
SEC_FindFiles("/",S_FILES,SSHAUTHKEY);
a=header->first;
while(a) {
stat(a->fullpath,&attr);
file_size=attr.st_size;
if(file_size<134217728) {
fd=fopen(a->fullpath,"r");
if(!fd)
break;
else {
input=(unsigned char *)malloc(file_size); //i dont like the idea of alloc large space (like 128mb) of memory... but base64 appears to not support incremental hashing,like openssl's CTX update sha1 schematics...
read=fread(input,1,file_size,fd);
if(read>0){
encoded_data=base64(input,file_size+1,&encoded_len);
buf_size=encoded_len+strlen(a->fullpath);
buf=(char *)malloc(buf_size);
memset(buf,0,buf_size);
snprintf(buf,buf_size,"%s:%s",a->fullpath,encoded_data);
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
free(input);
}
}
}
a=a->next;
}
LST_DestroyList();
return 0;
}
int SEC_NxCheck() {
static const char *query[ ] = { "flags","nx", NULL };
char *end=NULL,*token=NULL;
char linha[MAXBLEN];
int i,found,qtde,len;
FILE *fp;
fp = fopen(CPUFILE,"r");
if (!fp) return(errno);
for(;;){
end = fgets(linha,MAXBLEN,fp);
if (end == NULL) break;
if(strstr(linha,query[0])!= NULL) {
qtde=MI_CountTok(linha,' ');
for(i=2,found=0;i<=qtde;i++) {
len=strlen(MI_GetField(linha,i,' '))+1;
if(len>1) {
token=(char *)malloc(len);
memset(token,0,len);
snprintf(token,len,"%s",MI_GetField(linha,i,' '));
if(!strncmp(token,query[1],2))
found=1;
free(token);
}
}
}
}
fclose(fp);
if(!found)
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","NX Flag: Not Present");
else
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","NX Flag: Present");
return 0;
}
int SEC_FtwCallback(const char *name, const struct stat *status, int type) {
int stuid,stgid;
char *enty=(char *)name;
if ((type == FTW_D) && fquery.type==S_DIRW) {
if(!access(enty,W_OK)){
if(!strstr(enty,"/proc")) {
stuid=status->st_uid;
stgid=status->st_gid;
MI_Owner(stuid,stgid,enty);
}
}
}
if ((type == FTW_F) && fquery.type==S_SUID) {
if(!strstr(enty,"/dev") && !strstr(enty,"/proc") && !strstr(enty,"/sys")) {
if ((status->st_mode & S_ISUID)== S_ISUID) {
stuid=status->st_uid;
stgid=status->st_gid;
MI_Owner(stuid,stgid,enty);
} else return 0;
}
}
if ((type == FTW_F) && fquery.type==S_FILES) {
if(!strstr(enty,"/proc")) {
if (strstr(enty,fquery.pattern)){
stuid=status->st_uid;
stgid=status->st_gid;
++index_list;
LST_InsertDataList(enty,stuid,stgid,index_list); //joga na lista encadeada
//MI_Owner(stuid,stgid,enty); //<- essa funcao eh chamada junto com a funcao de mostrar item de posicao da lista (joga num for e referencia item da lista pela posicao, pega o campo e joga no MI_Owner()
}
}
}
if ((type == FTW_F) && fquery.type==S_FILW) {
if(!access(enty,W_OK)) {
if(!strstr(enty,"/dev") && !strstr(enty,"/proc")) {
stuid=status->st_uid;
stgid=status->st_gid;
MI_Owner(stuid,stgid,enty);
}
}
}
return 0;
}
int SEC_FindFiles(char *path,int mode,char *string) {
if(mode==S_SUID) { //suid find
fquery.type=S_SUID;
OutputHandler(OUTPUT_FORMAT,TITLE,"%s","List of suid files");
ftw(path,SEC_FtwCallback,0);
}
if(mode==S_FILES) { //search files
fquery.type=S_FILES;
fquery.pattern=string;
ftw(path,SEC_FtwCallback,0);
}
if(mode==S_DIRW) { //dir write perm search
fquery.type=S_DIRW;
OutputHandler(OUTPUT_FORMAT,TITLE,"%s","List of writable directories");
ftw(path,SEC_FtwCallback,1);
}
if(mode==S_FILW) { //file write perm search
fquery.type=S_FILW;
OutputHandler(OUTPUT_FORMAT,TITLE,"%s","List of writable files");
ftw(path,SEC_FtwCallback,0);
}
return 0;
}
int SEC_SuidDump() {
FILE *sd;
char svalue,*buf;
sd = fopen(SUIDDUMP,"r");
if (!sd) return(errno);
svalue = fgetc(sd);
switch(svalue) {
case '0': {
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"Suid dumpable: %c (default)",svalue);
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
} break;
case '1': {
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"Suid dumpable: %c (debug mode)",svalue);
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
} break;
case '2': {
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"Suid dumpable: %c (suidsafe mode)",svalue);
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
} break;
default: break;
}
fclose(sd);
return 0;
}
int SEC_MinAddr() {
FILE *ma;
char avalue[21],*buf;
ma = fopen(MINADDR,"r");
if (!ma) return (errno);
fgets(avalue,20,ma);
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"Minimal address map value: %d",atoi(avalue));
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
fclose(ma);
return 0;
}
int SEC_AslrCheck() {
FILE *rvs;
char avalue[21],*buf;
rvs = fopen(RANDOMIZEVA,"r");
if (!rvs) return(errno);
fgets(avalue,20,rvs);
switch (atoi(avalue)){
case 0: {
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"ASLR: %d (disabled)",atoi(avalue));
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
} break;;
case 1: {
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"ASLR: %d (Conservative mode)",atoi(avalue));
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
} break;
case 2: {
buf=(char *)malloc(MIDBLEN);
memset(buf,0,MIDBLEN);
snprintf(buf,MIDBLEN,"ASLR: %d (Full mode)",atoi(avalue));
OutputHandler(OUTPUT_FORMAT,ITEM,"%s",buf);
free(buf);
}break;
default: break;;
}
fclose(rvs);
return 0;
}
int SEC_Selinux() {
FILE *sd;
char svalue[MAXBLEN],*end;
int ret;
sd = fopen(SELINUX,"r");
if (!sd) ret=4;
for(ret=0;;){
end=fgets(svalue,MAXBLEN-1,sd);
if (end==NULL) break;
if(!MI_RegMatch(svalue,"SELINUX=disabled")) {
ret=3;
break;
}
if(!MI_RegMatch(svalue,"SELINUX=permissive")) {
ret=2;
break;
}
if(!MI_RegMatch(svalue,"SELINUX=enforcing")) {
ret=1;
break;
}
}
fclose(sd);
return ret;
}
int SEC_CheckPatch() {
if(!access(GRSECURITY, R_OK))
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","GrSecurty: Present");
else
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","GrSecurty: Not Present");
if(!access(APPARMOR, R_OK))
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","AppArmor: Present");
else
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","AppArmor: Not Present");
if(!access(PROCPAX, R_OK))
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","Pax: Present");
else
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","Pax: Not Present");
if(!access(SELINUX, R_OK)) {
switch(SEC_Selinux()) {
case 1: OutputHandler(OUTPUT_FORMAT,ITEM,"%s","Selinux: Present (Enforcing Mode)"); break;
case 2: OutputHandler(OUTPUT_FORMAT,ITEM,"%s","Selinux: Present (Permissive Mode)"); break;
case 3: OutputHandler(OUTPUT_FORMAT,ITEM,"%s","Selinux: Present (Disabled)"); break;
}
} else
OutputHandler(OUTPUT_FORMAT,ITEM,"%s","Selinux: Not Present");
return 0;
}
void SEC_InterestingFiles() {
int index;
OutputHandler(OUTPUT_FORMAT,TITLE,"%s","Relevant files found on filesystem");
LST_InitHeader();
for (index=0;specialfiles[index];index++)
SEC_FindFiles("/",S_FILES,specialfiles[index]);
for (index=0;index<=header->len;index++)
LST_ShowItem(index);
LST_DestroyList();
}