python--DenyHttp项目(2)--ACM监考客户端1.0版

时间:2022-09-15 21:24:54

修复了: 360搜索可以使用的漏洞

    更新版本,上一版本复制的Hosts文件保留的漏洞

#coding:gbk
import os
import sys
from subprocess import *
import time

def Denyfile():

    if not os.path.exists('D:\DenyHttp'):
        os.mkdir('D:\DenyHttp')
        os.mkdir(r'D:\DenyHttp\restore')    

    hostsName = 'C:\Windows\System32\drivers\etc\hosts'
    filename = r'D:\DenyHttp\hosts'
    '''若存在原文件,删除'''
    if os.path.exists(r'D:\DenyHttp\hosts') and os.path.exists(r'D:\DenyHttp\restore\hosts'):
        print(filename + '已存在!开始删除文件')
        os.system("del /f /s /q D:\DenyHttp\*.*")
        os.system("rd /s /q D:\DenyHttp")
        print('文件已经删除!')
    '''删除后创建新的目录'''
    print('创建文件夹:')
    os.system('mkdir d:\\DenyHttp\\restore')

    print('准备复制:')
    if not os.system(r'copy '+ hostsName +' D:\DenyHttp'):
        print('file remove from ' + hostsName + ' to ' + filename)
    if not os.system(r'copy '+ hostsName + r' D:\DenyHttp\restore'):
        print('file remove from ' + hostsName + ' to D:\DenyHttp\restore')

    L = []
    denyNetAddress = getHTTPList(L)
    '''添加htttp到文件'''
    for s in denyNetAddress:
        os.system('echo ' + s +'>>' + filename)

    '''修改hosts文件'''
    if not os.system(r'copy D:\DenyHttp\hosts C:\Windows\System32\drivers\etc'):
        print('file hosts has been already discovered!')

def Recoverfile():
    if not os.system(r'copy D:\DenyHttp\restore\hosts C:\Windows\System32\drivers\etc'):
        print('file hosts has been already recovered!')

def detectnet():
    L = []
    denyNetAddress = getNetList(L)
    mark = 1
    for s in denyNetAddress:
        p = Popen(["ping.exe" , s],
                  stdin=PIPE,stdout=PIPE,stderr=PIPE,
                  shell=True)
        out = p.stdout.read()
        if '127.0.0.1' in str(out):
            mark = 0
    '''mark为假不能访问'''
    if not mark:
        print('can\'t request http')
    else:
        print('can request http')
    return mark
def getHTTPList(denyNetAddress = []):
    denyNetAddress.append('127.0.0.1 sg.search.yahoo.com')
    denyNetAddress.append('127.0.0.1 www.cnblogs.com')
    denyNetAddress.append('127.0.0.1 www.baidu.com')
    denyNetAddress.append('127.0.0.1 www.sogou.com')
    denyNetAddress.append('127.0.0.1 blog.csdn.net')
    denyNetAddress.append('127.0.0.1 www.soso.com')
    denyNetAddress.append('127.0.0.1 cn.bing.com')
    '''1.0修复'''
    denyNetAddress.append('127.0.0.1 www.so.com')

    return denyNetAddress
def getNetList(denyNetAddress = []):
    denyNetAddress.append('www.baidu.com')
    denyNetAddress.append('www.sogou.com')
    denyNetAddress.append('www.soso.com')
    denyNetAddress.append('cn.bing.com')
    denyNetAddress.append('www.so.com')
    denyNetAddress.append('sg.search.yahoo.com')
    denyNetAddress.append('www.cnblogs.com')
    denyNetAddress.append('blog.csdn.net')

    return denyNetAddress
if __name__ == '__main__':
#     Denyfile()
    Recoverfile()
#     print(detectnet())