Python2 to Python3 Hex/Str/Byte Conversion Cheatsheet For Hackers

If you too have been personally victimized by Python3’s 'str' object has no attribute 'decode' exception or other string/bytes-related exceptions, I feel your agony. Trauma from such errors have stopped me from using Python3 for code handling buffers, like POCs for vulnerabilities or CTF exploits. Here’s a reference guide on how to convert between Python3’s hexstr/str/bytes/bytearray.

Python3 Buffer Type Review

  • str
    • An immutable unicode string
    • Created statically using quotes. 
    • Example: mystr = “don’t forget your daily calcium”
  • hexstring
    • A str consisting of hexadecimal numbers (0-9, a-f). 
    • Primarily used to convert binary data to a printable format. 
    • Created like str, but contains only hexadecimal numbers
    • Example: “calc” is “63616c63”
  • bytes
    • An immutable array of one-byte elements
    • Created statically by putting the letter “b” before quotes
    • Example: mybytes = b“bring all the boys to the yard”
  • bytearray 
    • A mutable list of one-byte elements
    • Created through the bytearray constructor, which is initialized with a more primitive type (such as bytes, str)
    • Example: mybytearray = bytearray(b”I’ll grow up to be in a bytearray”)

Ultimate Python3 Type Conversion Chart

This has helped me when it was in my personal notes. Hopefully, now it can help you, too.

 

have mystr of type str

have myhexstr of type hexstr

have mybytes of type bytes

have mybytearray of type bytearray

want mystr of type str

Life is good

import binascii

tmp=binascii.unhexlify(myhexstr)

mystr=tmp.decode(<*encoding_see_bottom_note>)

mystr=mybytes.decode(<*encoding_see_bottom_note>)

mystr=mybytearray.decode(<*encoding_see_bottom_note>)

want myhexstr of type hexstr

import binascii

tmp=binascii.hexlify(mystr)

myhexstr=tmp.decode(“utf-8”)

Life is good

tmp=bytearray(mybytes)

tmp=binascii.hexlify(tmp)

myhexstr=tmp.decode(“utf-8”)

tmp=binascii.hexlify(mybytearray)

myhexstr=tmp.decode(“utf-8”)

want mybytes of type bytes

mybytes=mystr.encode()

import binascii

mybytes=binascii.unhexlify(myhexstr)

Life is good

mybytes=bytes(mybytearray)

want mybytearray of type bytearray

mybytearray=bytearray(mystr.encode())

import binascii

mybytearray=bytearray(binascii.unhexlify(myhexstr))

mybytearray=bytearray(mybytes)

Life is good

*encoding is usually “ascii”/”utf-8” (they’re the same thing), but for non-English character sets, the encoding might be “utf-16”, “utf-32.”

Other Python3 Buffer Need-To-Know

Convert bytes to/from list of ints

Convenient for binary/mathematical operations such as XOR between two buffers

bytes to list of ints

myintlist = list(mybytes)

list of ints to bytes

mybytes = bytes(myintlist)

Read bytes and str from files

Read data from a file as str

Open the file using "r"

Example: mystr = open("C:\whtaguy\orderyogurt.py", "r").read()

Read data from a file as bytes

Open the file using "rb"

Example: mybytes = open("C:\whtaguy\orderyogurt.py", "r").read()


Congratulations. You are now empowered to handle Python3’s buffer frenzy. Welcome to your new, shiny life.

Comments

Popular posts from this blog

uTorrent CVE-2020-8437 Vulnerability And Exploit Overview

D-Link Router CVE-2021-27342 Timing Side-Channel Attack Vulnerability Writeup

Guy's 30 Reverse Engineering Tips & Tricks