DNS的deny动作交由tsg_master统一处理

This commit is contained in:
liuxueli
2021-08-09 09:43:45 +08:00
parent fdeb3ee864
commit 16b62ee411
5 changed files with 12 additions and 619 deletions

View File

@@ -2,9 +2,8 @@ cmake_minimum_required(VERSION 2.8)
add_definitions(-fPIC)
set(SRC fw_dns_plug.cpp fw_dns_rule.cpp)
set(SRC fw_dns_plug.cpp)
include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(/opt/MESA/include/)
include_directories(/opt/MESA/include/tsg/)

View File

@@ -11,7 +11,6 @@
#include "tsg_rule.h"
#include "tsg_label.h"
#include "tsg_send_log.h"
#include "fw_dns_plug.h"
#include "tsg_statistic.h"
#ifdef __cplusplus
@@ -35,275 +34,20 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
}
#endif
#ifndef PRINTADDR
#define PRINTADDR(a, b) ((b)<RLOG_LV_FATAL ? printaddr(&(a->addr), a->threadnum) : "")
#endif
#define MAX_TABLE_NAME_LEN 256
struct fw_dns_plug
{
int level;
int table_qname_id;
char table_qname[MAX_TABLE_NAME_LEN];
char log_path[MAX_TABLE_NAME_LEN*2];
void *logger;
};
char FW_DNS_PLUG_VERSION_20210607=0;
struct fw_dns_plug g_fw_dns_plug_info;
char *g_fw_dns_conffile=(char *)"tsgconf/main.conf";
static int get_answer_ttl(cJSON *object)
{
int min=0;
int max=0;
cJSON *item=NULL;
if(object==NULL)
{
return 0;
}
item=cJSON_GetObjectItem(object, "min");
min=item->valueint;
item=cJSON_GetObjectItem(object, "max");
max=item->valueint;
return (rand()%(max-min+1)+min);
}
static cJSON * get_answer_records(cJSON *object, int qtype)
{
int i=0;
cJSON *item=NULL;
int resolution_size=0;
int answer_type=DNS_TYPE_UNKNOWN;
cJSON *resolution_array=cJSON_GetObjectItem(object, "resolution");
resolution_size=cJSON_GetArraySize(resolution_array);
for(i=0; i<resolution_size; i++)
{
item=cJSON_GetArrayItem(resolution_array, i);
cJSON *tmp=cJSON_GetObjectItem(item, "qtype");
answer_type=fw_dns_type2index(tmp->valuestring);
if(answer_type==qtype)
{
return cJSON_GetObjectItem(item, "answer");
}
}
return NULL;
}
static int build_answer_records(struct streaminfo *a_stream, dns_info_t *dns_info, cJSON *object, cheat_pkt_opt_t *cheat_opt, int cheat_opt_num)
{
int ttl=0;
int record_id=0;
int i=0,used_num=0;
int answer_size=0;
cJSON *one_record=NULL;
cJSON *a_item=NULL;
cJSON *answer_array=NULL;
int answer_type=DNS_TYPE_UNKNOWN;
answer_array=get_answer_records(object, dns_info->query_question.qtype);
if(answer_array!=NULL)
{
memset(cheat_opt, 0, sizeof(cheat_pkt_opt_t)*cheat_opt_num);
answer_size=cJSON_GetArraySize(answer_array);
for(i=0; i<answer_size; i++)
{
one_record=cJSON_GetArrayItem(answer_array, i);
a_item=cJSON_GetObjectItem(one_record, "atype");
if(a_item==NULL)
{
continue;
}
answer_type=fw_dns_type2index(a_item->valuestring);
a_item=cJSON_GetObjectItem(one_record, "ttl");
ttl=get_answer_ttl(a_item);
a_item=cJSON_GetObjectItem(one_record, "value");
if(a_item!=NULL)
{
cheat_opt[used_num].res_type=answer_type;
cheat_opt[used_num].cfg_type=answer_type;
cheat_opt[used_num].ttl=ttl;
switch(answer_type)
{
case DNS_TYPE_A:
cheat_opt[used_num].res_len=sizeof(unsigned int);
inet_pton(AF_INET, a_item->valuestring, (void *)cheat_opt[used_num].res_info);
break;
case DNS_TYPE_AAAA:
cheat_opt[used_num].res_len=IPV6_ADDR_LEN;
inet_pton(AF_INET6, a_item->valuestring, (void *)cheat_opt[used_num].res_info);
break;
case DNS_TYPE_NS:
case DNS_TYPE_TXT:
case DNS_TYPE_PTR:
case DNS_TYPE_CNAME:
cheat_opt[used_num].res_len=(strlen(a_item->valuestring) > sizeof(cheat_opt[used_num].res_info)-1) ? sizeof(cheat_opt[used_num].res_info)-1 : strlen(a_item->valuestring);
memcpy(cheat_opt[used_num].res_info, a_item->valuestring, cheat_opt[used_num].res_len);
break;
default:
continue;
}
used_num++;
}
else
{
a_item=cJSON_GetObjectItem(one_record, "record_id");
record_id=a_item->valueint;
a_item=cJSON_GetObjectItem(one_record, "selected_num");
used_num+=dns_get_cheat_opt(record_id, a_item->valueint, ttl, answer_type, cheat_opt+used_num, cheat_opt_num-used_num);
}
}
}
return used_num;
}
static char fw_dns_action(struct streaminfo *a_stream, dns_info_t *dns_info, Maat_rule_t *p_result, const void *a_packet)
{
int ret=0;
int payload_len=0;
int answer_records_num=0;
dns_hdr_t *dns_hdr = NULL;
cJSON *item=NULL;
cJSON *object=NULL;
char *tmp_buff=NULL;
unsigned char senddir=0;
char state=PROT_STATE_GIVEME;
int method_type=TSG_METHOD_TYPE_UNKNOWN;
cheat_pkt_opt_t cheat_opt[MAX_ANSWER_RECORDS_NUM];
unsigned char cheat_pkt_payload[MAX_CHEAT_PKT_PAYLOAD_LEN];
tmp_buff=(char *)calloc(1, p_result->serv_def_len+1);
Maat_read_rule(g_tsg_maat_feather, p_result, MAAT_RULE_SERV_DEFINE, tmp_buff, p_result->serv_def_len);
object=cJSON_Parse(tmp_buff);
if(object==NULL)
{
MESA_handle_runtime_log(g_fw_dns_plug_info.logger,
RLOG_LV_FATAL,
"DO_ACTION",
"Hit policy_id: %d service: %d action: %d user_reagion: %s domain: %s qtype: %d addr: %s",
p_result->config_id,
p_result->service_id,
p_result->action,
(tmp_buff==NULL) ? p_result->service_defined : tmp_buff,
(char *)dns_info->query_question.qname,
dns_info->query_question.qtype,
PRINTADDR(a_stream, g_fw_dns_plug_info.level)
);
method_type=TSG_METHOD_TYPE_DROP;
}
else
{
item=cJSON_GetObjectItem(object, "method");
if(item!=NULL)
{
method_type=tsg_get_method_id(item->valuestring);
}
}
switch(method_type)
{
case TSG_METHOD_TYPE_DROP:
state=PROT_STATE_GIVEME|PROT_STATE_DROPPKT;
break;
case TSG_METHOD_TYPE_REDIRECTION:
if(g_fw_dns_plug_info.mode==0 && dns_info->hdr_info.qr==1) //mirror
{
break;
}
if(g_fw_dns_plug_info.mode==1 && dns_info->hdr_info.qr==0) //inline or transparent
{
break;
}
answer_records_num=build_answer_records(a_stream, dns_info, object, cheat_opt, MAX_ANSWER_RECORDS_NUM);
if(answer_records_num<=0)
{
MESA_handle_runtime_log(g_fw_dns_plug_info.logger,
RLOG_LV_FATAL,
"DO_ACTION",
"Hit policy_id: %d service: %d action: %d user_region: %s domain: %s qtype: %d addr: %s",
p_result->config_id,
p_result->service_id,
p_result->action,
(tmp_buff==NULL) ? p_result->service_defined : tmp_buff,
(char *)dns_info->query_question.qname,
dns_info->query_question.qtype,
PRINTADDR(a_stream, g_fw_dns_plug_info.level)
);
state=PROT_STATE_GIVEME|PROT_STATE_DROPPKT;
break;
}
memset(cheat_pkt_payload, 0, MAX_CHEAT_PKT_PAYLOAD_LEN);
dns_hdr = (dns_hdr_t *)cheat_pkt_payload;
dns_hdr->id = dns_info->hdr_info.id;
dns_hdr->qdcount = 1;
dns_hdr->ancount = answer_records_num;
payload_len=build_cheat_pkt(cheat_pkt_payload, MAX_CHEAT_PKT_PAYLOAD_LEN, &dns_info->query_question, cheat_opt, answer_records_num);
if(payload_len==-1)
{
MESA_handle_runtime_log(g_fw_dns_plug_info.logger,
RLOG_LV_FATAL,
"DO_ACTION",
"Hit policy_id: %d service: %d action: %d build_cheat_pkt ret: %d addr: %s",
p_result->config_id,
p_result->service_id,
p_result->action,
payload_len,
PRINTADDR(a_stream, g_fw_dns_plug_info.level)
);
break;
}
if(dns_info->hdr_info.qr==0)
{
senddir = MESA_dir_reverse(a_stream->routedir);
}
else
{
senddir = a_stream->routedir;
}
ret=MESA_inject_pkt(a_stream, (const char *)cheat_pkt_payload, payload_len, (const char *)a_packet, senddir);
if(ret<0)
{
MESA_handle_runtime_log(g_fw_dns_plug_info.logger,
RLOG_LV_FATAL,
"SEND_CHEAT_PKT",
"Return of MESA_inject_pkt_feedback function is %d, qname: %s qtype: %d cfg_id: %d service: %d addr: %s",
ret,
dns_info->query_question.qname,
dns_info->query_question.qtype,
p_result->config_id,
p_result->service_id,
PRINTADDR(a_stream, g_fw_dns_plug_info.level)
);
}
state=PROT_STATE_GIVEME|PROT_STATE_DROPPKT;
break;
default:
break;
}
if(object!=NULL)
{
cJSON_Delete(object);
object=NULL;
}
if(tmp_buff!=NULL)
{
free(tmp_buff);
tmp_buff=NULL;
}
return state;
}
static int fw_dns_send_log(struct streaminfo *a_stream, dns_info_t *dns_info, struct Maat_rule_t *result, int result_num, int thread_seq)
{
int i=0;
@@ -519,10 +263,9 @@ extern "C" char FW_DNS_PLUG_ENTRY(stSessionInfo* session_info, void **pme, int
if(hit_num>0)
{
p_result=tsg_fetch_deny_rule(result, hit_num);
if(p_result!=NULL)
{
state=fw_dns_action(a_stream, dns_info, p_result, a_packet);
state=tsg_deal_deny_action(a_stream, p_result, PROTO_DNS, ACTION_RETURN_TYPE_PROT, (const void *)dns_info);
if(state!=PROT_STATE_GIVEME)
{
fw_dns_send_log(a_stream, dns_info, p_result, 1, thread_seq);
@@ -531,6 +274,7 @@ extern "C" char FW_DNS_PLUG_ENTRY(stSessionInfo* session_info, void **pme, int
else
{
fw_dns_send_log(a_stream, dns_info, result, hit_num, thread_seq);
tsg_notify_hited_monitor_result(a_stream, result, hit_num, thread_seq);
}
}
@@ -545,7 +289,6 @@ extern "C" char FW_DNS_PLUG_ENTRY(stSessionInfo* session_info, void **pme, int
extern "C" int FW_DNS_PLUG_INIT(void)
{
int ret=0,len=0;
memset(&g_fw_dns_plug_info, 0, sizeof(g_fw_dns_plug_info));
MESA_load_profile_int_def(g_fw_dns_conffile, "DNS_PLUG", "LOG_LEVEL", &g_fw_dns_plug_info.level, RLOG_LV_FATAL);
@@ -567,26 +310,6 @@ extern "C" int FW_DNS_PLUG_INIT(void)
MESA_handle_runtime_log(g_fw_dns_plug_info.logger, RLOG_LV_FATAL, "REGISTER_TABLE", "Maat_table_register failed, table_name: %s", g_fw_dns_plug_info.table_qname);
return -1;
}
ret=fw_dns_rule_init(g_fw_dns_conffile, g_fw_dns_plug_info.logger);
if(ret<0)
{
return -1;
}
len=sizeof(g_fw_dns_plug_info.s_mode);
ret=sapp_get_platform_opt(SPO_DEPLOYMENT_MODE_STR, g_fw_dns_plug_info.s_mode, &len);
if(ret>=0)
{
if((memcmp(g_fw_dns_plug_info.s_mode, "mirror", strlen(g_fw_dns_plug_info.s_mode)))==0 || (memcmp(g_fw_dns_plug_info.s_mode, "dumpfile", strlen(g_fw_dns_plug_info.s_mode)))==0)
{
g_fw_dns_plug_info.mode=0;
}
else
{
g_fw_dns_plug_info.mode=1;
}
}
return 0;
}

View File

@@ -1,19 +0,0 @@
#ifndef __FW_DNS_PLUG_H__
#define __FW_DNS_PLUG_H__
#include "fw_dns_rule.h"
#define MAX_ANSWER_RECORDS_NUM 32
struct fw_dns_plug
{
int mode;
int level;
int table_qname_id;
char table_qname[MAX_TABLE_NAME_LEN];
char s_mode[MAX_TABLE_NAME_LEN];
char log_path[MAX_TABLE_NAME_LEN*2];
void *logger;
};
#endif

View File

@@ -1,259 +0,0 @@
#include <stdio.h>
#include <time.h>
#include <arpa/inet.h>
#include <MESA/stream.h>
#include <MESA/dns.h>
#include <MESA/cJSON.h>
#include <MESA_prof_load.h>
#include <MESA/Maat_rule.h>
#include <MESA/MESA_handle_logger.h>
#include "tsg_rule.h"
#include "fw_dns_rule.h"
struct fw_dns_rule g_fw_dns_rule_info;
char FW_DNS_RULE_VERSION_20191201=0;
struct dns_str2idx str2index[]={{DNS_TYPE_CNAME, 5, (char *)"CNAME"},
{DNS_TYPE_MX, 2, (char *)"MX"},
{DNS_TYPE_A, 1, (char *)"A"},
{DNS_TYPE_NS, 2, (char *)"NS"},
{DNS_TYPE_AAAA, 4, (char *)"AAAA"},
{DNS_TYPE_TXT, 3, (char *)"TXT"},
{DNS_TYPE_PTR, 3, (char *)"PTR"}
};
int fw_dns_type2index(char *type)
{
int i=0;
for(i=0; i<(int)(sizeof(str2index)/sizeof(struct dns_str2idx)); i++)
{
if(str2index[i].len==(int)strlen(type) && (strncasecmp(str2index[i].type, type, str2index[i].len))==0)
{
return str2index[i].index;
}
}
return -1;
}
void fw_dns_EX_data_create(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
int i=0;
cJSON *one_record=NULL,*pSub=NULL;
struct dns_records *records=(struct dns_records *)calloc(1, sizeof(struct dns_records));
char *tmp_records=(char *)calloc(1, strlen(table_line)+1);
sscanf(table_line, "%d %s %s %s %d", &records->record_id, records->record_name, records->record_type, tmp_records, &records->is_valid);
int index=fw_dns_type2index(records->record_type);
cJSON *tmp_array=cJSON_Parse(tmp_records);
if(tmp_array!=NULL)
{
records->record_num=cJSON_GetArraySize(tmp_array);
records->record_values=(void **)malloc(records->record_num*sizeof(void *));
for(i=0; i<records->record_num; i++)
{
one_record=cJSON_GetArrayItem(tmp_array, i);
if(one_record==NULL)
{
continue;
}
pSub=cJSON_GetObjectItem(one_record, "value");
if(NULL==pSub )
{
continue;
}
switch(index)
{
case DNS_TYPE_A:
records->record_values[i]=calloc(1, sizeof(unsigned int));
inet_pton(AF_INET, pSub->valuestring, records->record_values[i]);
break;
case DNS_TYPE_AAAA:
records->record_values[i]=calloc(1, IPV6_ADDR_LEN);
inet_pton(AF_INET6, pSub->valuestring, records->record_values[i]);
break;
case DNS_TYPE_MX:
break;
case DNS_TYPE_NS:
case DNS_TYPE_TXT:
case DNS_TYPE_PTR:
case DNS_TYPE_CNAME:
records->record_values[i]=calloc(1, strlen(pSub->valuestring)+1);
memcpy(records->record_values[i], pSub->valuestring, strlen(pSub->valuestring));
break;
default:
continue;
}
}
records->table_id=table_id;
atomic_inc(&records->ref_cnt);
(*ad)=(MAAT_PLUGIN_EX_DATA)(records);
cJSON_Delete(tmp_array);
tmp_array=NULL;
}
else
{
free(records);
records=NULL;
}
free(tmp_records);
tmp_records=NULL;
return ;
}
void fw_dns_EX_data_free(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
int i=0;
struct dns_records *records=(struct dns_records *)*ad;
if(records!=NULL)
{
atomic_dec(&records->ref_cnt);
if(records->ref_cnt<=0)
{
for(i=0; i<records->record_num; i++)
{
free(records->record_values[i]);
records->record_values[i]=NULL;
}
free(*records->record_values);
*records->record_values=NULL;
free(records);
records=NULL;
}
}
return ;
}
void fw_dns_EX_data_dup(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp)
{
struct dns_records *records=(struct dns_records *)(*from);
if(records!=NULL)
{
atomic_inc(&records->ref_cnt);
(*to)=(*from);
}
return ;
}
static int set_one_cheat_opt(cheat_pkt_opt_t *cheat_opt, int ttl, int atype, char *record_values)
{
cheat_opt->ttl=ttl;
cheat_opt->res_type=atype;
cheat_opt->cfg_type=atype;
switch(atype)
{
case DNS_TYPE_A:
cheat_opt->res_len=sizeof(unsigned int);
break;
case DNS_TYPE_AAAA:
cheat_opt->res_len=IPV6_ADDR_LEN;
break;
case DNS_TYPE_NS:
case DNS_TYPE_TXT:
case DNS_TYPE_PTR:
case DNS_TYPE_CNAME:
cheat_opt->res_len=MIN(strlen(record_values), sizeof(cheat_opt->res_info)-1);
break;
default:
return 0;
}
memcpy(cheat_opt->res_info, record_values, cheat_opt->res_len);
return 1;
}
int dns_get_cheat_opt(int record_id, int select_num, int ttl, int atype, cheat_pkt_opt_t* cheat_opt, int cheat_opt_num)
{
int i=0,idx=0;
char record_id_str[32]={0};
int used_num=0,real_num=0;
struct dns_records *records=NULL;
real_num=MIN(select_num, cheat_opt_num);
if(real_num<=0)
{
return 0;
}
snprintf(record_id_str, sizeof(record_id_str), "%d", record_id);
records=(struct dns_records *)Maat_plugin_get_EX_data(g_tsg_maat_feather, g_fw_dns_rule_info.table_records_id, record_id_str);
if(records!=NULL)
{
if(records->record_num<=real_num)
{
for(i=0; i<records->record_num; i++)
{
used_num+=set_one_cheat_opt(&(cheat_opt[used_num]), ttl, atype, (char *)(records->record_values[i]));
}
}
else
{
srand(time(0));
idx=rand()%(records->record_num-real_num+1);
for(i=0; i<real_num; i++)
{
used_num+=set_one_cheat_opt(&(cheat_opt[used_num]), ttl, atype, (char *)(records->record_values[idx+i]));
}
}
fw_dns_EX_data_free(records->table_id, (MAAT_PLUGIN_EX_DATA *)&records, 0, NULL);
}
return used_num;
}
int fw_dns_rule_init(const char *conffile, void *logger)
{
int ret=0;
long arg=0;
memset(&g_fw_dns_rule_info, 0, sizeof(g_fw_dns_rule_info));
MESA_load_profile_string_def(conffile, "DNS_PLUG", "TABLE_RECORDS", g_fw_dns_rule_info.table_records_name, MAX_TABLE_NAME_LEN, (char *)"TSG_PROFILE_DNS_RECORDS");
g_fw_dns_rule_info.table_records_id=Maat_table_register(g_tsg_maat_feather, g_fw_dns_rule_info.table_records_name);
if(g_fw_dns_rule_info.table_records_id<0)
{
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, "REGISTER_TABLE", "Maat_table_register failed, table_name: %s", g_fw_dns_rule_info.table_records_name);
return -1;
}
ret=Maat_plugin_EX_register(g_tsg_maat_feather,
g_fw_dns_rule_info.table_records_id,
fw_dns_EX_data_create,
fw_dns_EX_data_free,
fw_dns_EX_data_dup,
NULL,
arg,
NULL);
if(ret<0)
{
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, "REGISTER_TABLE", "Maat_plugin_EX_register failed, table_name: %s", g_fw_dns_rule_info.table_records_name);
return -1;
}
return 0;
}

View File

@@ -1,51 +0,0 @@
#ifndef __FW_DNS_RULE_H__
#define __FW_DNS_RULE_H__
#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 411)
#define atomic_inc(x) __sync_add_and_fetch((x),1)
#define atomic_dec(x) __sync_sub_and_fetch((x),1)
#define atomic_add(x,y) __sync_add_and_fetch((x),(y))
#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y))
typedef int atomic_t;
#define ATOMIC_INIT(i) { (i) }
#define atomic_read(x) __sync_add_and_fetch((x),0)
#define atomic_set(x,y) __sync_lock_test_and_set((x),y)
#else
#include <alsa/iatomic.h>
#endif
#define MIN(a, b) ((a>b) ? b: a)
#define MAX_TABLE_NAME_LEN 32
struct dns_str2idx
{
int index;
int len;
char *type;
};
struct dns_records
{
int ref_cnt;
int record_id;
int record_num;
int is_valid;
int table_id;
char record_type[129];
char record_name[129];
void **record_values;
};
struct fw_dns_rule
{
int table_records_id;
char table_records_name[MAX_TABLE_NAME_LEN];
};
int fw_dns_type2index(char *type);
int fw_dns_rule_init(const char *conffile, void *logger);
int dns_get_cheat_opt(int record_id, int select_num, int ttl, int atype, cheat_pkt_opt_t* cheat_opt, int cheat_opt_num);
#endif