1

I know that "start" is incorrect but I guess you understand what I want to do... I know there is a function called "bytes" but I don't know how to use it correctly. For example I have this bytearray

'\x54\x68\x69\x73\x20\x70\x72\x6F\x67\x72\x61\x6D\x20\x6D\x75\x73\x74\x20\x62\x65'

how can I read this and start it with the python script? What functions should I use and what should I import? Thank you for your answers (I'm a newbie btw)

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 2
    Sorry, I don't understand what you want to do. What output do you expect to see from your script? – Kevin Apr 10 '14 at 18:55
  • Possible duplicate of http://stackoverflow.com/questions/7380460/byte-array-in-python ...? – AlG Apr 10 '14 at 18:58
  • By `start` it sounds like you mean `pass`. Can python not read the bytes instead of passing them in? Or maybe place your bytes from whereever you got them into a file, pass python the name of the file and have it read the binary data from there. – cmd Apr 10 '14 at 19:12
  • You wanna be able to *read* it? ` >>> bytes('\x54\x68\x69\x73\x20\x70\x72\x6F\x67\x72\x61\x6D\x20\x6D\x75\x73\x74\x20\x62\x65') 'This program must be'` – Savir Apr 10 '14 at 19:12
  • kevin, I have converted an executable into a bytearray but I'm a newbie in python and I'm wondering how can I start that executable (converted in the bytearray) P.s.: the bytearray is just a little part, because I can't post the entire bytearray Thank you for your reply – user3520845 Apr 10 '14 at 19:13
  • If you can't post the entire bytearray, then please create a smaller dummy one which you can post, and post it. Without that it's very hard for us to understand what you want. – pts Apr 10 '14 at 19:30

1 Answers1

0

Is this what you thought of?

>>> bytes('\x54\x68\x69\x73\x20\x70\x72\x6F\x67\x72\x61\x6D\x20\x6D\x75\x73\x74\x20\x62\x65')                                              
'This program must be'
>>> 

if you would wanted to use that in a script:

#!/usr/local/bin/python


# You can encode the python instructions on this webpage:
# http://www.string-functions.com/string-hex.aspx
#
#
# The following hex string encodes this:
# for i in xrage(0,10):
#     print i
thestring = "\x66\x6f\x72\x20\x69\x20\x69\x6e\x20\x78\x72\x61\x6e\x67\x65\x28\x30\x2c\x31\x30\x29\x3a\x0d\x0a\x20\x20\x20\x20\x70\x72\x69\x6e\x74\x20\x69"

# Demonstration of possible code injection
# in the function:
thehack = "import os;os.system('echo foobar')"


def hex2string(myhexstring):
    """be carefull with this function possible codeinjection"""
    myhexcmd = bytes("%s" % myhexstring)
    exec myhexcmd


hex2string(thestring)

kind regards,

Dirk

Dirk
  • 451
  • 7
  • 21
  • I guess you nearly got it but after I run the module it says: File "C:\Users\***\***\test.py", line 1, in bytes('\x54\x68\x69\x73\x20\x70\x72\x6F\x67\x72\x61\x6D\x20\x6D\x75\x73\x74\x20\x62\x65') TypeError: string argument without an encoding – user3520845 Apr 10 '14 at 19:32
  • Try to put a single u letter (for unicode) in front of the string like this: bytes(u'\x54\x68\x69\x73\x20\x70\x72\x6F\x67\x72\x61\x6D\x20\x6D\x75\x73\x74\x20\‌​x62\x65') . This should work, but i cant test the code right now on windows sry :\ – Dirk Apr 10 '14 at 19:43
  • instead of the u (because it didnt work) I've put the b of bytes (I got it from the offical python site) aniway there are no more errors but the program still not run... this is so damn impossible – user3520845 Apr 10 '14 at 20:08
  • whats the actual output from the program? Or does it just return to the command prompt? if you use the code above just put a print in fron of the "hex2string" function call – Dirk Apr 10 '14 at 20:45
  • it doesn't work... maybe if you are so altruist can you send me your skype just to help me with this, i promise I'll give you what you deserve (reputation, thanks and other) – user3520845 Apr 10 '14 at 21:32
  • Um shure but first we would have to clarifiy the time (i would suggest utc). The handle would be uncle_raze ... But i am as nearly never online. So what utc time would you suggest? – Dirk Apr 11 '14 at 06:42
  • Hey, could we do that tomorrow at that time? Its been late for me today had a long workday. So if you dont mind i would suggest we do the skype session in tomorrow saturday 04/12/12 at 20.30 UTC? if that fits into your schedule. regards dirk – Dirk Apr 11 '14 at 20:40
  • Hey i still would help you solving your problem. Maybe you just want to send me a copy of your code per email. So i can have a look at it and tell you waht you have to change. Also helpful if you tell me your runtime environment. – Dirk Apr 13 '14 at 20:05
  • thank you for your help and sorry for my late answer... aniway I've added you on skype – user3520845 Apr 23 '14 at 10:20
  • @user3520845 No problem, you are welcome :) . Do you still need a support session to make this thing work? – Dirk Apr 23 '14 at 10:39
  • of course I need... but if you want to help me just accept skype because here is impossible... – user3520845 Apr 23 '14 at 11:33
  • @user3520845 of course i can do that. But right now improper for me because i am still at work. Give me about 2 1/2 hours then i can add you and we can have a chitchat about your issue and finde a way to solve it :) – Dirk Apr 23 '14 at 11:52