问题
I'm having trouble decoding a ASP.NET view state string in Python 3.
When I try decoding the string using bash's base64
command, it decodes the string successfully and I'm able to see all the information I need (most of it is in Hebrew, meaning UTF-8). The view state is of course base64-encoded only and not encrypted.
However, when I try do decode the string using Python's base64
library and then decoding the byte array to a UTF-8 string, I get an error message:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
I should mention that since the string is a view state, the first few bytes are binary data and "0xff" makes sense, however after these bytes the data is readable.
Python 3 code segment:
b = "The_ViewState"
print(base64.b64decode(b).decode("utf-8"))
Why does decoding work in bash and not in Python? How can this be resolved?
回答1:
After a little bit of research I found the answer:
b = "The_ViewState"
print(base64.b64decode(b).decode("utf-8", "ignore"))
Adding the "ignore" flag causes decode()
to discard any invalid byte sequences, thus leaving the irrelevant bytes out of the decoded string.
回答2:
Best way is use this link.
A small Python 3.5+ library for decoding ASP.NET viewstate.
First install that: pip install viewstate
>>> from viewstate import ViewState
>>> base64_encoded_viewstate = '/wEPBQVhYmNkZQ9nAgE='
>>> vs = ViewState(base64_encoded_viewstate)
>>> vs.decode()
('abcde', (True, 1))
来源:https://stackoverflow.com/questions/29285126/decoding-a-viewstate-string-with-utf-8-in-python-3