How to replace the bytes in a binary file? - python

My binary file test.bin contains
11 22 33 44 55 66 ...
I want to modify the 3rd position with AA and my file should be like
11 22 33 AA 55 66 ....

Open the file for update in binary mode, seek to the desired position in the file, then write the replacement character. The following will work in Python 2 and 3 and will overwrite the 4th byte of the file (3rd position if counting from 0) with 0xAA.
with open('test.bin', 'rb+') as f:
f.seek(3)
f.write(b'\xAA')

Related

Adding a column of numbers as a first column to an already existing TEXT file with numbers using PYTHON

I am trying to add a column of increasing multiples of 4,say (4 8 12 16) to an already existing data of numbers of a TEXT file. However, when I try the code [mentioned below] the column data is adding at the end of the data of the existing text file. I cannot use PANDAS because Abaqus software do not have it.
Here is the Existing data in a text file sample example, (i used comma here to separate the integers):
53 , 25
55 , 39
78 , 87
32 , 17
Expected output:
4 , 53 , 25
8 , 55 , 39
12 ,78 , 87
16 ,32 , 17
Here is the code
import numpy as np
# defining the numbers i want to add to the text file
x1 = np.array(range(4,20,4)).tolist()
# opening the existing data file using **append** format
exisiting_data = open('data.txt', 'a')
# looping through the numbers to add to the text file
for eachitem in x1:
exisiting_data.write(str(eachitem)+'\n')
exisiting_data.close()
output:
When i do this I am getting the output as
53 , 25
55 , 39
78 , 87
32 , 17
4
8
12
16
I think this is simple for many of you. I am a beginner for now. Thank you for your suggestions and answers. Thank you
You can't directly write your series as the first column in your text file. What you can do is that you can create a second file and write your series(4, 8, 12) + data from your first file to the second one.

Extract specific bytes in payload from a pcap file using scapy

I am trying to extract a specific byte from each packet in a pcap file. All packets are ICMP.
In the data section, there is a byte that changes each packet. It is in the same position for each one. I would like to extract that byte.
Using scapy:
pkts = rdpcap('test.pcap')
pl = PacketList([p for p in pkts])
bytes(pl[12].payload)
returns the following:
b'E\x00\x00T]\xa7\x00\x00***J***\x01!A\xc0\xa88\x01\xc0\xa88o\x08\x004\xe9\xbf2\x00\x00^"\x87\xbe\x00\x0c2\xf4\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567'
I have enclosed the byte that I want to extract within three stars. However, if I print out the bytes for each packet, the byte that I want to extract will be in a different offset.
If I run a hexdump for each packet as follows:
hexdump(bytes(pl[12].payload))
The specific byte I want to extract is always in the same position, but I don't know how to extract it.
How can I extract a specific byte from a pcap using scapy?
Following this answer here: Get specific bytes in payload from a pcap file
If I execute the same command, it doesn't do anything useful:
>>> hexdump(pkts[14][2].load[8])
0000 00 00 00 00 00 00 00 00 ........
>>>
You want the TTL?
Lets start at the high level, and move down.
Scapy is giving you the constructed packet. If you want the TTL of the packet, call the attribute:
>>> plist[182].ttl
64
If you want to get the specific byte of the packet, lets look at the hexdump:
>>> hexdump(plist[182])
0000 AA BB CC 66 42 DE AA BB CC 3F 52 A3 08 00 45 00 .a.lM..M.AK...E.
0010 00 5B 58 B9 40 00 40 06 64 96 C0 A8 01 28 AC D9 .[X.#.#.d....(..
...
These are in hex, the first field 0000 is the offset, then 16 bytes in hex, then ascii.
Offset Bytes ASCII
====== =============================================== ================
0000 AA BB CC 66 42 DE AA BB CC 3F 52 A3 08 00 45 00 .a.lM..M.AK...E.
Things start at 0, so the the byte addresses are 0..15 for the first line.
The second line the offset is 16 (16 * 1). So the byte addresses are 16..31.
The third line, the offset is 32 (16 * 2). So the byte addresses are 32..47
You highlighted the 7th byte on row 2:
Offset Bytes ASCII
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
====== =============================================== ================
0010 00 5B 58 B9 40 00 40 06 64 96 C0 A8 01 28 AC D9 .[X.#.#.d....(..
That address is:
offset + byte_address.
offset = 16 * 1
byte_address = 6
Which gives us:
16 + 6 = 22
With that, we can now get byte address 22 out of the raw packet:
>>> b = raw(plist[182])
>>> b[22]
64
Note that wireshark packet numbers start at 1. The packets in python are going to start at 0. So in my example, packet 182 corresponded to packet 183 in Wireshark.
plist[182].payload gives you the IP portion of the packet, so the offsets are going to be different since we aren't looking at the whole packet anymore. We can get the same value using the '.ttl' attribute. Or, knowing that the address is byte 8 in the IP header:
>>> plist[182].payload.ttl
64
>>> raw(plist[182].payload)[8]
64

Reading txt file with number and suming them python

I have txt file witht the following txt in it:
2
4 8 15 16 23 42
1 3 5
6
66 77
77
888
888 77
34
23 234 234
1
32
3
23 23 23
365
22 12
I need a way to read the file and sum all the numbers.
i have this code for now but not sure what to do next. Thx in advance
`lstComplete = []
fichNbr = open("nombres.txt", "r")
lstComplete = fichNbr
somme = 0
for i in lstComplete:
i = i.split()`
Turn them into a list and sum them:
with open('nombres.txt', 'r') as f:
num_list = f.read().split()
print sum([int(n) for n in num_list])
Returns 3227
Open the file and use read() method to get the content and then convert string to int, use sum() to get the result:
>>> sum(map(int,open('nombres.txt').read().split()))
3227

Python adding cr to lf in binary data?

I was writing a python script which converts an ascii file containing one pair numbers per line to a straight binary representation. Here is my script:
in_file = open("p02_0609.bin", 'r')
out_file = open("sta013.bin", 'w')
out_data = bytearray()
for line in in_file:
addr, i2c_data = [int(x) for x in line.split(" ")]
out_data.append(addr)
out_data.append(i2c_data)
out_file.write(out_data)
out_file.close()
in_file.close()
and a sample of the file it's reading (about 2000 lines total)
58 1
42 4
40 0
41 0
32 0
33 0
34 0
35 0
36 0
37 0
38 0
39 0
40 1
40 2
33 143
40 3
33 0
40 4
40 5
40 6
40 7
40 8
40 9
40 10
40 11
The output file ends on an odd byte, which it shouldn't since all the data is in pairs, and is about 80 bytes longer than expected. After poking around with a hex editor, I finally found the culprit. Every instance of "10" (Ascii LF) has had a CR appended in front of it. How do I make it stop doing that?
Tl;dr: Python is being a dumbass and adding CR to LF in binary data where that makes no sense. How to fix?
You are working with text files so line endings are automatically added by open function. You need to use the mode 'wb' in open for reading and writing bytes.

carriage characters are lost in the resulted string when reading a file on windows

When reading a txt file in windows by python, carriage characoters are lost in resulted string.
c:/text.txt
aaa\r\nbbb\r\nccc\r\nddd
code:
input = open('c:/text.txt')
str = input.read()
import repr
for i,ch in enumerate(str):
print i,ord(ch),repr.repr(ch)
result:
0 97 'a'
1 97 'a'
2 97 'a'
3 10 '\n'
4 98 'b'
5 98 'b'
6 98 'b'
7 10 '\n'
8 99 'c'
9 99 'c'
10 99 'c'
11 10 '\n'
12 100 'd'
13 100 'd'
14 100 'd'
you can see that all carriage characters are lost.
Any suggestion appreciated.
Thanks.
If you open the file in text mode, Windows line endings \r\n are automatically substituted by standard line endings \n. To prevent this from happening, open the file in binary mode:
input = open('c:/text.txt', 'rb')

Categories