python - Twitch Bot URL 恶意软件/病毒检测 PYTHON
问题描述
我正在研究 Python 中的 Twitch Bot 模块,其中 Bot 不断监视某人消息中的 URL,并将确定该 URL 是否是恶意的。恶意软件功能已经运行,并且已经针对 Python 中设置的字符串进行了测试。
这是从字符串创建 URL 数组的好代码:
好代码:
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
def Find(string):
# findall() has been used
# with valid conditions for urls in string
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,string)
return [x[0] for x in url]
# Driver Code
string = 'Hey guys I think you really need to check www.youtube.com and www.discovery.com out'
indicators = Find(string)
def malware(bot, user):
for site in indicators:
params = {'apikey': api_key, 'resource': site}
response = requests.get(url, params=params)
response_json = json.loads(response.content)
if response_json['positives'] <= 0:
bot.send_message(f"The URL {site} has been determined to be: NOT MALICIOUS AND SAFE")
elif 1 >= response_json['positives'] >= 3:
bot.send_message(f"The URL {site} has been determined to be: POSSIBLY MALICIOUS")
elif response_json['positives'] >= 4:
bot.send_message(f"The URL {site} has been determined to be: DEFINITELY MALICIOUS")
else:
bot.send_message(f"The URL {site} cannot be determined to be safe or not: PROCEED WITH CAUTION!!!!")
time.sleep(5)
不幸的是,当我尝试进一步开发此代码以持续监控消息时,它要么不起作用,要么将包括 URL 在内的原始消息拉到恶意软件函数中并且函数中断。
谁能给我一些关于如何使这个糟糕的代码工作的想法?
我需要有关“检查”功能的帮助
错误代码:
def Find2(message):
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,message)
return [x[0] for x in url]
LINKS = ("www.yes.com", "www.google.com")
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
def Check(bot, user, message):
if any([site in message for site in LINKS]):
indicators = Find2(message)
malware(bot, user, indicators)
return False
return True
def malware(bot, user, message):
for site in message:
params = {'apikey': api_key, 'resource': site}
response = requests.get(url, params=params)
response_json = json.loads(response.content)
bot.send_message(f"I detected the URL {site} before I broke")
if response_json['positives'] <= 0:
bot.send_message(f"The URL {site} has been determined to be: NOT MALICIOUS AND SAFE")
elif 1 >= response_json['positives'] >= 3:
bot.send_message(f"The URL {site} has been determined to be: POSSIBLY MALICIOUS")
elif response_json['positives'] >= 4:
bot.send_message(f"The URL {site} has been determined to be: DEFINITELY MALICIOUS")
else:
bot.send_message(f"The URL {site} cannot be determined to be safe or not: PROCEED WITH CAUTION!!!!")
time.sleep(5)
解决方案
推荐阅读
- azure-devops - 有什么方法可以限制哪些池可以为 Artifact 提要做出贡献?
- c++ - 使用 Ceres Solver 求解非线性系统:编译问题
- java - 如何保留 POJO 中定义的 JSON 响应顺序
- google-chrome-extension - 更新网址时如何禁用内容脚本?
- android - 离子卡滚动错误
- python - 组合的组合
- html - Bootstrap 4 个垂直选项卡和右侧的内容
- c# - 主窗口之前的 WPF PRISM 7 登录窗口
- php - 如何在单击 html 按钮时调用特定的 php 代码部分?
- ansible - 检测 Terraform 中的 Ansible 更改并执行它们