ucloud 签名算法

 

 

# Python version:3.6.5
# Python version:Python 3.6.5
import hashlib
import urllib
from urllib.parse import urlparse
import datetime
import requests
import json
import re

apiUrl = "https://api.ucloud.cn"
headers = {"content-type": "application/json"}
now_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
PublicKey = "PublicKey"
Action = "DescribeUHostInstance"
private_key = 'private_key'

def _verfy_ac(private_key, data):
"""
参考文章:
https://docs.ucloud.cn/UCloudStack/apiguide/overview?id=_13-%e7%ad%be%e5%90%8d%e7%ae%97%e6%b3%95
https://docs.ucloud.cn/api/summary/signature
"""
# 请求参数串
items = list(data.items())
# 将参数串排序
items.sort()
# 拼接
params_data = "";
for key, value in items:
params_data = params_data + str(key) + str(value)
params_data = params_data + private_key
# 生成的Signature值
sign = hashlib.sha1()
sign.update(params_data.encode("utf-8"))
signature = sign.hexdigest()
return signature



def ListUHostsecBaseCheckWarnsForPay():
Offset = 0
Limit = 100
for r in range(100):
data = {
"Action": "ListUHostsecBaseCheckWarnsForPay",
"Region": "cn-bj2",
'Zone': 'cn-bj2-02',
'PublicKey': PublicKey,
"PageIndex": r,
"PageSize": 40
}

data["Signature"] = _verfy_ac(private_key, data)
res = requests.post(url=apiUrl, data=data)
response = res.text
infos = json.loads(response)['Infos']
if infos:
for i in infos:
if '存在弱口令用户' not in i['Description'] and i['RiskLevel'] == '高危':
print(i['RuleID'],i['RiskLevel'],i['RiskType'],i['Treatment'],i['HostIP'],i['AppName'],i['Description'].replace(' ','_'),i['Exe'],i['Path'])
# print(infos)
else:
break

def GetUHostsecBaseCheckRuleDescription():
# 数据格式化
data = {
'Action': 'GetUHostsecBaseCheckRuleDescription',
'Region': 'cn-bj2',
'Zone': 'cn-bj2-02',
'PublicKey': PublicKey,
'RuleID': 'B0407019V01'
}
data["Signature"] = _verfy_ac(private_key, data)

# 进行post请求
res = requests.post(url=apiUrl, data=data)

# 将返回的数据进行格式化提取描述(Description)
response = res.text
infos = json.loads(response)
Description = infos['Description']

# 从描述中提取风险描述内容
risk_description = re.search(r'<strong>风险描述:</strong><br/>\s*<p>(.*?)</p>', Description, re.S)
if risk_description:
risk_description = risk_description.group(1).strip()
else:
risk_description = "风险描述内容为空"

# 从描述中提取处理建议内容
advice = re.findall(r'处理建议:</strong><br/>\n(.*?)</pre>', Description, re.S)
if advice:
print(advice)
advice = re.sub(r'<.*?>', '', advice[0].strip()).replace('\n\n', '')
else:
advice = "处理建议内容为空"

# 提取备注内容
remark = re.findall(r'备注:<br/>\s*(.*?)<br/>', Description, re.S)
if remark:
remarks = remark
remark = re.sub(r'<.*?>', '', remarks[0].strip()).replace('\n', '')

else:
remark = "备注内容为空"

print("风险描述:")
print(risk_description)
print()
print("处理建议:")
print(advice)
print()
print("备注:")
print(remark)

print(Description)
# GetUHostsecBaseCheckRuleDescription()
ListUHostsecBaseCheckWarnsForPay()
 

 

posted @ 2023-03-03 17:13  Hello_worlds  阅读(39)  评论(0编辑  收藏  举报