Scapy:如何在http标头中操纵Host? [英] Scapy: How to manipulate Host in http header?

查看:192
本文介绍了Scapy:如何在http标头中操纵Host?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这段代码来获取http标头并设置Host:

I wrote this piece of code to get http header and set Host:

http_layer = packet.getlayer(http.HTTPRequest).fields
http_layer['Host'] = "newHostName"
return packet

运行上述代码后,正确设置了新的主机名,但是问题是,当我在pcap文件中写入数据包时,仍然在http字段中看到了先前的主机, 是否有操纵http_layer['Host']的绝对方法? 任何帮助,将不胜感激. 问候.

After running the afforementioned code,the new host name has been set correctly, but the problem is that when I write the packet in pcap file, I still see the previous host in http fields, Is there an absolute way to manipulate http_layer['Host'] ? Any help would be appreciated. Regards.

推荐答案

毕竟,找到了答案. 关键是scapy首先解析HTTP Request并显示其字段的字典.因此,当我们尝试分配诸如Host的新字段时,它将更改已解析的Host,并且不会更改原始字段的值. 因此,这是修改Host或任何其他相应字段的方法:

After all, found the answer. The key is that scapy firstly parses HTTP Request and shows the dict of its fields. So when we try to assign a new field like Host, it changes the Host which it has already parsed and does not change the original field value. So, this is the way to modify Host or any other respective fields:

str_headers = pkt['HTTP']['HTTP Request'].fields['Headers']
str_headers = str_headers.replace('Host: ' + pkt['HTTP']['HTTP Request'].fields['Host'], 'Host: ' + new_val)
pkt['HTTP']['HTTP Request'].fields['Headers'] = str_headers
return pkt

这篇关于Scapy:如何在http标头中操纵Host?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆