PHP静态分析与跨站脚本检测(三)
ProgramConverter
- convert()
首先利用PhpParser建立一棵parseTree,但是这棵tree只在这里使用而已。利用这棵tree、输入文件以及ProgramConverter对象本身建立TacConverter对象baseTac,然后调用baseTac.convert()。这个baseTac即是可以通过本对象返回的TacConverter属性。然后通过baseTac.getIncludeNodes()获得需要处理的被包含文件。
进入while循环,前提是函数内局部变量goOn为true和没有“-w”参数。该while循环命名为(w1*),设置goOn=false。如果需要处理的包含文件链表不为空,进入第二个while循环(w2*)。通过for循环将所有的CfgNodeInclude结点单独提取出来,如果这个node是需要跳过的(skip),就取下一个node;如果不是直接通过“常量”包含文件的话,就继续取下一个,这个展示不处理;如果以上两条都不满足,调用include()方法,返回一个内部枚举类型IncStatus,重设将要处理链表,继续w2*。使用baseTac处理一下之后,如果不存在non-literal includes就跳出w1*。否则,开始处理non-literal includes。(253:this.baseTac.backpatch();)。使用baseTac中得到的所有方法,包括用户方法和Main方法计算(259 - 260)。建立AliasAnalysis对象,并以之作为参数建立LiteralAnalysis对象,调用analyze()开始分析。然后,获取包含结点设置为将要处理的包含文件链表,进入for循环,逐个检测,如果已经检测过,跳过,否则即为non-literal的包含文名,按条件分别处理。在通过this.include()方法的返回值决定for循环的去向。再通过this.baseTac.assignFunctions(),然后重新设置需要处理的包含链表,继续w1*。
将之前得到的可能会有用的包含文件列表中实际没有用的都去掉。并将literalAnalysis置为null,以节省内存。如果需要使用AliasAnalysis,作出相应处理(361 - 364行),否则,利用baseTac调用replaceGlobals(),将所有函数的局部变量使用相应的全局变量替换,进行type分析,与前边的literal过程分析差不多。然后将functions转换为CfgNode,然后输出统计信息。释放资源,调用baseTac.addSuperGlobalElements()添加全局元素,将节点倒序排列。
- include()
得到应该包含的文件,如果文件不存在,返回NOTFOUND。否则,在allFiles中添加该文件,并将包含与被包含添加到includeGraph中,如果添加成功,对被包含的文件建立parseTree,并建立TacConverter对象,但是这里的对象都只是用来检测里边的include,并将这些CfgNodeInclude添加进从convert()传过来的weComeAfterwards中,以待处理。这样将goOn=true,循环检测。
CfgEdge
inEdges对于CfgNodeIf来说,是指判断条件,而outEdges是结果。对于其他的node有点说不清楚。
InterAnalysis
从这个程序看,将被扫描文件分析之后,得到的主要是TacConverter,由他得到TacFunction,然后再得到CfgNode,一个CfgNode对应一个Context,二者共同组成InteWorkListElemnt。在InterAnalysisInfo中则是每一个Cfgode对应一个InterAnalysisNode,在这个node中,由先前与刚才CfgNode对应的Context对应一个LatticeElement,而Latticelement则分别存放相应的信息。如AliasLatticelement存放MustAliases和MayAliases,而DepLatticeElement则存放TacPlace、DepSet、Vatiable等。
对于test.php来说,通过initTransferFunction()时,在ProgramConverter.convert()中产生了TransferFunctionId,而在checker.analyzeTaint()中则还得到了ConpositeTransferFunction。这时,每个InterAnalysisNode所包含的TransferFunction是确定了的实例,因而下边的transfer方法调用时会调用相应的实例的方法。对于本例,由于TransferFunctionId.transfer()返回的是传入的参数本身,故调用transfer之后得到的outValue与inValue是一样的。仅出现在当analyze()中node为后便三种的时候,第一种没有使用outValue,第二种直接使outValue = inValue。
- initGeneric()
后边的interAnalysisInfo()怎么就有长度了,没搞清楚。通过initTransferFunction()好像能够将genericAnalysisInfo添加数据,试验中是23个。但是 interAnalysisInfo也是23个。因该是这样的,二者指向同一个内存地址,对genericAnalysisInfo添加内容,就使得interAnalysisInfo也有了同样的内容。
- analyze ()
这个方法主要是针对不同的node采取不同的措施。主要分为了5类node
- CfgNodeCall
- CfgNodeExit
- CfgNodeIf
- CfgNodeCallRet
- 其他
通过inValue转换得到outValue,然后得到node的所有outgoing的边(outEdges),将每条边的终点节点即node的继承者successor得到,然后通过当前的context和刚得到的outValue、successor共同增殖(propagete),向analysisNode设置新的 PhiValue,并向workList中添加InterWorkListElement。这里我理解propagate是产生一个InterWorkListElement,因为在analyze中使用的是它。
在analyze()刚开始的时候,workList中只有一个元素InterWorkListElement(this.mainHead, this.mainContext)。
analyze()中通过analysisNode获得的LatticeElement由test.php第一次在ProgramConverter.convert()中时都是TypeLatticeElement,而在Checker中analyzeTaint()时则全是DepLatticeElement。通过调用dump方法可知,前几个的结果即placeToDep为空,而后便则打印出整个Map。
- propagate(Context context, LatticeElement value, CfgNode target)
在analyze()中多数情况下调用都是(当前context,outValue,successor)
通过target得到InterAnalysisInfo中对应的analysisNode,不为null的话,通过context得到target的oldPhiValue,如果oldPhiValue=null,则将其设为所有LatticElementd的初始值,实际上也是null。如果value==oldPhiValue,则说明值没有改变,可以返回了。否则,在lattice中使用value和oldPhiValue计算一个newPhiValue,如果这个newPhiValue与oldPhiValue不同,则将其设置为target的Phivalue,并在workList中添加一个InterWorkListElement以便analyze()中继续分析。
DepClient
- collectSinks()
对于test0225.php而言,对XSS检测得到的functions数目为2,但是sinks数目为5;对SQL检测functions数目为2,sinks数目为1。
首先通过depAnalysis实例得到所有的TacFunction,然后对于每个TacFunction获得Cfg之后对CfgNode排序然后逐个检测。
得到的TacFunction有两个分别为:_main和foo,里边包含的CfgNode分别是21个和3个。而对于XSS检测有5个sink,均为echo,这里不管是否会产生XSS,均作为sink返回。
- findDangerousUninit(DepGraph relevant)
首先找出relevant中的uninit nodes,结果显示在上一个方法中XSS的5个sink中,后两个返回结果都为空。对于不为空的uninitNode,则是找出其父节点Predecessor,如果父结点不为1个,则抛出异常,否则取出这一个父结点,研究两种情况:
- DepGraphOpNode 直接认定为evil function返回。
- DepGraphNormalNode 调用initiallyTainted()方法查看返回值,如果为ALWAYS或者IFRG,则认为是evil function,返回。
- initiallyTainted(TacPlace place)
这里place考虑三种情况:
- Constant(常量) 这种情况下,直接就认为不可能是tainted,返回NEVER
- Variable(变量) 又分两种情况
- superglobals 在某些特定情况下认为是harmless,其余都是可能有害的。详见DepClient.java
- non-superglobals 同上。但是某些跟命令行“-g”选项有关。
- others 都认为是有害的。
TacConverter
- start()
通过传入的PhpTree的root建立起两个CfgNode,作为Cfg的root和exit,然后将tree中的各个node连接起来,将phptree转换成Cfg。
至于上文中提到的test0225.php,文件如下:
php
function foo ()
{
$var = $_GET[''evil''];
return $var;
}
//$a = $_GET[''evil''];
$b = foo ();
//$a=5;
//echo $a;
echo $b;
// $a is 6 here
echo $_GET[''a''];
echo $e;
$x = explode(''_'', $get);
mysql_query($x[0]);
if (true) echo "ABCD";
if ($a == ''a'' && $c == ''d'') echo "dfg";
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
