PHP static analysis and cross-station script detection (III.)

Source: Internet
Author: User
The following is my view of some of the procedures in the Pixy record, mainly in order to be able to quickly understand the process of thinking, record down, otherwise so many things light on the brain how to line. There may be flaws in some places, and I may not feel good about it myself.
Programconverter
-Convert ()
First use Phpparser to build a parsetree, but this tree is only used here. Use this tree, the input file, and the Programconverter object itself to establish the Tacconverter object Basetac, and then call Basetac.convert (). This BASETAC is the Tacconverter property that can be returned through this object. The included files that need to be processed are then obtained by Basetac.getincludenodes ().
Enter the while loop, provided that the local variable in the function is goon true and there is no "-w" argument. The while loop is named (w1*), and the Goon=false is set. Enter the second while loop (w2*) if the containing file list to be processed is not empty. All cfgnodeinclude nodes are extracted separately from the For loop, if the node is skipped (skip), remove a node, and if it is not directly through the "constant" containing the file, continue to take down one, the display does not handle; if none of the above two is satisfied , call the Include () method, return an internal enumeration type Incstatus, reset the list that will be processed, and continue w2*. After using BASETAC, if there is no non-literal includes, jump out of w1*. Otherwise, start processing non-literal includes. (253:this.basetac.backpatch ();). Use all of the methods obtained in Basetac, including the user method and the Main method calculation (259-260). The Aliasanalysis object is established and the Literalanalysis object is established as a parameter, and the Analyze () is invoked to start parsing. Then, get a list of included files containing the node set to be processed, enter for loop, test one by one, and if it has been detected, skip it, otherwise it will be the non-literal containing Wennan, treated separately according to the conditions. Determines the whereabouts of the For loop through the return value of the This.include () method. Continue w1* by This.baseTac.assignFunctions (), and then reset the containing list to be processed.
Remove all of the previously available list of included files that might be useful. and set the literalanalysis to null to save memory. If you need to use aliasanalysis, make the corresponding processing (361-364 lines), otherwise, using Basetac invoke Replaceglobals (), the local variables of all functions are replaced with the corresponding global variables, for type analysis, Similar to the literal process in front of the analysis. The functions is then converted to Cfgnode and the statistics are output. Release the resource, call Basetac.addsuperglobalelements () to add the global element, and arrange the nodes in reverse order.
-Include ()
Get the file that should be included, and return NotFound if the file does not exist. Otherwise, add the file in Allfiles and add the inclusion and inclusion to the includegraph, if the addition succeeds, create a parsetree for the included file, and create a Tacconverter object. But the objects here are only used to detect the include inside, and add these cfgnodeinclude to the wecomeafterwards from convert () for processing. This will goon=true, cyclic detection.
Cfgedge
Inedges for Cfgnodeif, it means judging conditions, and outedges is the result. It's a bit unclear about the other node.

interanalysis
From this program look, will be scanned file analysis, get the main is Tacconverter, by him get tacfunction, and then get Cfgnode, a cfgnode corresponding to a context, The two together constitute inteworklistelemnt. In Interanalysisinfo, each cfgode corresponds to a interanalysisnode, in this node, a latticeelement corresponding to the context of the previous Cfgnode, The latticelement, however, holds the corresponding information separately. such as aliaslatticelement storage mustaliases and mayaliases, while Deplatticeelement store Tacplace, Depset, vatiable and so on.
For test.php, the Transferfunctionid is produced in Programconverter.convert () through the inittransferfunction (). and in Checker.analyzetaint () also got conpositetransferfunction. At this point, each interanalysisnode contains a transferfunction that is determined, so the method of the corresponding instance is invoked at the bottom of the transfer method call. For this example, because Transferfunctionid.transfer () returns the passed-in parameter itself, the Outvalue transfer after the call is the same as the invalue. Only now when the node in analyze () three species, the first one did not use Outvalue, the second direct Outvalue = Invalue.
-Initgeneric ()
Behind the Interanalysisinfo () how has the length, did not understand. by Inittransferfunction () it seems that the genericanalysisinfo can be added to the data, the experiment is 23. But Interanalysisinfo is also 23. Because this is the case, the two point to the same memory address, add content to the Genericanalysisinfo, so that Interanalysisinfo also have the same content.
-Analyze ()
This method is mainly aimed at different node to take different measures. Mainly divided into 5 types of node
-Cfgnodecall
-Cfgnodeexit
-Cfgnodeif
-Cfgnodecallret
-Other
The Outvalue is obtained by invalue conversion, and then all the outgoing edges (outedges) of node are obtained, and the endpoint node of each edge is successor to the successor of node. Then, through the current context and the newly obtained outvalue, successor common multiplication (propagete), the new Phivalue is set to Analysisnode, and add interworklistelement to the worklist. I understand here that propagate is producing a interworklistelement because it is used in analyze.
At the beginning of analyze (), there was only one element interworklistelement (This.mainhead, This.maincontext) in worklist.
The latticeelement obtained by Analysisnode in analyze () is programconverter.convert for the first time in Typelatticeelement (), And in the checker Analyzetaint () is all deplatticeelement. By invoking the Dump method, the first few results are PLACETODEP and then the entire map is printed.

-Propagate (context context, latticeelement value, Cfgnode target)
In analyze () Most of the cases are called (current context,outvalue,successor)
By using target to get the corresponding analysisnode in interanalysisinfo, if not NULL, get Target's oldphivalue through the context, if oldphivalue=null, It is set to the initial value of all latticelementd, and is actually null. If Value==oldphivalue, the value is not changed and can be returned. Otherwise, a newphivalue is computed using value and oldphivalue in lattice, and if this newphivalue is different from Oldphivalue, it is set to target phivalue. and add a interworklistelement to the worklist to continue profiling in analyze ().



depclient
-Collectsinks ()
For test0225.php, the number of functions for XSS detection is 2, but the number of sinks is 5, and the number of functions for SQL detection is 2,sinks 1.
First, all the tacfunction are obtained through the depanalysis instance, then the Cfgnode is sorted and then tested for each tacfunction after the CFG is obtained.
There are two tacfunction: _main and foo, which contain 21 and 3 Cfgnode respectively. For XSS detection, there are 5 sink, Echo, which is returned as sink regardless of whether XSS is produced.

-Finddangerousuninit (depgraph relevant)
First find the UnInit nodes in relevant, and the result is shown in the 5 sink of XSS in the last method, and the last two return results are empty. For uninitnode that are not empty, find the parent node predecessor, throw an exception if the parent is not 1, or remove the parent knot, and study two things:
-Depgraphopnode directly identified as evil function return.
-Depgraphnormalnode calls the initiallytainted () method to view the return value and, if it is always or IFRG, the evil function, and returns.

-Initiallytainted (Tacplace place)
Here place considers three kinds of situations:
-Constant (constant) in this case, directly considered impossible to be tainted, returned never
-Variable (variable) is divided into two different cases
-Superglobals is considered to be harmless under certain circumstances and the rest are potentially harmful. Refer to Depclient.java
-Non-superglobals-ditto. But some are related to the command line "-g" option.
-others are considered to be harmful.


Tacconverter
-Start ()
Two cfgnode are established as root and exit of the CFG by the root of the incoming phptree, and then the node in the tree is connected to convert the Phptree into a cfg.
As for the test0225.php mentioned above, the documents are as follows:


? 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 " ;

?>  

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.