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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
============================================================================
Name : xxx.c
Author : [email protected]
Version : 2016-07-30 v0.01
Copyright : All right reserved by Institute of Engineer,Chinese Academic of Science 2014~2018
Description : ip_shift_dns_service in C++, Ansi-style
V0.0.2 2014-11-27
============================================================================
*/
#include <time.h>
//#include <MESA/magellan_logger.h>
#include "dns_jt_audit.h"
#include "inc/dns_analyse.h"
int default_dbg_level = 1;
#define DBG(level, msg...) do {\
if(default_dbg_level > level) \
printf(msg); \
}while(0)
#define IP_REQ_NAME "JT_IP_PKT"
int JT_DNS_PLUG_VERSION_20170724 = 0;
char * dj_dns_service_conf_path = (char *)"./djconf/dns_jt_audit.conf";
const char *dns_service_module_name = "DJ_DNS_SERVICE";
//const char *register_gq_dns_table_name[REGISTER_TABLE_NUM] = {"DNS_DOMAIN_PZ", "IP_GEO_PZ"};
const char *register_gq_dns_table_name[REGISTER_TABLE_NUM] = {"DNS_DOMAIN_PZ"};
/*char *opt_log_name[11] = { \
// (char *)"opt_sip", (char *)"opt_dip", (char *)"opt_sport", (char*)"opt_dport", (char*)"opt_smac", (char *)"opt_dmac", \
// (char *)"opt_dns_domain",(char *)"opt_dns_responseip",(char *)"opt_alarm_id", (char *)"opt_alarm_risk", (char*)"opt_rule_id"};
*/
dns_service_conf g_dns_service;
int dns_project_req_id = 0;
#ifndef atomic_read
#define atomic_read(x) __sync_add_and_fetch((x),0)
#endif
#ifndef atomic_add
#define atomic_add(x,y) __sync_add_and_fetch((x),(y))
#endif
#ifndef atomic_set
#define atomic_set(x,y) __sync_lock_test_and_set((x),y)
#endif
unsigned short dns_seq = 0;
int dj_dns_service_add_cache(char* opt_name, const void* opt_value, int opt_len,dj_cache_t cache_handle,APPD_MSG_TYPE status)
{
int ret = 0;
opt_unit_t dns_service_opt = create_opt_unit(opt_name,opt_value,opt_len);
ret = add_dj_cache(cache_handle,status,dns_service_opt);
if(ret == -1)
{
printf("error\n");
MESA_handle_runtime_log(g_dns_service.log_handle, RLOG_LV_FATAL,dns_service_module_name,"Add cache failed!");
return ret;
}
return 0;
}
#if 0
unsigned long long get_uniq_id()
{
static unsigned short sequence=0;
static time_t last_second=0;
time_t now;
time(&now);
// time_t now=g_CurrentTime;
unsigned long long ADDR_OVER_ID=0;
short dns = 2;
if(last_second!=now)
{
last_second=now;
sequence=0;
}
//suppose maximum 65535 logs per second
assert(sequence<0xffff);
//16bit alarm type +32bit time + 16bit sequence
ADDR_OVER_ID=(dns&0x00000000000000FF)<<48|now<<32|sequence;
sequence++;
return ADDR_OVER_ID;
}
#endif
void dns_time_to_string(char *time_str, unsigned int time_str_len)
{
time_t t;
time(&t);
dj_thread_safe_ctime(&t, time_str, time_str_len);
}
int dns_set_magellan_opt(magellan_opt_t*m_opt,int opt_type,int opt_len,const void* opt_value)
{
m_opt->opt_type=opt_type;
m_opt->opt_len=opt_len;
m_opt->opt_value=(void *)opt_value;
return 1;
}
int dj_dns_create_log(dns_response_t* dns_info,struct streaminfo *dns_stream, int thread_num, Maat_rule_t *presult,magellan_opt_t *m_opt,char un_id[])
{
scan_status_t mid = NULL;
char resip[700];
int m_opt_cnt = 0;
if(presult->do_blacklist != DOMAIN_BLACK_CHAR )
return -1;
m_opt_cnt = 2;
m_opt[0].opt_type = MAGELLAN_OPT_DNS;
m_opt[1].opt_type = MAGELLAN_OPT_DOMAIN_IP;
memcpy(resip, m_opt[1].opt_value, m_opt[1].opt_len);
m_opt[1].opt_value = resip;
time_t captime = time(NULL);
//m_opt_cnt+=dns_set_magellan_opt(m_opt+2,MAGELLAN_OPT_CAP_TIME, sizeof(time_t),&captime);
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_CAP_TIME, sizeof(time_t),&captime);
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_ID,strlen(un_id),un_id);
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_RISK,sizeof(char),&(presult->do_log));
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_RULE_ID,sizeof(int),&(presult->config_id));
dj_magellan_write_log(dns_stream,QDJCQ_MGLL_USERID,TABID_DOMAIN_LISTEN_ALARM,m_opt,m_opt_cnt,thread_num);//TABID_DOMAIN_LISTEN_ALARM
Maat_clean_status(&mid);
return 0;
}
int dj_dns_scan_domain(dns_response_t* dns_info,struct streaminfo *dns_stream, void *a_packet,magellan_opt_t *m_opt,int thread_num)
{
int ret = 0;
int domain_len = 0;
char *domain = NULL;
// unsigned long long uniq_id;
time_t now;
short seq = 0;
int found_pos = 0;
Maat_rule_t domain_scan_result[MAX_SUPPORT_RULE_SIZE];
scan_status_t mid = NULL;
//char dot_domain[256];
memset(domain_scan_result,0,MAX_SUPPORT_RULE_SIZE*sizeof(Maat_rule_t));
domain = (char*)(dns_info->question.qname);
domain_len = strlen(domain);
/* memset(dot_domain, 0, 256);
snprintf(dot_domain, domain_len+2, ".%s", domain);*/
ret = Maat_full_scan_string(g_dj_feather,g_dns_service.table_id[REGISTER_TABLE_DOMAIN],CHARSET_GBK,\
domain,domain_len,domain_scan_result,&found_pos,MAX_SUPPORT_RULE_SIZE,&mid,thread_num);
if(ret == -1)
{
if(g_dns_service.debug_info == DJ_DEBUG_ON)
DBG(0, "Maat scan domain failed!\n");
MESA_handle_runtime_log(g_dns_service.log_handle,RLOG_LV_FATAL,dns_service_module_name,(char *)"Maat scan domain failed!\n");
Maat_clean_status(&mid);
return ret;
}
else if(ret > 0)
{
int i, index=0, black_flag=0;
int risk = 0;
for(i=0; i<ret; i++)
{
if(domain_scan_result[i].action == DOMAIN_BLACK_CHAR)
{
if(domain_scan_result[i].do_log >= risk)
{
risk = domain_scan_result[i].do_log;
index = i;
}
black_flag = 1;
}
}
//add to dynamic black list
if(black_flag == 1)
{
// make_ip_control_wblist(dns_stream, thread_num, domain_scan_result[index].config_id,risk,IPC_BLACK_IP_TYPE);
target_tag *dns_tag = (target_tag *)dictator_malloc(thread_num, sizeof(target_tag));
memset(dns_tag, 0, sizeof(target_tag));
dns_tag->type = domain_scan_result[index].service_id;
dns_tag->rule_id = domain_scan_result[index].config_id;
dns_tag->risk = risk;
// uniq_id = get_uniq_id();
// sprintf(dns_tag->id,"%lld",uniq_id);
atomic_add(&dns_seq,1);
seq = atomic_read(&dns_seq);
now = time(NULL);
sprintf(dns_tag->id,"%u%lu%u",DNS_BUSINESS,now,seq);
black_ip *dns_black_ip = NULL;
if(black_flag == 1) {
if (dns_info->ipv4_num > 0) {
dns_black_ip = (black_ip *) dictator_malloc(thread_num, sizeof(black_ip));
memset(dns_black_ip,0,sizeof(black_ip));
dns_black_ip->ip_num = dns_info->ipv4_num;
memcpy(dns_black_ip->ipv4, dns_info->ipv4, dns_info->ipv4_num * sizeof(unsigned int));
dns_black_ip->thread_num = thread_num;
dns_black_ip->ip_type = IPC_BLACK_IP_TYPE;
dns_tag->dns_ip = dns_black_ip;
}
}
int tag_ret = project_req_add_struct(dns_stream,dns_project_req_id,dns_tag);
if(tag_ret < 0)
{
if(g_dns_service.debug_info == DJ_DEBUG_ON)
DBG(0, "Add flow tag fail!\n");
MESA_handle_runtime_log(g_dns_service.log_handle,RLOG_LV_FATAL,dns_service_module_name,(char *)"Add flow tag fail!\n");
}
//if(black_flag == 1) {
dj_dns_create_log(dns_info,dns_stream, thread_num, &domain_scan_result[index],m_opt,dns_tag->id);
}
// printf("\n\ndomain hitted---%d, ser_def=%s\n\n", domain_scan_result[index].do_blacklist, domain_scan_result[index].service_defined);
}
//dj_dns_create_log(dns_info,dns_stream, thread_num, &domain_scan_result[0]);
Maat_clean_status(&mid);
return ret;
}
void dns_free_magellan_opt(magellan_opt_t *m_opt, int thread_seq)
{
if(m_opt->opt_value != NULL)
{
dictator_free(thread_seq, (void *)m_opt->opt_value);
m_opt->opt_value = NULL;
}
}
int dns_init_process_context(process_context_t **process_context, int thread_seq)
{
process_context_t *context = (process_context_t *)dictator_malloc(thread_seq, sizeof(process_context_t));
memset(context, 0 , sizeof(process_context_t));
*process_context = context;
return 1;
}
void dns_destroy_process_context(process_context_t *process_context, int thread_seq)
{
int i = 0;
if(process_context->m_opt_index != 0)
{
for(i = 0; i < process_context->m_opt_index; i++)
{
dns_free_magellan_opt(&(process_context->m_opt[i]), thread_seq);
}
}
dictator_free(thread_seq,process_context);
process_context = NULL;
}
char dj_dns_service_entry (stSessionInfo *session_info, void **pme, int thread_seq, struct streaminfo *dns_stream, void *a_packet)
{
int i,len,ret = 0;
int domain_len = 0;
int m_opt_cnt = 0;
magellan_opt_t m_opt[MAX_MAGELLON_OPT_NUM];
char respip[700];
char ipaddr[60];
const char *module_name = "JT_DNS_PLUG";
process_context_t *context = (process_context_t *)*pme;
struct Maat_rule_t scan_result[MAX_RESULT_NUM];
int found_pos = 0;
scan_status_t scan_mid = NULL;
if(*pme == NULL)
{
if((dns_init_process_context(&context, thread_seq)) < 0)
{
MESA_handle_runtime_log(g_dns_service.log_handle, RLOG_LV_FATAL, module_name, "<%s>%d: init_process_context failed ...", __FILE__, __LINE__);
return PROT_STATE_DROPME;
}
*pme = context;
}
if(NULL == session_info)
{
dns_destroy_process_context(context,thread_seq);
return PROT_STATE_DROPME;
}
dns_response_t* dns_info = (dns_response_t *)session_info->app_info;
domain_len = strlen((char*)(dns_info->question.qname));
if(domain_len <= 0)
{
dns_destroy_process_context(context,thread_seq);
return PROT_STATE_DROPME;
}
context->hit_config_num = Maat_full_scan_string(g_dj_feather,
g_dns_service.table_id[REGISTER_TABLE_DOMAIN],
CHARSET_GBK,
(const char *)(dns_info->question.qname),
strlen((char*)(dns_info->question.qname)),
//sizeof(dns_info->question.qname),
scan_result,
&found_pos,
MAX_RESULT_NUM,
&scan_mid,
thread_seq
);
Maat_clean_status(&scan_mid);
//如果匹配错误
if(context->hit_config_num < 0)
{
MESA_handle_runtime_log(g_dns_service.log_handle,RLOG_LV_FATAL,dns_service_module_name,(char *)"Error in Maat_full_scan_string ");;
}
// 默认全是白名单
int flag_whitelist = 0;
for (i = 0; i < context->hit_config_num; i++)
{
//黑名单是优先
if(scan_result[i].action == 1)
{
flag_whitelist = 1;
}
}
//0是白名单,1是存在黑名单
//如果全是白名单,且有命中
if(flag_whitelist == 0 && context->hit_config_num > 0)
{
MESA_handle_runtime_log(g_dns_service.log_handle,RLOG_LV_FATAL,dns_service_module_name,"<%s> filter 。。。",(const char *)(dns_info->question.qname));
dns_destroy_process_context(context,thread_seq);
return PROT_STATE_DROPME;
}
else
//if(flag_whitelist == 1 || context->hit_config_num == 0)
//如果不是白名单
{
memset(respip, 0, 700);
len = 0;
for(i=0; i<dns_info->ipv4_num; i++)
{
context->respone_flag = 1;
memset(ipaddr,0,60);
inet_ntop(AF_INET, (const void *)(&(dns_info->ipv4[i])),ipaddr,60);
memcpy(respip + len, ipaddr, strlen(ipaddr));
len += strlen(ipaddr);
respip[len++] = ';';
}
char cur_time_str[20] = {0};
dns_time_to_string(cur_time_str, sizeof(cur_time_str));
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_REQUEST,domain_len,dns_info->question.qname);
if(context->respone_flag == 1)
{
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_RESPONSE,len,respip);
}
else
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_RESPONSE,4,"NULL");
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_TIME,sizeof(cur_time_str),cur_time_str);
if(dns_stream->type == STREAM_TYPE_TCP)
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_PROTOCOL,3,"TCP");
else
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_PROTOCOL,3,"UDP");
m_opt_cnt+=dns_set_magellan_opt(m_opt+m_opt_cnt,MAGELLAN_OPT_APP,3,"dns");
if(g_dns_service.audit_level == 0)
dj_magellan_write_log(dns_stream,QDJCQ_MGLL_USERID,TABID_DNS_AUDIT_LOG,m_opt,m_opt_cnt,thread_seq);
//scan domain
ret = dj_dns_scan_domain(dns_info,dns_stream, a_packet,m_opt,thread_seq);
}
if(ret <= 0)
{
dns_destroy_process_context(context,thread_seq);
return PROT_STATE_DROPME;
}
return PROT_STATE_GIVEME;
}
int register_table(void)
{
int i = 0;
for(i = 0; i < REGISTER_TABLE_NUM; i++)
{
g_dns_service.table_id[i] = Maat_table_register(g_dj_feather, register_gq_dns_table_name[i]);
if(g_dns_service.table_id[i] == -1)
{
MESA_handle_runtime_log(g_dns_service.log_handle, RLOG_LV_FATAL, dns_service_module_name, "<%s>%d: Maat_table_register failed ...", __FILE__, __LINE__);
return -1;
}
}
return 0;
}
void free_project_req(int thread_seq, void *project_req_value)
{
if(project_req_value != NULL) {
target_tag *tmp = (target_tag *) project_req_value;
if(tmp->dns_ip != NULL)
{
dictator_free(thread_seq,tmp->dns_ip);
tmp->dns_ip = NULL;
}
dictator_free(thread_seq,tmp);
tmp = NULL;
}
}
int dj_dns_service_init()
{
MESA_load_profile_int_def(dj_dns_service_conf_path,(char *)"SYSTEM",(char *)"DEBUG_INFO",&g_dns_service.debug_info,0);
MESA_load_profile_int_def(dj_dns_service_conf_path,(char *)"SYSTEM",(char *)"LOG_LEVEL",&g_dns_service.log_level,0);
MESA_load_profile_int_def(dj_dns_service_conf_path,(char *)"SYSTEM",(char *)"AUDIT_LEVEL",&g_dns_service.audit_level,0);
MESA_load_profile_string_def(dj_dns_service_conf_path,(char *)"SYSTEM",(char *)"LOG_PATH",g_dns_service.log_path,MAX_LOG_FILE_PATH_LENGTH,NULL);
g_dns_service.log_handle = MESA_create_runtime_log_handle(g_dns_service.log_path,g_dns_service.log_level);
if(NULL == g_dns_service.log_handle)
{
DBG(0, "dj_dns_service_init---MESA_create_runtime_log_handle failed.\n");
}
if((register_table()) == -1)
return -1;
dns_project_req_id = project_producer_register(IP_REQ_NAME,PROJECT_VAL_TYPE_STRUCT,free_project_req);
if(g_dns_service.debug_info == DJ_DEBUG_ON)
{
DBG(0, "Hello! This is dj_dns_service.so\n");
}
return 0;
}
void dj_dns_service_destory()
{
if(g_dns_service.debug_info == DJ_DEBUG_ON)
{
DBG(0, "Hello! This is dj_dns_service.so service_destory, but i am not complete!!!\n");
}
MESA_destroy_runtime_log_handle(g_dns_service.log_handle);
}
|