#!/usr/bin/python
# coding: utf-8

import os, sys, public

_title = 'Check if the nginx config file is compromised'
_version = 1.0  # 版本
_ps = "Check if the nginx config file is compromised"  # 描述
_level = 3  # 风险级别： 1.提示(低)  2.警告(中)  3.危险(高)
_date = '2023-11-21'  # 最后更新时间
_ignore = os.path.exists("data/warning/ignore/sw_nginx_malware.pl")
_tips = [
    "Follow the prompts to find the nginx config file and delete the js hang-up content ",
    "Restart nginx server"
]
_help = ''
_remind = 'Before fixing it manually, it is recommended to make a backup of the configuration file in case the service fails to start. Or use one click to fix '


def check_run():
    '''
        @name 开始检测
        @author lwh<2023-11-21>
        @return tuple (status<bool>,msg<string>)
    '''
    import glob
    path = '/www/server/panel/vhost/nginx/'
    risk_file = []
    for filename in glob.glob(os.path.join(path, '*.conf')):
        if os.path.isdir(filename):
            continue
        output = public.ReadFile(filename)
        if "sub_filter" in output:
            risk_file.append(filename)
    if len(risk_file) > 0:
        return False, 'The following nginx files contain malicious content: {}'.format('、'.join(risk_file))
    return True, 'Risk-free'
