issue with formatting characters - python

having a bit of an issue displaying characters
i have a payload recieved from a protocol request :
538cb9350404521a6c44020404563b152606102001085800020002aabb0000563b1526000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
the length of that is 509
what i want to display is the first 4 bytes, then 1 byte, then 1 byte
538cb935
04
04
currently to view the payload i am doing the following :
tm = struct.unpack(">L", payload[0:4])
print "Time : ", tm
ouroripv = struct.unpack(">b", payload[5])
print "Our onion address : "
print "Ip version : ", ouroripv
ouroraddrlen = struct.unpack(">b", payload[6]) # Giving a length of 82 etc atm
print "Ip length : ", ouroraddrlen
i get the result :
Time : (1401731381,)
Our onion address :
Ip version : (4,)
Ip length : (82,)
as you can see the Ip length, the 6th byte in on the payload is displaying 82 rather than the 4 it should be, what is the correct struct.unpack command that is needed to display this ?
how can i do this ?
Thanks guys

in python, the slicing doesn't include the last value, so payload[0:4] takes the first 4 bytes, from 0 to 3.
payload[3] is the fourth byte
payload[4] is the fifth byte

Related

Python Multiline String Find

I have an Cisco ASA with a VPN tunnel configured. I call the CLI command via API and it returns this multiline string:
\nSession Type: LAN-to-LAN\n\nConnection : 192.168.1.10\nIndex : 11701 IP Addr : 192.168.1.10\nProtocol : IKEv2 IPsecOverNatT\nEncryption : IKEv2: (1)AES256 IPsecOverNatT: (1)AES256\nHashing : IKEv2: (1)SHA256 IPsecOverNatT: (1)SHA256\nBytes Tx : 0 Bytes Rx : 0\nLogin Time : 23:14:43 EST Fri Dec 3 2021\nDuration : 0h:11m:50s\n\n
I can't figure out how to get only the "Bytes Rx" plus the number out beside it. I've tried searching it like this, but it returns "Bytes Tx":
import re
regex_parse = re.compile(r'[a-zA-Z]+\s[a-zA-Z][a-zA-Z]\s+:\s[0-9]+')
multilinestring = webhook_api_call()
for item in multilinestring:
a = regex_parse.search(item)
print(a.group(0))
Output:
Bytes Tx : 0
I want to only get Bytes Rx and the number out beside it
Looks like you are trying to parse the result of sh vpn-sessiondb l2l from a Cisco ASA. The output is pretty standard, so I would skip the regex and do the following:
multilinestring = webhook_api_call()
lines = multilinestring.split("\n")
for l in lines:
if l.find("Bytes Tx") != -1:
print("Bytes Rx" + l.partition("Bytes Rx")[2])
Output:
Bytes Rx : 0
Good luck with your code!

Send UDP Datagrams using Python

I want to send a data request over udp using the socket API. The format of the request is as follows:
ID | Data_Length | Data
The request contains the following parameters :An Identifier (ID), (Data_Length) is the size of (Data) and (Data) which is the data to be sent, (Data) has a variable size.
The code I wrote is as follows:
def send_request():
request_format="bbs" # 1 Byte for the ID 1 Byte for Data_Length and s for data
data_buff=np.array([1,2,3,4,5,6,7,8,9]) # Data to be sent
msg = struct.pack(request_format,0x01,0x09,data_buff.tobytes())
print("msg = ", msg)
s0.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s0.sendto(msg, (UDP_BC_IP, UDP_SERVER_PORT))
My questions:
1- Using Wireshark I can see that only the 1st Byte of Data has been sent why ?
2- The output of the print instruction is msg = b'\x01\t\x01' why did I get this output, I was waiting for something similar to [0x01,0x09,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09]
Check the dtype of data_buff - it is int64 unless you use:
data_buff = np.array([1,2,3,4,5,6,7,8,9], dtype=np.uint8)
Then repeat your s specifier according to the size of the array:
request_format="bb" + str(data_buff.size) + "s"
Now you can pack with:
msg = struct.pack(request_format,0x01,0x09,data_buff.tobytes())
and your message will look like this:
b'\x01\t\x01\x02\x03\x04\x05\x06\x07\x08\t'
The TAB character is ASCII code 9, so you will see \t where your data is 9.

Why is this first Byte printed as (b'c\x..) instead of (b'x63\x..)?

I am writing a program for padding oracle attacks and need bytearrays,
but if I define a new bytearray the first byte 0x63 gets printed different.
I have to XOR 2 bytearrays bytewise.
test = bytearray( [99,203,00] )
print(test)
print(hex(99))
Output:
bytearray(b'c\xcb\x00')
0x63
This is my first question here. Thanks for your help!
For string output python replaces printable hexcodes by the chr(hexcode) character for display purposes:
print('c', ord('c'),hex(ord('c'))) # c 99 '0x63'
t = bytearray([99,203,0])
print(t) # bytearray(b'c\xcb\x00')
print(t[0],t[1],t[2]) # 99 203 0
They are equivalent - but shorter to print out. You can get a all-hex representation like so:
t = bytearray([99,203,0])
t_hex = '\\'+'\\'.join( ( hex(i) for i in t) )
print(t_hex)
Output:
\0x63\0xcb\0x0

Python convert HEX number digits ASCI representation into string

I want to make function doing exactly this:
#This is my imput number
MyNumberDec = 114
MyNumberHex = hex(MyNumberDec)
print (MyNumberDec)
print (MyNumberHex)
#Output looks exactly like this:
#114
#0x8a
HexFirstDigitCharacter = MagicFunction(MyNumberHex)
HexSecondDigitCharacter = MagicFunction(MyNumberHex)
#print (HexFirstDigitCharacter )
#print (HexSecondDigitCharacter )
#I want to see this in output
#8
#A
What is that function?
Why I need this?
For calculating check-sum in message sending towards some industrial equipment
For example command R8:
N | HEX | ASC
1 52 R
2 38 8
3 38 8
4 41 A
Bytes 1 and 2 are command, bytes 3 and 4 are checksum
Way of calculating checksum: 0x52 + 0x38 = 8A
I have to send ASCII 8 as third byte and ASCII A as fourth byte
Maybe I dont need my magicfunction but other solution?
You can convert an integer to a hex string without the preceding '0x' by using the string formatter:
MyNumberDec = 114
MyNumberHex = '%02x' % MyNumberDec
print(MyNumberHex[0])
print(MyNumberHex[1])
This outputs:
7
2

RFID reading using python via usb SyntaxError: invalid token

I have two rfid card the values are 0004518403 and 000452738 after reading their value in python terminal. I want to give them name like 0004518403 is "javed" and 000452738 as "aquib". So when next time when I use the card it must not show me the value but it must show me the name which I've defined to them.
import serial
import time
serial = serial.Serial('/dev/ttyUSB0', baudrate = 9600)
while True:
if serial.inWaiting() > 0:
read_result =serial.read(15)
print("Sleeping 2 seconds")
if(read_result==0004520738):
print "aquib"
elif(read_result==0004518403):
print "javed"
time.sleep(2)
serial.flushInput() # ignore errors, no data
I am trying this code but it show me an error :
SyntaxError: invalid token
In the first if condition.
I am not getting where is the problem.
You should compare read results with strings, not numbers, like
read_result=='0004520738'
0004520738 without quotes is a number. As it starts with a 0 sign, it is interpreted as number of base 8. Numbers of base 8 cannot contain digits 8 and 9 apparently.
>>> 01234567
342391
>>> 012345678
File "<stdin>", line 1
012345678
^
SyntaxError: invalid token
>>>
Also I don't get why you read 15 bytes, but compare results with 10 bytes string, that's smth wrong there

Categories