Is there any way to modify strings inside .pcap files with python scapy wthiout yielding an error?

I am experimenting with scapy in python and captured a test pcap which only contains a transfer of a file over HTTP. The contents of the file are plaintext.

I am trying to basically change the Server string:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.11.8
</code>
<code>HTTP/1.0 200 OK Server: SimpleHTTP/0.6 Python/3.11.8 </code>
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.11.8

to a different one, let’s say:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>HTTP/1.0 200 OK
Server: A custom one
</code>
<code>HTTP/1.0 200 OK Server: A custom one </code>
HTTP/1.0 200 OK
Server: A custom one

The thing is that after this change, the size of the packet changes and as a result a lot of TCP packets are marked as retransmissions, the content of the HTTP packet says that bytes are missing etc.

Is there a way to modify strings – and the corresponding size fields – so that the pcap remains without errors?

P.S. I tried changing the Frame Length and Capture Length fields of Ether layer but still was getting errors.

My demo script for the time being is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from scapy.all import *
def replace_user_agent(packets):
i = 0
for packet in packets:
i+=1
if packet.haslayer(Raw):
raw = packet[Raw].load
if b"SimpleHTTP/0.6 Python/3.11.8" in raw:
raw = raw.replace(b"SimpleHTTP/0.6 Python/3.11.8", b"A custom one"+ b" " * (len("SimpleHTTP/0.6 Python/3.11.8") - len("A custom one"))) # cover the missing length with spaces
packet[Raw].load = raw
# Recalculate length fields and checksums
if packet.haslayer(IP):
del packet[IP].len # Recalculate IP length
del packet[IP].chksum # Recalculate IP checksum
if packet.haslayer(TCP):
del packet[TCP].chksum # Recalculate TCP checksum
elif packet.haslayer(UDP):
del packet[UDP].len # Recalculate UDP length
del packet[UDP].chksum # Recalculate UDP checksum
return packets
if __name__ == "__main__":
input_pcap = "input.pcap"
output_pcap = "output.pcap"
packets = rdpcap(input_pcap)
modified_packets = replace_user_agent(packets)
wrpcap(output_pcap, modified_packets)
</code>
<code>from scapy.all import * def replace_user_agent(packets): i = 0 for packet in packets: i+=1 if packet.haslayer(Raw): raw = packet[Raw].load if b"SimpleHTTP/0.6 Python/3.11.8" in raw: raw = raw.replace(b"SimpleHTTP/0.6 Python/3.11.8", b"A custom one"+ b" " * (len("SimpleHTTP/0.6 Python/3.11.8") - len("A custom one"))) # cover the missing length with spaces packet[Raw].load = raw # Recalculate length fields and checksums if packet.haslayer(IP): del packet[IP].len # Recalculate IP length del packet[IP].chksum # Recalculate IP checksum if packet.haslayer(TCP): del packet[TCP].chksum # Recalculate TCP checksum elif packet.haslayer(UDP): del packet[UDP].len # Recalculate UDP length del packet[UDP].chksum # Recalculate UDP checksum return packets if __name__ == "__main__": input_pcap = "input.pcap" output_pcap = "output.pcap" packets = rdpcap(input_pcap) modified_packets = replace_user_agent(packets) wrpcap(output_pcap, modified_packets) </code>
from scapy.all import *

def replace_user_agent(packets):
    i = 0
    for packet in packets:
        i+=1
        if packet.haslayer(Raw):
            raw = packet[Raw].load
            if b"SimpleHTTP/0.6 Python/3.11.8" in raw:
                raw = raw.replace(b"SimpleHTTP/0.6 Python/3.11.8", b"A custom one"+ b" " * (len("SimpleHTTP/0.6 Python/3.11.8") - len("A custom one"))) # cover the missing length with spaces
                packet[Raw].load = raw

                # Recalculate length fields and checksums
                if packet.haslayer(IP):
                    del packet[IP].len  # Recalculate IP length
                    del packet[IP].chksum  # Recalculate IP checksum
                if packet.haslayer(TCP):
                    del packet[TCP].chksum  # Recalculate TCP checksum
                elif packet.haslayer(UDP):
                    del packet[UDP].len  # Recalculate UDP length
                    del packet[UDP].chksum  # Recalculate UDP checksum


    return packets

if __name__ == "__main__":
    input_pcap = "input.pcap"
    output_pcap = "output.pcap"

    packets = rdpcap(input_pcap)
    modified_packets = replace_user_agent(packets)
    wrpcap(output_pcap, modified_packets)

This script does and does not do what I am trying to achieve. It basically replaces the Server string with my shorter custom one, and for the missing bytes it just uses spaces.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật