python为木云DNS添加黑名单URL地址
木云DNS有个让人很舒服的功能,可以对一些钓鱼网站、垃圾邮件等之类的网站解析到特定的地址来实现DNS层面对网络的防护,用户也可以自定义URL库以弥补系统库的收集不足。
今天在维护DNS时发现居然开放了API接口,以早应开发了,一直没有看过,今天有时间看了一下API文档,现在就开放了两个接口,获取威胁库列表和设置威助数据。说干就干,先设置API联动接口。
接口生成之后会给一个APPID各APPKEY,平台接口调用地址为:
http 协议://系统地址:系统端口/dns/api/,分共参数有四个分别是appid,action,timestamp,sign.
sign根据appid,action,timestamp,app_key进行m5算法进行签名。
获取威胁库名
请求参数:{"api_id": 3, "timestamp": 1650870098, "sign": "
cd41a379c1be52631f6036d20bcf0ddf", "action": "getTiBuckets"}
返回结果:
{"code": 0, "msg": "success", datas:[{“id”: 57, “name”:”钓鱼网站”}, {“id”: 58, “name”:”恶意软件”}, {“id”: 59,
“name”:”矿池”}]}
添加威胁数据
请求参数:
{"api_id": 3, "timestamp": 1650870098, "sign": "cd41a379c1be52631f6036d20bcf0ddf", "action": "setTiData",
"bucket_id": 3, "datas": ["192.168.3.88", "192.168.0.1"]}
根所以上内容开始进行脚本编写:
#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*-
import requests
import json
import hashlib
import time
import warnings
from urllib3.exceptions import InsecureRequestWarning
import argparse
def set_signature(appid,api_key,timestamp,action):
sign=hashlib.md5(str(appid).encode('utf-8')+api_key.encode('utf-8')+action.encode('utf-8')+str(timestamp).encode('utf-8')).hexdigest()
return sign
def operation_dns(appid,api_key,timestamp,action,**kwargs):
headers = {'Content-Type': 'application/json'}
url=""
sign=set_signature(appid,api_key,timestamp,action)
data={"api_id":appid,"timestamp":timestamp,"sign":sign,"action":action}
if action=="setTiData":
data.update({"bucket_id":kwargs.get("bucket_id"),"datas":kwargs.get("datas")})
with warnings.catch_warnings():
warnings.simplefilter("ignore",InsecureRequestWarning)
respose=requests.post(url=url,headers=headers,json=data,verify=False)
if respose.status_code==200:
res=respose.json()
print(res.get("msg"))
if __name__ == '__main__':
print("This is a test file for DNS resolution")
appid=2
api_key=""
url=""
timestame=time.time()
operation_dns(appid,api_key,timestame,action="getTiBuckets")
#operation_dns(appid,api_key,timestame,action="setTiData",bucket_id=1,datas=["abc.com"])
parser=argparse.ArgumentParser()
group=parser.add_mutually_exclusive_group()
group.add_argument("-f","--file",help="请输入完整的文件路径")
group.add_argument("-u","--url",help="请输入域名")
args=parser.parse_args()
if args.file:
with open(args.file,"r") as f:
blackurllist=[s.strip() for s in f.readlines()]
print(blackurllist)
#operation_dns(appid,api_key,timestame,action="setTiData",bucket_id=1,datas=blackurllist)
elif args.url:
datas=[args.url.strip()]
operation_dns(appid,api_key,timestame,action="setTiData",bucket_id=1,datas=[args.url.strip()])
测试可以正常添加威肋库,以后收集到URL名单,再添加时不需要再登录系统,一个脚本即可完成