3 回复
做得好!
那真正出色的混淆效果怎么样呢?
更多关于基于Golang实现的PHP解析器v0.5版本发布的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
你指的是什么类型的混淆?
如果你问的是基于eval的混淆,我可能要让你失望了
eval(gzinflate(base64_decode(strrev(str_rot13('=RDe1Za8XXAI1A9vlESqYAcHlB5FXFIqmeVI1RwNzYoIC78FEmZmA9ZFlEwZZyplZ3RYgbRHIO7IBalH')))));
解析器将返回函数调用的AST。
转储输出
[*node.Root]
"Stmts":
[*stmt.Expression]
"Expr":
[*expr.Eval]
"Expr":
[*expr.FunctionCall]
"Function":
[*name.Name]
"NamespacedName": gzinflate;
"Parts":
[*name.NamePart]
"Value": gzinflate;
"ArgumentList":
[*node.ArgumentList]
"Arguments":
[*node.Argument]
"Variadic": false;
"IsReference": false;
"Expr":
[*expr.FunctionCall]
"Function":
[*name.Name]
"NamespacedName": base64_decode;
"Parts":
[*name.NamePart]
"Value": base64_decode;
"ArgumentList":
[*node.ArgumentList]
"Arguments":
[*node.Argument]
"IsReference": false;
"Variadic": false;
"Expr":
[*expr.FunctionCall]
"Function":
[*name.Name]
"NamespacedName": strrev;
"Parts":
[*name.NamePart]
"Value": strrev;
"ArgumentList":
[*node.ArgumentList]
"Arguments":
[*node.Argument]
"Variadic": false;
"IsReference": false;
"Expr":
[*expr.FunctionCall]
"Function":
[*name.Name]
"NamespacedName": str_rot13;
"Parts":
[*name.NamePart]
"Value": str_rot13;
"ArgumentList":
[*node.ArgumentList]
"Arguments":
[*node.Argument]
"Variadic": false;
"IsReference": false;
"Expr":
[*scalar.String]
"Value": '=RDe1Za8XXAI1A9vlESqYAcHlB5FXFIqmeVI1RwNzYoIC78FEmZmA9ZFlEwZZyplZ3RYgbRHIO7IBalH';
否则,它必须能够解析任何有效的PHP代码。
这是一个非常令人兴奋的项目!基于Golang实现的PHP解析器为开发者提供了在Go生态中处理PHP代码的能力,这对于需要跨语言分析、转换或处理PHP代码的工具开发非常有价值。
以下是该解析器的一些典型应用场景和示例代码:
基本使用示例
package main
import (
"fmt"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/visitor"
)
func main() {
// 解析PHP代码
src := `<?php
function hello($name) {
return "Hello, " . $name;
}
echo hello("World");
?>`
parser := php7.NewParser([]byte(src), "7.4")
parser.Parse()
// 获取抽象语法树
rootNode := parser.GetRootNode()
// 遍历AST
dumper := &visitor.Dumper{
Writer: os.Stdout,
Indent: " ",
Comments: parser.GetComments(),
Positions: parser.GetPositions(),
}
rootNode.Accept(dumper)
}
提取函数信息
package main
import (
"fmt"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/visitor"
)
type FunctionExtractor struct {
visitor.Null
Functions []string
}
func (f *FunctionExtractor) StmtFunction(n *stmt.Function) {
functionName := n.FunctionName.(*node.Identifier).Value
f.Functions = append(f.Functions, functionName)
}
func extractFunctions(phpCode string) []string {
parser := php7.NewParser([]byte(phpCode), "7.4")
parser.Parse()
extractor := &FunctionExtractor{}
parser.GetRootNode().Accept(extractor)
return extractor.Functions
}
代码转换示例
package main
import (
"bytes"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/printer"
)
func convertPHPToGoLike(phpCode string) string {
parser := php7.NewParser([]byte(phpCode), "7.4")
parser.Parse()
// 创建自定义打印机
var buf bytes.Buffer
p := printer.NewPrinter(&buf)
// 打印转换后的代码
parser.GetRootNode().Accept(p)
return buf.String()
}
分析PHP文件结构
package main
import (
"fmt"
"io/ioutil"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/visitor"
)
type StructureAnalyzer struct {
visitor.Null
Classes []string
Functions []string
Namespaces []string
}
func (s *StructureAnalyzer) StmtClass(n *stmt.Class) {
className := n.ClassName.(*node.Identifier).Value
s.Classes = append(s.Classes, className)
}
func (s *StructureAnalyzer) StmtFunction(n *stmt.Function) {
functionName := n.FunctionName.(*node.Identifier).Value
s.Functions = append(s.Functions, functionName)
}
func analyzePHPFile(filename string) {
content, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
parser := php7.NewParser(content, "7.4")
parser.Parse()
analyzer := &StructureAnalyzer{}
parser.GetRootNode().Accept(analyzer)
fmt.Printf("Classes: %v\n", analyzer.Classes)
fmt.Printf("Functions: %v\n", analyzer.Functions)
}
这个解析器为Go开发者提供了强大的PHP代码处理能力,特别适合构建代码分析工具、迁移工具或需要同时处理PHP和Go代码的应用程序。v0.5版本的发布意味着该项目正在趋于成熟,值得关注和试用。

