-2

I'm trying to convert 64-bit symbolic string to 32-bit. My advisor said, what that 64-bit symbolic string is a Hex string and I should only cut all the bits after 8th left bit. I decided to do it in Python by mask:

that_string & 0xFFFFFFFF

But it's impossible to do without converting string to int():

int('0x'+that_string, 16) & 0xFFFFFFFF

But then 'that_string' becomes a truly Integer and I can't convert her back to string. It's not possible to make chr(int('0x'+that_string, 16) & 0xFFFFFFFF), it causes in problem:

ValueError: chr() arg not in range(256)

Also it's not possible to do decode() because of another Error:

bash ~$: (that_string).decode('hex')
...  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Odd-length string

I tried to search for another opportunity, but didn't get any suitable solution.

And, yes, I tried base64 - library, but my adviser said, what that's wrong idea and what that's not a solution.

I'm much embarrassed. Could You help me, please? May be, there are other ways exist, aren't they?

Fruitty
  • 57
  • 2
  • 10
  • Can you post the actual hex string so we can figure out the format? – tdelaney Dec 03 '15 at 18:00
  • Can you fit the value represented by your 64-bit string into a 32-bit number? – Michelle Welcks Dec 03 '15 at 23:20
  • By *symbolic* string, do you mean used for *symbolic execution*? The answer is tied to the specific library/framework you are using I think please provide extra info. But each symbolic variable should be an array of symbolic bits, if you can extract the *least siginificant symbolic bits*, you are done! – memoselyk Dec 04 '15 at 06:42
  • Thank You for replying! Symbolic string in this context means a random non-numeric string with any length: for example, it can be '401759ea2' - it's 64-bit string, that's **not a number**, so that's not 17204354722 in 0x and 'a' here doesn't stands for 10 in 0x. – Fruitty Dec 07 '15 at 01:41

1 Answers1

0

Take the rightmost 8 digits with a slice:

str[-8:]

where str is a string containing the hexadecimal representation of your input data.

If str has fewer than 8 digits, then they will all be returned by this expression, which is what you want.

For example:

>>> str = '0123456789abcdef'
>>> str[-8:]
'89abcdef'
>>> str = 'abcd'
>>> str[-8:]
'abcd'
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Unfortunately, No - afraid, You got the idea wrong: you're talking about the simple symbolic string, but it's totally Wrong. The real idea is following: there is a string `that_string`, what is encoded in **hex**. So I have something like `that_string = hex(original_string)`, but `that_string` is **not** a digit. And `original_string` is unknown, it's need to be found. Hope, it's more clear explanation, isn't it? As examples, http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python?rq=1 But trouble is that those examples for **digits**, but my string is **symbolic hex**. – Fruitty Dec 03 '15 at 22:35
  • 1
    I just implemented what's in your question using textual operations rather than bitwise operations. If what I said is not what you need then your question is wrong. Perhaps you could make the question clearer. – David Heffernan Dec 03 '15 at 22:39
  • Probably, it also should be noted for your answer, what if `str` has, as You said, "fewer than 8 digits", or symbols, i.e. `len(str)<=8`, where `str` is a list (`str=[]`), it doesn't mean, what `str` <= 8 **bits**, i.e. `size_of(str)`. But there in the question not digit or other symbol, but **8th left bit** is mentioned ! – Fruitty Dec 03 '15 at 22:51
  • I don't think you really understand any of this. `that_string` is a string as I understand it. Do you know what a slice is? – David Heffernan Dec 03 '15 at 22:59
  • Dear Mr.Heffernan, I do really appreciate Your attempts ! But it looks, like either you don't read my previous comments, or you're trying to answer another question; Why does slice here ?! As it is said at the question, `that_string` is **hex**. So You wrote at your answer: `str = '0123456789abcdef' str[-8:] = '89abcdef'` where `str` is `that_string` and `str[-8:]` is Your solution, that is `original_string`. Then conditionally `str == hex(str[-8:]) AND str == '0123456789abcdef'` (and 64-bit at the same time) and `str[-8:]` should be 32-bit. Do You think `'89abcdef'` could be a solution ? – Fruitty Dec 03 '15 at 23:47
  • Taking the last 8 digits of a hex representation is the same as masking with `0xffffffff`. – David Heffernan Dec 04 '15 at 07:03
  • Yes, sure. So, how it could be done? As I wrote previously: `that_string & 0xFFFFFFFF` doesn't work for **non-numeric** `that_string` in Python. Could you provide a code example? – Fruitty Dec 07 '15 at 01:24
  • I already did provide a code sample that takes the final 8 digits of the hex string – David Heffernan Dec 07 '15 at 07:07
  • But that wasn't an answer. Finally, I found the solution, for which my adviser was looking for, and, yes, as I said above, it's nothing about str[-8:], off course! I will provide a code in next answer, if it's curious. – Fruitty Jan 13 '16 at 15:58
  • It seems that your actual problem differed from the question that you asked. I suggest that you just delete this question and move on. Nobody is going to learn anything from this post. – David Heffernan Jan 13 '16 at 16:04