PHP静态分析与跨站脚本检测
最近在看PHP静态分析与跨站脚本检测的东西,用的是维也纳大学一个博士生做出来的Pixy,这个东西是开源的,而且也作了好几年了,功能逐渐增强。现在这个3.0.3版本里边有225个程序,Checker是主程序,现在所有结果都是显示在命令行的,如果被检测程序大,结果很多,当然是个问题。而我要做的大概是将其显示到GUI中去,并且改进它本身呈鼓里边一些不足的地方。
从寒假就开始看他的程序,寒假里边没有怎么搞明白,又冷,手生冻疮了。回来以后,从头开始看吧,分析那一部分不是很明白,但是看到后来,检测漏洞的时候,我想暂时不管它存储的结构什么,反正都是Node之类的东西,看他是怎么检测的,有的细节地方那个暂时翻过去,结果感觉比前边analysze部分简单得多了,连那些存储结构什么的都明白些了。
当然也看他的论文,论文换了一个寒假看完,没弄明白,像Cfg这些东西在论文中有,但是显示不出来,没有直观的感觉,不爽。所以这两天忙着弄了个GUI界面来显示这个Cfg控制流图,麻烦了一点,不过总算是出来了,看看好像也没有多大问题,献丑在这里了。另外,本来是自己使用的,有的地方考虑不周也无所谓,自己再调调就行了。
共有3个文件,第一个是Coor.java,保存每个节点的坐标以及其子节点坐标:
package at.ac.tuwien.infosys.www.pixy; import java.util.*; public class Coor { private int x; private int y; private List<Coor> coors; public Coor(int x, int y) { this.coors = new LinkedList<Coor>(); this.x = x; this.y = y; } public int getX() { return this.x; } public int getY() { return this.y; } public List<Coor> getCoors() { return this.coors; } public void addCoor(Coor coor) { this.coors.add(coor); } public boolean equals(Coor coor) { if (coor.getX()==this.x && coor.getY()==y) { return true; } return false; } public boolean contains(Coor c) { for (Coor coor : this.coors) { if (coor.getX()==c.getX() && coor.getY()==c.getY()) { return true; } } return false; } }
第二个是DrawPanel.java,负责画图的组件:
package at.ac.tuwien.infosys.www.pixy; import at.ac.tuwien.infosys.www.pixy.conversion.Cfg; import at.ac.tuwien.infosys.www.pixy.conversion.nodes.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author Administrator */ public class DrawPanel extends JPanel { private java.util.List<CfgNode> cfgList; private java.util.List<Coor> coorList; /** Creates a new instance of OvalJPanel */ public DrawPanel(java.util.List<CfgNode> cfgList, java.util.List<Coor> coorList) { this.cfgList = cfgList; this.coorList = coorList; } //在面板上绘制图形 public void paintComponent(Graphics g) { for (int i=0 ;i<this.cfgList.size() ;i++ ) { CfgNode cfgNode = this.cfgList.get(i); Coor coor = this.coorList.get(i); int x = coor.getX(); int y = coor.getY(); g.setColor(Color.red); g.drawOval(x-50, y-15, 100, 30); g.setColor(Color.blue); g.drawString(cfgNode.toString(), x-30, y-5); g.drawString("Loc :" + String.valueOf(cfgNode.getOrigLineno()), x, y+10); java.util.List<Coor> coors = coor.getCoors(); for (Coor c : coors) { int cx = c.getX(); int cy = c.getY(); g.setColor(Color.black); if (c.equals(coor)) { g.setColor(Color.yellow); } g.drawLine(x, y+15, cx, cy-15); } } } }
第三个是Draw.java,主控制组件,只需要在Checker中调用该类,传以适当参数(Cfg),就可以了。
package at.ac.tuwien.infosys.www.pixy; import at.ac.tuwien.infosys.www.pixy.conversion.Cfg; import at.ac.tuwien.infosys.www.pixy.conversion.nodes.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /** * * @author Administrator */ public class Draw { //声明框架 private JFrame frame = new JFrame("Control Flow Graph"); //声明书签面板 private DrawPanel draw; private Cfg cfg; private Map<CfgNode, Coor> map; private java.util.List<CfgNode> cfgList; private java.util.List<Coor> coorList; private int startX = 50; private int startY = 30; /** Creates a new instance of TabbedJFrame */ public Draw(Cfg cfg) { //this.map = new TreeMap<CfgNode, Coor>(); this.cfgList = new LinkedList<CfgNode>(); this.coorList = new LinkedList<Coor>(); this.cfg = cfg; } public void show() { frame.add(new JScrollPane(new DrawPanel(this.cfgList, this.coorList)), BorderLayout.CENTER); frame.setSize(1000, 1000); frame.setLocation(50, 50); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 将Cfg转换为cfgList和coorList. */ public void convert() { CfgNode node = this.cfg.getHead(); int size = this.cfg.size(); Coor coor = new Coor(startX, startY); this.cfgList.add(node); this.coorList.add(coor); System.out.println(size); java.util.List<CfgNode> noded = new LinkedList<CfgNode>(); for (int i=0 ;i<size ;i++ ) { int n = i; if (noded.contains(node)) { n = -1; for (CfgNode cfgNode : this.cfgList ) { n++; if (noded.contains(cfgNode)) { continue; } node = cfgNode; break; } } noded.add(node);//为了上边的if判断好做,故在此向noded中添加 java.util.List<CfgNode> list = node.getSuccessors(); int len = list.size(); if (len == 0) { continue; } int k = 0; coor = this.coorList.get(this.cfgList.indexOf(node)); startY = coor.getY() + 60; for (CfgNode cfgNode : list) { startX = coor.getX() + k*250; k++; if (this.cfgList.contains(cfgNode)) { Coor c = (Coor)this.coorList.get(this.cfgList.indexOf(cfgNode)); if (!coor.contains(c)) { coor.addCoor(c); } continue; } coor.addCoor(new Coor(startX, startY)); this.cfgList.add(cfgNode); this.coorList.add(new Coor(startX, startY)); } node = list.get(0);//这里取到的CfgNode可能已经分析过了,通过上边的if判断可以从cfgList中另外取一个。 } } public void setStartX(int x) { this.startX = x; } public void setStartY(int y) { this.startY = y; } public int getStartX() { return this.startX; } public int getStartY() { return this.startY; } public void dump() { System.out.println("------------------"); for (int i=0 ; i<this.cfgList.size(); i++) { CfgNode cfgNode = this.cfgList.get(i); Coor coor = this.coorList.get(i); System.out.println(cfgNode.toString() + " "/* + cfgNode.toString()*/ +" " + coor.getX() + " " + coor.getY() + " " + coor.getCoors().size()); } System.out.println("------------------"); } public void dumpMap() { java.util.List<CfgNode> list = this.cfg.dfPreOrder(); System.out.println("******************"); for (CfgNode node : list) { System.out.println(" " + node.toString() + " " + node.getSuccessors().size()); } System.out.println("******************"); } public static void main(String [] args) { // new Draw(); } }
可能这个项目还会做很久,中间会不会有些心得继续放到这个懒得管的空间中来呢,期待着。
本文地址:http://www.phprm.com/code/42524fc16d5f85d537fb00e73eef10b7.html
转载随意,但请附上文章地址:-)