Spoiler to Python Challenge (help!!!)

I

Ian Vincent

Damn this is annoying me.

I have a webpage with a BZ2 compressed text embedded in it looking like:

'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M
\x07<]\xc9\x14\xe1BA\x06\xbe\x084'

Now, if I simply copy and paste this into Python and decompress it - it
works a treat.

However, I want to read the file containing this data, extract the data
and decompress it and this for some reason does not work.

I am doing the following (excuse the probably very long handed way of
doing it):

file = urllib.urlopen(url, proxies=proxies)
line = file.readlines()
file.close()
line = line[20:]
line = line[:-1]
user = line[0]
password = line[1]
user = user[5:]
user = user[:-2]
user = str(user)
password = password[5:]
password = password[:-2]

This gives me a user string of:

BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M
\x07<]\xc9\x14\xe1BA\x06\xbe\x084

But if I put this into the decompression function, I get a error of
'IOError: invalid data stream'.

I know it is the escape characters but how do I get these to be correctly
converted into a string compatible with bz2.decompress()?
 
T

Terry Hancock

I have a webpage with a BZ2 compressed text embedded in it looking like:

'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M
\x07<]\xc9\x14\xe1BA\x06\xbe\x084'

Now, if I simply copy and paste this into Python and decompress it - it
works a treat.

However, I want to read the file containing this data, extract the data
and decompress it and this for some reason does not work.
[...]
This gives me a user string of:

BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M
\x07<]\xc9\x14\xe1BA\x06\xbe\x084

But if I put this into the decompression function, I get a error of
'IOError: invalid data stream'.

I know it is the escape characters but how do I get these to be correctly
converted into a string compatible with bz2.decompress()?

Took me a long time to figure out what you meant. ;-)

So the string actually contains the backslashes, not the escaped characters.

This works:
'huge'

(which I take it is what your sample data encoded -- though I can't help
but notice it is actually much shorter than the "compressed" version. ;-)).

This may have some security issues, though, since it evaluates essentially
any expression given for user. I'd be interested to know if someone
knows a more secure way.

Cheers,
Terry
 
I

Ian Vincent

Terry Hancock said:
Took me a long time to figure out what you meant. ;-)

So the string actually contains the backslashes, not the escaped
characters.

This works:

'huge'

Unfortunately, it doesn't. Get the same error.
 
T

Terry Hancock


Actually, it doesn't -- I sent you the wrong version of the email.

THIS works (and is what actually produced the output above).

Sorry about that. I was trying the other as an alternative,
but in fact, it doesn't work. So ignore that.

Cheers,
Terry
 
C

Christos Georgiou

This works:


This may have some security issues, though, since it evaluates essentially
any expression given for user. I'd be interested to know if someone
knows a more secure way.

given

a = "a tab\\x09between"

this is more secure than eval:

b= a.decode("string_escape")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top