编程语言
首页 > 编程语言> > 使用阿里云的SLB与日志服务的Python sdk实现自动封禁恶意访问用户IP

使用阿里云的SLB与日志服务的Python sdk实现自动封禁恶意访问用户IP

作者:互联网

文章目录

使用阿里云的SLB与日志服务的Python sdk实现自动封禁恶意访问用户IP

from aliyun.log import    GetProjectLogsRequest,LogClient
import datetime
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.AddAccessControlListEntryRequest import AddAccessControlListEntryRequest
from aliyunsdkslb.request.v20140515.RemoveAccessControlListEntryRequest import RemoveAccessControlListEntryRequest
from aliyunsdkslb.request.v20140515.DescribeAccessControlListAttributeRequest import DescribeAccessControlListAttributeRequest
import json


endpoint = 'https://cn-hangzhou.log.aliyuncs.com'  # 选择与上面步骤创建Project所属区域匹配的Endpoint
access_key_id = '**************'  # 使用您的阿里云访问密钥AccessKeyId
access_key = '*****************'  # 使用您的阿里云访问密钥AccessKeySecret
project = "*********"  #日志中心project ID
AccessControlListID="***********"  #slb访问控制列表ID
client = AcsClient(access_key_id, access_key, 'cn-hangzhou')

limit = 1000
whileIPList = ["124.71.133.196","122.9.71.14","116.63.49.58"]

def getBlackIP():
    startTime=(datetime.datetime.now() - datetime.timedelta(minutes=5)).strftime("%Y-%m-%d %H:%M:%S")
    endTime=(datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
    sql = "select  remote_addr,count(*) as c from uki-prod-kong where __date__ > '{0}' and __date__ < '{1}' and status > 308 and status < 500  group by remote_addr order by  c DESC LIMIT 5".format(startTime,endTime)
    print(sql)
    client = LogClient(endpoint, access_key_id, access_key)
    req = GetProjectLogsRequest(project,sql )
    res = client.get_project_logs(req)
    dataList =  res.body
    print(dataList)
    blackIPList = []
    for item in dataList:
       if  int(item['c']) > limit and item["remote_addr"] not in  whileIPList:
           blackIPList.append(item["remote_addr"])
    return blackIPList

def AddAccessControlListEntry(BlackIPList):
    addEntryList = []
    for item  in BlackIPList:
        addEntryList.append({"entry": "{}/32".format(item),"comment":"{}".format((datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S"))})
    request = AddAccessControlListEntryRequest()
    request.set_accept_format('json')
    request.set_AclId(AccessControlListID)
    request.set_AclEntrys(addEntryList)
    client.do_action_with_exception(request)
    print("已添加黑名单IP:",BlackIPList)

def RemoveAccessControlListEntry(removeEntryList):
    delEntryList=[]
    for item in removeEntryList:
        delEntryList.append({'entry': "{}".format(item)})
    #print("删除2小时之前的IP:",delEntryList)
    request = RemoveAccessControlListEntryRequest()
    request.set_accept_format('json')
    request.set_AclId(AccessControlListID)
    request.set_AclEntrys(delEntryList)
    client.do_action_with_exception(request)
    print("已清理过期的黑名单IP:",removeEntryList)

def  getRemoveEntryList():
    request = DescribeAccessControlListAttributeRequest()
    request.set_accept_format('json')
    request.set_AclId(AccessControlListID)
    response = client.do_action_with_exception(request)
    data = response.decode("utf-8")
    data = json.loads(data)
    delTime = (datetime.datetime.now() - datetime.timedelta(hours=2)).strftime("%Y-%m-%d %H:%M:%S")
    removeEntryList=[]
    try:
        for item in data["AclEntrys"]["AclEntry"]:
           if item["AclEntryComment"] != "" and item["AclEntryComment"] < delTime:
               removeEntryList.append(item["AclEntryIP"])
        return  removeEntryList
    except KeyError:
        print("IP黑名单为null")
        return []

def  main():
    blackIPList = getBlackIP()
    RemoveEntryList = getRemoveEntryList()

    if  len(blackIPList) == 0:
        print("暂时没有IP需要被添加到黑名单")
    else:
        AddAccessControlListEntry(blackIPList)
    if len(RemoveEntryList) == 0:
        print("暂时没有过期的IP 需要被清理")
    else:
        RemoveAccessControlListEntry(RemoveEntryList)

if __name__ == '__main__':
    main()

标签:__,Python,IP,request,datetime,item,import,SLB
来源: https://blog.csdn.net/qq_23191379/article/details/112004337