static void *aucQuery(void *unused)
{
int ret = 0;
struct url_info url_info;
bool is_mali = false;
#if SUPPORT_CLOUD_UPDATE_AUC_INFO
struct auc_info auc_info;
#endif
unsigned int cat_id = 0;
int tmp_id = 0;
int index = 0;
int index_cat = 0;
int index_subcat = 0;
unsigned char matched = 0;
struct url_entry *tmp_list = NULL;
struct url_entry *clean = NULL;
unsigned char tmp_url_len;
char tmp_url[MAX_URL_LEN] = {0};
printWar("create AUC query thread\n");
while (1) {
pthread_mutex_lock(&url_lock);
tmp_list = url_list.next;
pthread_mutex_unlock(&url_lock);
while (tmp_list) {
pthread_mutex_lock(&url_lock);
tmp_list->process_flag = URL_PROCESSING;
pthread_mutex_unlock(&url_lock);
url_info.url = tmp_list->url;
url_info.info_len = 0;
ret = pthread_mutex_trylock(&mali_url_check_lock);
if (!ret)
{
is_mali = web_protection? is_url_malicious(url_info.url) : false;
if (is_mali)
{
cat_id = URL_CAT_SECURITY;
URL_CLASS_LOG(LOG_LEVEL_DEBUG, "Malicious URL detected. URL: %s, Original Cat ID: %d, Mapped Cat ID: %d", url_info.url, cat_id, cat_id);
#if DEBUG
printWar("cat ret=%d url=%s id=%d info_id=%d\n", cat_id, url_info.url, tmp_list->id, tmp_list->info_id);
#endif /* DEBUG */
pthread_mutex_lock(&url_lock);
tmp_list->prev->next = tmp_list->next;
if (tmp_list->next) {
tmp_list->next->prev = tmp_list->prev;
}
tmp_list->query = 1;
tmp_id = tmp_list->id;
tmp_list->id = 0;
tmp_list->info_id = 0;
tmp_list->cat_map = 0;
tmp_url_len = tmp_list->url_len;
memset(tmp_url, 0, MAX_URL_LEN);
strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len);
clean = tmp_list;
tmp_list = tmp_list->next;
clean->next = NULL;
clean->prev = NULL;
pthread_mutex_unlock(&url_lock);
pthread_mutex_unlock(&mali_url_check_lock);
write_to_csv(url_info.url, cat_id, cat_id, "Security");
goto block_mali;
}
else
{
pthread_mutex_unlock(&mali_url_check_lock);
}
}
if((!opts.use_tp_service && tmp_list->cat_map) ||
(opts.use_tp_service && (tmp_list->info_id != PCTL_WEB_URL_ID_ALL) && (tmp_list->cat_map & (~URL_CAT_SECURITY))))
{
printWar("before send to auc\n");
#if SUPPORT_CLOUD_UPDATE_AUC_INFO
update_libauc_url(&auc_info, opts.use_tp_service);
ret = auc_query(&url_info, &auc_info, opts.use_tp_service);
#else
ret = auc_query(&url_info, opts.use_tp_service);
#endif
printWar("after send to auc\n");
for (index = 0; index < url_info.info_len; index++) {
printWar("cat ret=%d url=%s id=%d name=%s\n", ret, url_info.url, url_info.info[index].id, url_info.info[index].name);
}
}
//remove from url_list
pthread_mutex_lock(&url_lock);
tmp_list->prev->next = tmp_list->next;
if (tmp_list->next) {
tmp_list->next->prev = tmp_list->prev;
}
tmp_list->query = 1;
tmp_id = tmp_list->id;
tmp_list->id = 0;
tmp_list->info_id = 0;
tmp_list->cat_map = 0;
tmp_url_len = tmp_list->url_len;
memset(tmp_url, 0, MAX_URL_LEN);
strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len);
clean = tmp_list;
tmp_list = tmp_list->next;
clean->next = NULL;
clean->prev = NULL;
pthread_mutex_unlock(&url_lock);
//send to kernel
cat_id = 0;
int original_cat_id = 0;
const char *category_name = "";
for (index = 0; index < url_info.info_len; index++) {
original_cat_id = url_info.info[index].id;
category_name = url_info.info[index].name;
int cat_id = 0; // 每次循环初始化 cat_id
URL_CLASS_LOG(LOG_LEVEL_DEBUG, "Non-malicious URL. URL: %s, Original Cat ID: %d, Mapped Cat ID: %d", url_info.url, original_cat_id, cat_id);
for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) {
matched = 0;
for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) {
if (url_info.info[index].id == mapping_table[index_cat][index_subcat] ||
mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) {
cat_id |= 0x1 << (mapping_table[index_cat][0] - 1);
matched = 1;
break;
}
}
if (matched) {
break;
}
}
// 将结果写入CSV文件
write_to_csv(url_info.url, original_cat_id, cat_id, category_name);
}
block_mali:
printWar("send to kernel, url=%s cat_id=%x\n", url_info.url, cat_id);
url_send(tmp_id, cat_id, tmp_url, tmp_url_len, URL_REQ_CAT);
printWar("after send to kernel\n");
}
cond_timedwait(500);//500ms
}
return 0;
}
这段代码实现了将分类结果存储到csv文件中的功能,下面第二个代码没有保存的功能,请你根据第一个代码,在不改变第二个代码原本的功能前提下,添加保存到csv文件的功能
static void *aucQuery(void *unused)
{
int ret = 0;
struct url_info url_info;
bool is_mali = false;
#if SUPPORT_CLOUD_UPDATE_AUC_INFO
struct auc_info auc_info;
struct auc_info web_protection_info;
#endif
unsigned int cat_id = 0;
int tmp_id = 0;
int index = 0;
int index_cat = 0;
int index_subcat = 0;
unsigned char matched = 0;
struct url_entry *tmp_list = NULL;
struct url_entry *clean = NULL;
#ifdef SUPPORT_BLOCKING_WHITE_LIST
bool in_whitelist = false;
#endif
unsigned char tmp_url_len;
char tmp_url[MAX_URL_LEN] = {0};
printWar("create AUC query thread\n");
while (1) {
pthread_mutex_lock(&url_lock);
tmp_list = url_list.next;
pthread_mutex_unlock(&url_lock);
while (tmp_list) {
pthread_mutex_lock(&url_lock);
tmp_list->process_flag = URL_PROCESSING;
pthread_mutex_unlock(&url_lock);
url_info.url = tmp_list->url;
url_info.info_len = 0;
#ifdef SUPPORT_BLOCKING_WHITE_LIST
if(tmp_list->in_whitelist)
goto skip;
#endif
printWar("url_info: url=%s, info_id=0x%x, cat_map=0x%x\n", url_info.url, tmp_list->info_id, tmp_list->cat_map);
ret = pthread_mutex_trylock(&mali_url_check_lock);
if (!ret)
{
if(tmp_list->info_id == PCTL_WEB_URL_ID_ALL && web_protection)
{
#ifdef SUPPORT_WEB_PROTECTION_WHITELIST
if(check_web_protection_whitelist(url_info.url))
{
printWar("url=%s in web_protection whitelist, skip query!\n", url_info.url);
is_mali = false;
}
else
{
#endif
if(opts.no_avira_web_protection == USE_AVIRA_SPECIAL_WEB_PROTECION_SERVICE)
{
printWar("before send to avira web_protection\n");
#if SUPPORT_CLOUD_UPDATE_AUC_INFO
update_avira_web_protection_url(&web_protection_info);
ret = malicious_url_query(&url_info, &web_protection_info);
#else
ret = malicious_url_query(&url_info);
#endif
if(url_info.info_len >= 1)
{
is_mali = (url_info.info[0].id != 1);
}
else
{
is_mali = false;
}
printWar("after send to avira web_protection\n");
}
#ifdef SUPPORT_TPLINK_WEB_PROTECTION
else if(opts.no_avira_web_protection == USE_TPLINK_WEB_PROTECION_SERVICE)
{
printWar("before send to tplink web_protection\n");
#if SUPPORT_CLOUD_UPDATE_AUC_INFO
update_libauc_url(&auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE);
auc_initialize(&auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE);
malicious_url_query_using_tplink(&url_info, &auc_info);
#else
auc_initialize(opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE);
malicious_url_query_using_tplink(&url_info);
#endif
if(url_info.info_len >= 1)
{
is_mali = (url_info.info[0].id != 1);
}
else
{
is_mali = false;
}
printWar("after send to tplink web_protection\n");
}
#endif
else if(opts.no_avira_web_protection == USE_AUC_INSTEAD)
{
is_mali = false;
//Force treat it as a original url, go to auc
tmp_list->info_id = PCTL_WEB_URL_ID_ALL + 1;
tmp_list->cat_map |= (0x1 << 11); //Fake it as blocking unknown, rather than pure security request(0x0030)
printWar("Force run a AUC query\n");
}
#ifdef SUPPORT_LOCAL_MALICIOUS_URL_PROTECTION
else if(opts.no_avira_web_protection == USE_LOCAL_DATABASE)
{
printWar("before send to local web_protection datbase\n");
is_mali = web_protection ? is_url_malicious(url_info.url) : false;
printWar("after send to avira web_protection datbase\n");
}
#endif
#ifdef SUPPORT_WEB_PROTECTION_WHITELIST
}
#endif
}
else
{
is_mali = false;
}
if (is_mali)
{
cat_id = URL_CAT_SECURITY;
#if DEBUG
printWar("cat ret=%d url=%s id=%d info_id=%d\n", cat_id, url_info.url, tmp_list->id, tmp_list->info_id);
#endif /* DEBUG */
pthread_mutex_lock(&url_lock);
tmp_list->prev->next = tmp_list->next;
if (tmp_list->next) {
tmp_list->next->prev = tmp_list->prev;
}
tmp_list->query = 1;
tmp_id = tmp_list->id;
tmp_list->id = 0;
tmp_list->info_id = 0;
tmp_list->cat_map = 0;
#ifdef SUPPORT_BLOCKING_WHITE_LIST
tmp_list->in_whitelist = false;
#endif
tmp_url_len = tmp_list->url_len;
memset(tmp_url, 0, MAX_URL_LEN);
strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len);
clean = tmp_list;
tmp_list = tmp_list->next;
clean->next = NULL;
clean->prev = NULL;
pthread_mutex_unlock(&url_lock);
pthread_mutex_unlock(&mali_url_check_lock);
goto block_mali;
}
else
{
pthread_mutex_unlock(&mali_url_check_lock);
}
}
#ifdef SUPPORT_BLOCKING_WHITE_LIST
skip:
#endif
if(tmp_list->info_id != PCTL_WEB_URL_ID_ALL)
{
printWar("before send to auc\n");
#if SUPPORT_CLOUD_UPDATE_AUC_INFO
update_libauc_url(&auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE);
ret = auc_query(&url_info, &auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE);
#else
ret = auc_query(&url_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE);
#endif
printWar("after send to auc\n");
for (index = 0; index < url_info.info_len; index++) {
printWar("cat ret=%d url=%s id=%d name=%s\n", ret, url_info.url, url_info.info[index].id, url_info.info[index].name);
}
}
//remove from url_list
pthread_mutex_lock(&url_lock);
tmp_list->prev->next = tmp_list->next;
if (tmp_list->next) {
tmp_list->next->prev = tmp_list->prev;
}
tmp_list->query = 1;
tmp_id = tmp_list->id;
tmp_list->id = 0;
tmp_list->info_id = 0;
tmp_list->cat_map = 0;
#ifdef SUPPORT_BLOCKING_WHITE_LIST
in_whitelist = tmp_list->in_whitelist;
tmp_list->in_whitelist = false;
#endif
tmp_url_len = tmp_list->url_len;
memset(tmp_url, 0, MAX_URL_LEN);
strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len);
clean = tmp_list;
tmp_list = tmp_list->next;
clean->next = NULL;
clean->prev = NULL;
pthread_mutex_unlock(&url_lock);
//send to kernel
cat_id = 0;
for (index = 0; index < url_info.info_len; index++) {
for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) {
matched = 0;
for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) {
if (url_info.info[index].id == mapping_table[index_cat][index_subcat]
|| mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) {
cat_id |= 0x1 << (mapping_table[index_cat][0] - 1);
matched = 1;
break;
}
}
if (matched) {
break;
}
}
}
#ifdef SUPPORT_BLOCKING_WHITE_LIST
if (in_whitelist && (cat_id & URL_CAT_SECURITY)) {
/* if device in whitelist, remove security property from result */
cat_id = cat_id & (~URL_CAT_SECURITY);
printWar("url=%s is malicious(in auc), but allow it as its device(mac) is in device whitelist!\n", url_info.url);
}
#endif
#ifdef SUPPORT_WEB_PROTECTION_WHITELIST
if ((cat_id & URL_CAT_SECURITY) && check_web_protection_whitelist(url_info.url)) {
/* if url is in web_protection whitelist, remove security property from result */
cat_id = cat_id & (~URL_CAT_SECURITY);
printWar("url=%s is malicious(in auc), but allow it as it's in web_protection whitelist!\n", url_info.url);
}
#endif
block_mali:
printWar("send to kernel, url=%s cat_id=%x\n", url_info.url, cat_id);
url_send(tmp_id, cat_id, tmp_url, tmp_url_len, URL_REQ_CAT);
printWar("after send to kernel\n");
}
cond_timedwait(500);//500ms
}
return 0;
}
最新发布