4
I’m having a hard time convert the code below for the version 3.4 of Python, the purpose of this code is to encode and decode hexadecimal, in order to create shellcodes.
import binascii, sys, time
RED = '\033[31m'
WHITE = '\033[37m'
RESET = '\033[0;0m'
def main():
print("shellcode hex encode decoder")
print("programmer : gunslinger_ <yudha.gunslinger[at]gmail.com>")
print "what do you want to do ? %sencode%s / %sdecode%s" % (RED, RESET, WHITE, RESET)
q = raw_input("=> ")
if q == "encode":
inputtype = raw_input("Please input data : ")
print "shellcode => ",
for encoded in inputtype:
print "\b\\x"+encoded.encode("hex"),
sys.stdout.flush()
time.sleep(0.5)
print RESET
elif q == "decode":
inputtype = raw_input("Please input data : ")
cleaninput = inputtype.replace("\\x","")
print "hex => ",cleaninput
print "plaintext => ",
print "\b"+cleaninput.decode("hex")
else:
print "wrong answer ! your choice is %sencode%s or %sdecode%s" % (RED, RESET, WHITE, RESET)
sys.exit(1)
if __name__ == '__main__':
main()
That part I didn’t understand:
print "what do you want to do ? %sencode%s / %sdecode%s" % (RED, RESET, WHITE, RESET)
All right he set the colors on top, and the %sencode%s / %sdecode%s
how does it work? I understand he made a %s
, at the beginning and at the end of the words encounter and Decode and call with the colors.
q = raw_input("=> ")
He defined the variable and this =>
something specific in Python 2?
In this part below I understood some parts but not all if someone explain me better I am grateful.
inputtype = raw_input("Please input data : ")
print "shellcode => ",
for encoded in inputtype:
print "\b\\x"+encoded.encode("hex"),
sys.stdout.flush()
time.sleep(0.5)
Personal thank you.
Have you tried the
2to3
? If that doesn’t work out, say what are these "little problems", what you’ve tried to do to solve them, and what you want us to help. At first glance, a few parentheses after theprint
would solve most of the problems...– mgibsonbr
@mgibsonbr Why don’t you create an answer? I think it should be interesting to show this translation process.
– stderr
@qmechanik I was just giving a quick tip that maybe solve problems without having to think long. But in reality, an answer like yours that explains the differences between the versions is much more useful. The
2to3
, from what I read (never actually used it), it would only be an aid to convert large codes, but it does not guarantee that the result is a correct and functional code (i.e. it does not dispense with careful revision of the converted code).– mgibsonbr