I need to get bytearray as string on python3.
on python 2.7 , str(bytearray) results the contents of bytearray in string format.
Python 2.7.18 (default, Feb 8 2022, 09:11:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> c = bytearray(b'\x80\x04\x95h\x00\x00')
>>> str(c)
'\x80\x04\x95h\x00\x00'
>>>
on python 3.6, even the "bytearray" keyword is added into the resulting string.
Python 3.6.8 (default, Aug 12 2021, 07:06:15)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> c = bytearray(b'\x80\x04\x95h\x00\x00')
>>> str(c)
"bytearray(b'\\x80\\x04\\x95h\\x00\\x00')"
>>>
why is it happening so on 3.6 ?
how to get the exact same behavior on 3.6 as that of 2.7 ?
Note: I cannot do c.decode(), as those are compressed/pickled data which will result in invalid start byte errors.
Any suggestions please.
The __str__ method for the bytearray class is different in Python 3, to obtain a similar result you could try below snippet.
>>> str(bytes(c))
"b'\\x80\\x04\\x95h\\x00\\x00'"
Related
When I start a python interactive session from the command line I am greeted by :
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Is there a way of disabling that message so that I go immediately to the >>> prompt?
Yes, there is a way to do so.
Type in the cmd:
python -q
instead of
python
and this should do the trick.
With Path I can run path.is_dir(), how can I do the same thing with PosixPath?
How can I do the same thing with PosixPath?
Exactly the same way. PosixPath extends Path so has all the underlying methods:
Python 3.9.0 (default, Oct 12 2020, 02:44:01)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib, os
>>> pathlib.PosixPath("/").is_dir()
True
>>> os.system("touch /tmp/aFile.txt")
>>> pathlib.PosixPath("/tmp/aFile.txt").is_dir()
False
>>> pathlib.PosixPath("/tmp/aFile.txt").is_file()
True
I'm testing on Intel x86_64, Ubuntu 64bit, Python3, Pwntools v4.3.1
$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import *
>>> addr = 0xbffffb78
>>> print(p32(addr))
b'x\xfb\xff\xbf'
In my opinion, the correct packing result for 0xbffffb78 should be\x78\xfb\xff\xbf.
But why did b'x\xfb\xff\xbf' happen?
where is \x78 ?
And what is the correct way of packing, not using p32()?
This is just how Python renders bytes objects. If a byte can be rendered as an ASCII character, it is displayed as one.
>>> b"\x78"
b'x'
To see the bytes rendered as hex you can use the hex method of the bytes object:
>>> b'x\xfb\xff\xbf'.hex()
'78fbffbf'
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> id(None)
9392928
I see that None in python has been already instantiated at the beginning without me doing anything. Can someone help me to understand why I cannot do this even if None is just a 'name' for an object.
>>> None = 3
File "<stdin>", line 1
SyntaxError: cannot assign to None
It's one of the many built in constants. See here - https://docs.python.org/2/library/constants.html
I'm trying to compute hmac using sha-512.
The Perl code:
use Digest::SHA qw(hmac_sha512_hex);
$key = "\x0b"x20;
$data = "Hi There";
$hash = hmac_sha512_hex($data, $key);
print "$hash\n";
and gives the correct hash of
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde
daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854
Python version:
import hashlib, hmac
print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
which gives the incorrect hash of
9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182
477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a
any ideas why the Python version is giving me the wrong hash?
Edit:
version is
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
yes indeed -- it seems the Leopard version of python2.5 is the one that is broken.
below run on a Penryn-based MBP...
$ **uname -a**
Darwin lizard-wifi 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386
dpc#lizard-wifi:~$ **which python**
/usr/bin/python
Running this version installed in Leopard OS
dpc#lizard-wifi:~$ python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib, hmac
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a
>>>
And then the MacPorts version of python2.5
$ /opt/local/bin/python2.5
Python 2.5.4 (r254:67916, Feb 3 2009, 21:40:31)
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib, hmac
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854
>>>
I am unable to replicate your results here. In IDLE using Python 2.5:
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
...
IDLE 1.2.2
>>> import hashlib, hmac
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854
Which version of Python? Strings are Unicode in Python 3. Is this a Unicode issue?
Under python 2.5.2 I get the correct hash
I guess the old version was the problem