Java language build white box static code, black box website plug-in automatic security audit platform

Source: Internet
Author: User

Recently intends to do a plug-in white box static code security audit Automation platform and black box Web site security Audit Automation platform. Now open source or semi-open-source do black box site security scanning platform, mostly based on Python script, security personnel contribute Python script plug-in enhanced platform functionality. Be familiar with the Java language for yourself or the developers around you, and plan to use Java to write a platform for later maintenance. In addition, the White-box code security scanning has fortify and other fee-based software, or rely on PMD for code analysis, but such as new security issues, need to customize or extend the more troublesome.

For example, a simple: Now with struts2 vulnerabilities, now need to upgrade to 2.3.28, so I write a plugin, Judge Pom.xml if there is configuration struts2, as long as the version is not 2.8.2 is considered to be a vulnerability. The platform provides the code to download the SVN or git or other server, and then executes the plugin to scan which items are problematic.

The project in recent days with colleagues to do a preliminary version, ideas and imperfect place, as a later reference, the main purpose is to build a can use the shelf out.

To provide a plug-in first, you need to have a separate jar project as a plugin-dependent interface project, it needs to define the interface of the plug-in and interactive objects, the interface requires at least 2 methods: Verify that the plug-in can be scanned, perform a plug-in logic scan, such as the White box Scan plugin interface is defined as follows:

Public interface Iwhiteboxservice {/** * Verify that the file meets the requirements of the rule *  * @param inputfile *            file Information scanned * @return Validation results */validatere Sult Validate (Inputfile inputfile);/** * If validate authentication is passed, start scanning *  * @param inputfile *            scanned File information * @return Scan results */lis T<scanresult> Scan (Inputfile inputfile);}

The

Then needs to have a unified service that invokes the plug-in to perform a code scan. You only need to invoke the service through a Web site or a scheduled task. You can do scanning tasks to scan only which plug-ins, such as scanning all plug-in code:

public void Scan (whitetask task) {//todo: Loop plug-in scan. or late based on task and plug-in close table scan if (null = = Task | | Stringutil.isnull (Task.getcodepath ())) {//todo: Logging, Task table has a problem return;} /* * 1, read the Whiteplugin table all plug-ins and cache plug-in corresponding code, without each reflection * 2, Loop Task.getcodepath () path all files, default Java file, according to Getfileext () File type and getexcluderule () excluded files * According to the file stored in the list after the loop, the elimination of the file is to reduce the contents of the list * 3, according to the Loop list results recycle all files, call the plugin code to scan * 4, Scan results Insert Whitescanresult table * 5, after the loop is complete, modify the current task of the Whitetask table scan complete */list<whiteplugin> allplugins = Whitepluginservice.getall (); if (null = = Allplugins | | allplugins.size () < 1) {//todo: Logging, no plug-in available return;} list<whiteplugindata> Allplugindatas = null;if (null! = allplugins) {Allplugindatas = Whiteplugingenerator.generate (allplugins);} if (null = = Allplugindatas | | allplugindatas.size () < 1) {//todo: Logging, plugin, conversion failed return;} list<filedata> Filedatas = Filegenerator.generate (Task.getcodepath (), Task.getfileext (), Task.getExcluderule ( ));d Oscan (Filedatas, Allplugindatas, Task);} 

Doscan method for true scanning:

public void Doscan (list<filedata> filedatas, list<whiteplugindata> Allplugindatas, Whitetask Task) {if ( Null = = Filedatas | | Null = = Allplugindatas | | Filedatas.size () < 1 | | Allplugindatas.size () < 1) {return;} for (int i = 0; i < filedatas.size (); i++) {//loops each file calls each plug-in filedata tempfiledata = Filedatas.get (i); for (int j = 0; J &L T Allplugindatas.size (); J + +) {//loop each plug-in iwhiteboxservice Whiteboxservice = Allplugindatas.get (j). Getwhiteboxservice (); Inputfile InputFile = new Inputfile (); Inputfile.setcontents (Tempfiledata.getcontent ()); Inputfile.setfileext (Fileutil.getext ( Tempfiledata.getfilename ())); Inputfile.setfilename (Tempfiledata.getfilename ());//todo: The absolute path is passed here, Not packet path Inputfile.setpackagepath (Tempfiledata.getpath ()); list<inputfilecontent> inputfilecontents = getfilecontents (Tempfiledata.getcontent ()); Inputfile.setfilecontents (inputfilecontents); Validateresult Validateresult = whiteboxservice.validate (inputfile); if (null! = Validateresult && ValidateresulT.isissuccess ()) {list<scanresult> scanresults = Whiteboxservice.scan (inputfile); if (null! = ScanResults & & Scanresults.size () > 0) {//Insert database for (int z = 0; z < scanresults.size (); z++) {Whitescanresult Whitescanresult = Getscanresult (Inputfile, Scanresults.get (z), Allplugindatas.get (j), Task); Whitescanresultservice.save ( Whitescanresult);}}}}}

After the plug-in scan is complete, insert the results into the scan results table. The core table structure includes the task table, the plug-in table, the scan result table, the related Main table fields are as follows:

DROP TABLE IF EXISTS ' vulfeel '. ' Whiteplugin '; CREATE TABLE ' Vulfeel '. ' Whiteplugin ' (' ID ' int (one) not NULL auto_increment, ' name ' varchar ($) ' DEFAULT ' COMMENT ' plugin   Name ', ' type ' tinyint (4) default null COMMENT ' Late extension class ', ' importlevel ' tinyint (4) default null COMMENT ' plugin importance, 1 minor, 2 general, 3 important ', ' classpath ' varchar default NULL COMMENT ' plug-in package path, such as Com.xx.vulfeel.plugin.a ', ' filename ' varchar ($) Default null COMMENT ' class name or jar name, if the plug-in is a jar package name, such as A.jar, otherwise the class name, such as A.class ', ' functions ' varchar, DEFAULT NULL COMMENT ' plugin function description ', ' Version ' varchar ($) default null COMMENT ' plug-in version ', ' refurl ' varchar (+) default NULL COMMENT ' plug-in principle parameter  Test URL and other content ', ' userid ' int (one) default null COMMENT ' plugin provider User ID ', ' createtime ' datetime DEFAULT null, ' remark ' varchar (2000) Default NULL COMMENT ' plugin other remarks ', PRIMARY KEY (' ID ')) engine=innodb DEFAULT charset=utf8 comment= ' white box scan plugin table ';D ROP table IF EX Ists ' Vulfeel '. ' Whitescanresult '; CREATE TABLE ' Vulfeel '. ' Whitescanresult ' (' id ' int (one) ' Not NULL Auto_incremENT, ' taskid ' int (one) default null COMMENT ' Associated task table ID ', ' pluginid ' int (one) default NULL COMMENT ' association problem plugin ID ', ' impactlevel '  tinyint (4) default null COMMENT ' influence degree, 1 minor, 2 general, 3 important, main and importlevel associated with plug-in table ', ' classpath ' varchar ($) Default NULL COMMENT ' Plugin package path, such as Com.xx.vulfeel.plugin.a ', ' filename ' varchar ($) DEFAULT NULL COMMENT ' class name or jar name, if the plugin is a jar package then the jar package name, such as A.jar, otherwise class name, such as A.class ', ' startline ' int (one) default null COMMENT ' problem start code line ', ' stopline ' int (one) default null COMME NT ' problem end code line ', ' createtime ' datetime default NULL, ' remark ' varchar (+) default null COMMENT ' Other remarks ', PRIMARY KEY (' id ' ) Engine=innodb DEFAULT Charset=utf8 comment= ' white box scan results table ';D ROP table IF EXISTS ' Vulfeel '. ' Whitetask '; CREATE TABLE ' Vulfeel '. ' Whitetask ' (' id ' int (one) not null auto_increment, ' name ' varchar ($) ' DEFAULT NULL COMMENT ' task Name ', ' codepath ' varchar ($) default null COMMENT ' code path, not dependent on SVN or git, just need to pass in code address ', ' fileext ' varchar ($) Default null COMMENT ' scanned file format, default Java file, comma separated, for example: Java,js ', ' ExcluderuLe ' varchar ' default null COMMENT ' does not scan the path rule,:: Two colon-delimited regular, for example:. *test::js$::css$ ', ' status ' tinyint (4) default NULL COMM ENT ' mission status, 1 initial task, 2 executing, 3 task normal end, 4 task abnormally ended ', ' userid ' int (one) ' DEFAULT NULL COMMENT ' Association create task User ID ', ' createtime ' datetime defau LT null, ' lastmodifytime ' datetime default NULL, ' remark ' varchar ($) default null COMMENT ' Other remarks ', PRIMARY KEY (' id ') Engine=innodb DEFAULT charset=utf8 comment= ' White box Scan task ';

Once the task is set up, just click Perform scan to scan the results according to the plugin to see the results:

For example, a simple scan of SQL injection plug-in scan results, such as the plug-in code as follows:

public class Sqlinjectplugin implements Iwhiteboxservice {@Overridepublic Validateresult validate (Inputfile inputfile) {//TODO auto-generated method Stubvalidateresult result = new Validateresult (); result.setissuccess (false); if (null = = in Putfile | | Null = = Inputfile.getfilecontents ()) {return result;} if (Inputfile.getpackagepath (). Contains ("DAO")) {result.setissuccess (true);} return result;} @Overridepublic list<scanresult> Scan (inputfile inputfile) {//TODO auto-generated method Stublist<scanresult > result = new arraylist<scanresult> (); Scanresult Scanresult = new Scanresult (); if (null = = Inputfile | | null = = inputfile.getfilecontents ()) {return result;} Pattern pattern = pattern.compile ("(\" [^\ "]+\" |\\w+) \\s* (\\+|\\+=) \\s*\\w+ "), if (Inputfile.getpackagepath (). Contains ("DAO")) {//scan only the package path has a DAO file//loop inputfile each line, if there is a concatenation of SQL to indicate the presence of injection for (int i = 0; i < inputfile.getfilecontents (). Size (); i++) {Inputfilecontent inputfilecontent = inputfile.getfilecontents (). get (i); Matcher MATcher = Pattern.matcher (Inputfilecontent.getcontent ()), if (Matcher.find ()) {Scanresult.setstartline ( Inputfilecontent.getline ()); Scanresult.setstopline (Inputfilecontent.getline ()); Scanresult.setremark ("There is SQL splicing! "); Result.add (Scanresult);}}} return result;}}

Now the project is mainly to build a shelf, web black box scanning shelves are also test, white box scanning is now static code scanning, no use of PMD and other code generation syntax tree analysis, but also very simple. There is time to refine and expand later.

If you need reprint please specify from: http://lawson.cnblogs.com/

Java language build white box static code, black box website plug-in automatic security audit platform

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.