A simple question.
I have a <\class 'str'> (checked) with a 124 length hexa content named hexapacket.
I'm doing like this:
import bitstring
data = BitStream(hexa=hexapacket)
# My processing on bits
But it raises errors like it can't find the length etc ..
ValueError: invalid literal for int() with base 10: '0edd0e000201a9017dc0c3898000000000'
and
ValueError: Don't understand length '0edd0e000201a9017dc0c3898000000000' of token.
and
KeyError: ('0edd0e000201a9017dc0c3898000000000', 0)
Could you help to make it working ? It's the solution i wanted to parse datas.
EDIT :
I tried some debug and the output is strange, the hex() cast and the bin() cast add 0b and 0x at the start of the string and i have handle it by string = string[2:]
But it still doesn't work with BitStream from bitstring.
I precise that the original packet comes from pyshark and i casted the packet.data.data into string.
CODE :
if hexapacket.find(':') != -1:
hexapacket = ''.join(packet.split(":"))
if hexapacket.find('0x') != -1:
hexapacket = hexapacket[2:]
msgid = int(bin(int(hexapacket[:4],16))[2:-2],2)
messagetype = dict_ids[msgid]
lenoflen = int(bin(int(hexapacket[:4],16))[-2:],2)
print("ID: %d\nMSG: %s\nLoL: %d\n" % (msgid,messagetype,lenoflen))
print("My hexapacket\n%s" % hexapacket)
raw = BitStream(hex=hexapacket)
OUTPUT :
ID: 950
MSG: GameMapMovementRequestMessage
LoL: 1
My hexapacket
0ed93c0003519a418c418b050c0405fafb5a21348190b66ecc166c09f832a7324069fcd9e19ea6be654b26b42563908947857a2b3cb25ce920837262a5fb69
ERRORS:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/bitstring.py", line 612, in tokenparser
length = int(length)
ValueError: invalid literal for int() with base 10: '0pad:0pad:0pad:0pad:0pad:0pad:0pad:0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 232, in <module>
messages = PacketProcessing().splitProcess(packet)
File "main.py", line 182, in splitProcess
data1 = raw.read('pad:%d'%datalen*8)
File "/usr/local/lib/python3.5/site-packages/bitstring.py", line 3880, in read
_, token = tokenparser(fmt)
File "/usr/local/lib/python3.5/site-packages/bitstring.py", line 622, in tokenparser
raise ValueError("Don't understand length '{0}' of token.".format(length))
ValueError: Don't understand length '0pad:0pad:0pad:0pad:0pad:0pad:0pad:0' of token.
OUTPUT of repr(hexapacket) and type(hexapacket):
'0ed93a0002118b11a8050c04053e03bcd154bb84543c9b2a7992280bddf099b126acd1e75bf274842565e499d9e0221f86c02fa26d0a859ce426e63a74'
and
<class 'str'>
ANSWER : Use BitString module for Python3.x, it's easier to cast and read data.
It should work, if you specify hex= keyword argument:
>>> import bitstring
>>> bitstring.BitStream(hex='0edd0e000201a9017dc0c3898000000000')
BitStream('0x0edd0e000201a9017dc0c3898000000000')
Related
`
def arduino_connect():
global sock
print("Cihazlar axtarılır....")
nearby_devices = bluetooth.discover_devices()
num = 0
for i in nearby_devices:
num+=1
print(str(num)+":"+bluetooth.lookup_name(i)+" MAC: "+i)
if i=="00:21:13:00:EF:19":
selection = num-1
bd_addr = nearby_devices[selection]
port = 1
print("Sən seçdin:" +bluetooth.lookup_name(bd_addr))
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))
`
Traceback (most recent call last):
File "ordubot.py", line 92, in <module>
test(wake)
File "ordubot.py", line 81, in test
response(voice)
File "ordubot.py", line 57, in response
arduino_connect()
File "ordubot.py", line 38, in arduino_connect
print(str(num)+":"+bluetooth.lookup_name(i)+" MAC: "+i)
TypeError: must be str, not NoneType
This code gives this error, can you please help?
In this code, I want python to connect to the mac address specified by bluetooth, but this code gives an error.
When joining items using the + operator, they have to be of the same type.
That means that if bluetooth.lookup_name(i) returns a result which isn't a string (a NoneType in your case) than the concatenation fails.
You can use format string to print the result anyway -
print(f"{}:{} MAC: {}".format(num, bluetooth.lookup_name(i), i)
This will work even if not all of the arguments of format are strings.
There is code here that I am trying to convert from Python2 to Python3. In this section of code, data is received from a socket. data is declared to be an empty string and then concatenated. This is an important Python2 to 3 distinction. In Python3, the 'received' variable is of type Bytes and thus needs to be converted to string first via the use of str(). However, str() needs an encoding parameter. What would the default one be for python 2? I've tried several different encodings (latin-1 and such) but they seem to not match up with a magic value that is defined here after being unpacked here
"\xffSMB seems to decode correctly while "\xfeSMB does not.
I have adjusted the non_polling_read function and the NetBIOSSessionPacket class as follows. Full modified source code available on GitHub.
def non_polling_read(self, read_length, timeout):
data = b''
bytes_left = read_length
while bytes_left > 0:
try:
ready, _, _ = select.select([self._sock.fileno()], [], [], timeout)
if not ready:
raise NetBIOSTimeout
received = self._sock.recv(bytes_left)
if len(received) == 0:
raise NetBIOSError('Error while reading from remote', ERRCLASS_OS, None)
data = data + received
bytes_left = read_length - len(data)
except select.error as ex:
if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN:
raise NetBIOSError('Error occurs while reading from remote', ERRCLASS_OS, ex[0])
return data
class NetBIOSSessionPacket:
def __init__(self, data=0):
self.type = 0x0
self.flags = 0x0
self.length = 0x0
if data == 0:
self._trailer = ''
else:
try:
self.type = data[0]
if self.type == NETBIOS_SESSION_MESSAGE:
self.length = data[1] << 16 | (unpack('!H', data[2:4])[0])
else:
self.flags = data[1]
self.length = unpack('!H', data[2:4])[0]
self._trailer = data[4:]
except Exception as e:
import traceback
traceback.print_exc()
raise NetBIOSError('Wrong packet format ')
When I start the server and issue 'smbclient -L 127.0.0.1 -d 4' from the commandline, the server first creates a libs.nmb.NetBIOSTCPSession which appears to be working well. Once it tries to unwrap the libs.nmb.NetBIOSSessionPacket, it throws an exception.
Traceback (most recent call last):
File "/root/PycharmProjects/HoneySMB/libs/smbserver.py", line 3975, in processRequest
packet = smb.NewSMBPacket(data=data)
File "/root/PycharmProjects/HoneySMB/libs/smb.py", line 690, in __init__
Structure.__init__(self, **kargs)
File "/root/PycharmProjects/HoneySMB/libs/structure.py", line 77, in __init__
self.fromString(data)
File "/root/PycharmProjects/HoneySMB/libs/structure.py", line 144, in fromString
self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0])
File "/root/PycharmProjects/HoneySMB/libs/structure.py", line 288, in unpack
raise Exception("Unpacked data doesn't match constant value %r should be %r" % (data, answer))
Exception: ("Unpacked data doesn't match constant value b'\\xffSMB' should be 'ÿSMB'", 'When unpacking field \'Signature | "ÿSMB | b\'\\xffSMBr\\x00\\x00\\x00\\x00\\x18C\\xc8\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xfe\\xff\\x00\\x00\\x00\\x00\\x00\\xb1\\x00\\x02PC NETWORK PROGRAM 1.0\\x00\\x02MICROSOFT NETWORKS 1.03\\x00\\x02MICROSOFT NETWORKS 3.0\\x00\\x02LANMAN1.0\\x00\\x02LM1.2X002\\x00\\x02DOS LANMAN2.1\\x00\\x02LANMAN2.1\\x00\\x02Samba\\x00\\x02NT LANMAN 1.0\\x00\\x02NT LM 0.12\\x00\\x02SMB 2.002\\x00\\x02SMB 2.???\\x00\'[:4]\'')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/PycharmProjects/HoneySMB/libs/smbserver.py", line 3597, in handle
resp = self.__SMB.processRequest(self.__connId, p.get_trailer())
File "/root/PycharmProjects/HoneySMB/libs/smbserver.py", line 3979, in processRequest
packet = smb2.SMB2Packet(data=data)
File "/root/PycharmProjects/HoneySMB/libs/smb3structs.py", line 435, in __init__
Structure.__init__(self,data)
File "/root/PycharmProjects/HoneySMB/libs/structure.py", line 77, in __init__
self.fromString(data)
File "/root/PycharmProjects/HoneySMB/libs/structure.py", line 144, in fromString
self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0])
File "/root/PycharmProjects/HoneySMB/libs/structure.py", line 288, in unpack
raise Exception("Unpacked data doesn't match constant value %r should be %r" % (data, answer))
Exception: ("Unpacked data doesn't match constant value b'\\xffSMB' should be 'þSMB'", 'When unpacking field \'ProtocolID | "þSMB | b\'\\xffSMBr\\x00\\x00\\x00\\x00\\x18C\\xc8\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xfe\\xff\\x00\\x00\\x00\\x00\\x00\\xb1\\x00\\x02PC NETWORK PROGRAM 1.0\\x00\\x02MICROSOFT NETWORKS 1.03\\x00\\x02MICROSOFT NETWORKS 3.0\\x00\\x02LANMAN1.0\\x00\\x02LM1.2X002\\x00\\x02DOS LANMAN2.1\\x00\\x02LANMAN2.1\\x00\\x02Samba\\x00\\x02NT LANMAN 1.0\\x00\\x02NT LM 0.12\\x00\\x02SMB 2.002\\x00\\x02SMB 2.???\\x00\'[:4]\'')
Now it's obvious why this throws an exception. After all, 0xFF does NOT equal ÿ or þ. The question is why is 0xFF the value it tries to write in there in the first place when it should be two different values?
The original python 2 code seems to be quite close to C (using unpack and importing cstring). Is there an obvious benefit to this here or could this be done more simply?
My actual question is:
In the original code, there is no reference to any encoding anywhere. So how is this divined then? Where does it translate 0xFF to ÿ?
My get_current_mac function:
def get_current_mac(interface):
ifconfig_result = subprocess.check_output(["ifconfig", interface])
mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
if mac_address_search_result:
return mac_address_search_result.group(0)
else:
print("[-] Could not read MAC address.")
I am then wanting to print the result to the screen:
options = get_arguments()
current_mac = get_current_mac(options.interface)
print("Current MAC address is: " + str(current_mac))
The entire error is:
Traceback (most recent call last):
File "./MAC_changer.py", line 38, in <module>
current_mac = get_current_mac(options.interface)
File "./MAC_changer.py", line 30, in get_current_mac
mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
File "/usr/lib/python3.7/re.py", line 183, in search
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
line 38 is: options = get_arguments()
line 30 is: mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
You need to convert a byte-like object into a string, this can be done using decode method:
ifconfig_result = ifconfig_result.decode('utf-8')
In this link you can read more about converting bytes to string
I'm trying to send some data with a python script to a java server. I use the socket module in python to send and recieve data.
When I send data, I need to specify a header with the datalength in it. The header is as following:
a uint8 for the version number
a uint8 for padding ('reserved')
a uint16 for the length of the data that is sent
That is a total of 32 bits.
I can use numpy to create an array with a certain data type, but the problem is sending this data through the socket. I use the following function to send data:
def send(socket, message):
r = b''
totalsent = 0
# as long as not everything has been sent ...
while totalsent < len(message):
# send it ; sent = actual sent data
sent = socket.send(message[totalsent:])
r += message[totalsent:]
# nothing sent? -> something wrong
if sent == 0:
raise RuntimeError("socket connection broken")
# update total sent
totalsent = totalsent + sent
return r
message = (something_with_numpy(VERSION_NUMBER, PADDING, len(data)))
send(socket, message)
I keep getting TypeErrors with this function. These pop up at len(message), r += message[...], or some other place.
I was wondering if there is a better way to do this, or how to fix this so it does work?
UPDATE: here are some exact error traces. I have tried several different things, so these error traces might have become irrelevant.
Traceback (most recent call last):
File "quick.py", line 47, in <module>
header += numpy.uint8(VERSION_NUMBER)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3')
header = numpy.array([VERSION_NUMBER * 255 + PADDING, len(greetData)], dtype=numpy.uint16)
Traceback (most recent call last):
File "quick.py", line 48, in <module>
print(header + greetData)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S22') dtype('S22') dtype('S22')
Traceback (most recent call last):
File "quick.py", line 47, in <module>
r = send(conn, numpy.uint8(VERSION_NUMBER))
File "quick.py", line 13, in send
while totalsent < len(message):
TypeError: object of type 'numpy.uint8' has no len()
Traceback (most recent call last):
File "quick.py", line 47, in <module>
r = send(conn, numpy.array([VERSION_NUMBER], dtype=numpy.uint8))
File "quick.py", line 17, in send
r += message[totalsent:]
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3')
You'll want to use the struct module to format the header before sending the data.
import struct
def send_message(socket, message):
length = len(message)
version = 0 # TODO: Is this correct?
reserved = 0 # TODO: Is this correct?
header = struct.pack('!BBH', version, reserved, length)
message = header + message # So we can use the same loop w/ error checking
while ...:
socket.send(...)
i am trying to conclude the unfinished module, but i'm having major problem which i can't find fix for, Also note, that i am doing this in Python2. Full code is here.
When the code was executed, the first problem i got was in line 69:
Full Log:
>>> main()
INFO:Exchange.exchange:Exchange Service is starting
INFO:pika.adapters.base_connection:Connecting to 127.0.0.1:5672
INFO:pika.adapters.blocking_connection:Created channel=1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 38, in main
Exchange(rmqhost, redishost, redisport, redisdb, status, publishresult).consume()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 69, in __init__
log.debug("Connecting to redis on " + self.redishost + ":" + self.redisport + " db: " + self.redisdb)
TypeError: coercing to Unicode: need string or buffer, int found
So i tried converting it to unicode string:
log.debug("Connecting to redis on " + str(self.redishost) + ":" + str(self.redisport) + " db: " + str(self.redisdb))
But it gave me the same error on the same line:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 69, in __init__
log.debug("Connecting to redis on " + str(self.redishost) + ":" + str(self.redisport) + " db: " + str(self.redisdb))
TypeError: coercing to Unicode: need string or buffer, int found
I also tried another way of converting string, but same error:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 69, in __init__
log.debug("Connecting to redis on %s: %s db: %s",
TypeError: coercing to Unicode: need string or buffer, int found
What may the problem be? Even when i tried to covert it to the string it gave me the same error unfortunately... Do i need to import something from __futre__ that would help the problem?
The coercing error happens when trying so do an operation with a unicode and something which cannot be automatically converted to unicode.
For example:
>>> u'something' + 11
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, int found
On the other hand, with unicode and str it will work - str will coerce to unicode before applying the operation:
>>> u'something' + 'else'
u'somethingelse'
In the example, there are strings and variables (of unknown type), so it has to be that at least one of the variables was unicode and one an int, hence the "coercing to Unicode" error.
General Solution
Any of these will work:
>>> u'aaaa ' + str(9) + ':' + str(15)
u'aaaa 9:15'
>>> u'aaaa %s: %s' % (9, 15)
u'aaaa 9: 15'
>>> u'aaaa {}: {}'.format(9, 15)
u'aaaa 9: 15'
Logging Solution
The proper way to format logs would be:
log.debug("Connecting to redis on %s: %s db: %s",
self.redishost,
self.redisport,
self.redisdb)
I.e. you do not format the string - you let the logger do that.
That way, you avoid formatting the strings if the log level is disabled.
It also looks much better.