假设您在php中有一个具有函数和所有这些的类。如何检查类外是否没有代码?我试着用PHP编写这个检查器,并用regex和tokens来实现,但对我来说什么都不起作用:/
一个例子
<?php
class example {
var $name;
var $password;
function __construct($name, $password) {
$this->name = $name;
$this->password = $password;
}
----Allowed code----
}
----Not allowed code----
?>
EDIT:(已解决)感谢@user3163495提供的所有信息
下面是我所做的:
1º我尝试使用以下两个函数获取文件中的类名:
function getClass($tokens) {
$clases = array();
for($i = 0; $i < count($tokens); $i++) {
//Tipo del token actual
$tokenName = getTokenName($tokens, $i);
if($tokenName === "T_CLASS") {
//Searchs the name that it has in the file.
return getClassName($tokens, $i);
}
}
return "";
}
function getClassName($tokens, $i) {
$index = $i + 1;
//Line in which is the class inside the file
$lineaClase = getTokenLine($tokens, $i);
//Line to be updated while searching
$lineaTemp = getTokenLine($tokens, $index);
//Type of token to be updated while searching
$tokenName = getTokenName($tokens, $index);
//Searchs in the parsed array for the first class token
while($index < count($tokens) &&
$lineaClase === $lineaTemp &&
($tokenName === "UNKOWN" || $tokenName === "T_WHITESPACE")) {
$index++;
$tokenName = getTokenName($tokens, $index);
$lineaTemp = getTokenLine($tokens, $index);
}
//Returns the name of the class
return getTokenContent($tokens, $index);
}
然后,我在文件的末尾插入PHP代码,以检查它是否只是一个类。另外,我将这个新内容保存在一个名为temp.php
的新文件中,最后通过shell执行该文件,以获得与beginning_of_class:end_of_class
对应的注入代码的回应。这就是我使用@user3163495告诉我的,再次感谢你。
function codeInjection($clase, $contenido) {
//PHP code to inject, thanks to @user3163495
$codigoPHP = "<?php \$class = new ReflectionClass(\"{$clase}\"); \$comienzo = \$class->getStartLine(); \$fin = \$class->getEndLine(); echo \$comienzo . \":\" . \$fin; ?>";
$contenido .= $codigoPHP;
//Creating temp file
file_put_contents("temp.php", $contenido);
//Returning result of execution
return shell_exec("php temp.php");
}
此外,我从token parsed数组中删除了那些位于类的开头和结尾之间的令牌。最后,我遍历数组,寻找与注释、空格等不同的内容。
(变量是西班牙语的,如果你不理解一些变量的意思,请随意询问)
发布于 2018-06-27 04:12:30
如果您想要“扫描”另一个脚本中的可疑文件,以查看类之外是否有任何代码,那么您可以使用PHP语言中的ReflectionClass。
步骤1:获取定义类的文件名
$class = new ReflectionClass("example");
$fileName = $class->getFileName();
步骤2:获取类定义在文件中占据的起始和结束代码行
$startLine = $class->getStartLine();
$endLine = $class->getEndLine();
$numLines = $endLine - $startLine;
步骤3:对在步骤1中获取的文件名使用file_get_contents(),查看在开始行之前或结束行之后是否有禁用代码。你必须测试和处理你得到的东西,因为我不知道getStartLine()和getEndLine()分别把"start“和"end”放在哪里。
我希望你能明白这一点。
下面是从这个答案中提取的一些代码:
https://stackoverflow.com/questions/51050505
复制相似问题