// Path: src/main/java/burp/HTTPMatcher.java
// public static String getApplicationContext(URL url) {
// 
//     String path = url.getPath();
//     int i = path.indexOf("/", 1);
//     String context = path.substring(0, i + 1);
// 
//     return context;
// }
// 
// Path: src/main/java/burp/j2ee/CustomScanIssue.java
// public class CustomScanIssue implements IScanIssue {
// 
//     private IHttpService httpService;
//     private URL url;
//     private IHttpRequestResponse httpMessages;
//     private String name;
//     private String detail;
//     private Risk severity;
//     private String remedy;
//     private Confidence confidence = Confidence.Certain;
//     
//     public CustomScanIssue(
//             IHttpService httpService,
//             URL url,
//             IHttpRequestResponse httpMessages,
//             String name,
//             String detail,
//             String remedy,
//             Risk severity,
//             Confidence confidence) {
//         this.httpService = httpService;
//         this.url = url;
//         this.httpMessages = httpMessages;
//         this.name = "J2EEScan - " + name;
//         this.detail = detail;
//         this.remedy = remedy;
//         this.severity = severity;
//         this.confidence = confidence;
//     }      
//     
// 
//     @Override
//     public URL getUrl() {
//         return url;
//     }
// 
//     @Override
//     public String getIssueName() {
//         return name;
//     }
// 
//     @Override
//     public int getIssueType() {
//         return 0;
//     }
// 
//     @Override
//     public String getSeverity() {
//         return severity.toString();
//     }
// 
//     @Override
//     // "Certain", "Firm" or "Tentative"
//     public String getConfidence() {
//         return confidence.toString();
//     }
// 
//     @Override
//     public String getIssueBackground() {
//         return null;
//     }
// 
//     @Override
//     public String getRemediationBackground() {
//         return null;
//     }
// 
//     @Override
//     public String getIssueDetail() {
//         return detail;
//     }
// 
//     @Override
//     public String getRemediationDetail() {
//         return remedy;
//     }
// 
//     @Override
//     public IHttpRequestResponse[] getHttpMessages() {
//         return new IHttpRequestResponse[]{httpMessages};
//     }
// 
//     @Override
//     public IHttpService getHttpService() {
//         return httpService;
//     }
// 
// }
// 
// Path: src/main/java/burp/j2ee/IssuesHandler.java
// public class IssuesHandler {
//     
//         
//         /**
//          * 
//          * Verify for a specific protocol - host a specific
//          * issue has been already detected
//          * 
//          * @param callbacks IBurpExtenderCallbacks
//          * @param vulnerabilityName vulnerability to check if it's present into the issues
//          * @param protocol (http/https)
//          * @param host the hostname 
//          * 
//          */
//         public static boolean isvulnerabilityFound(IBurpExtenderCallbacks callbacks, 
//                 String vulnerabilityName,
//                 String protocol,
//                 String host) {
// 
//         IScanIssue[] allIssues;
// 
//         allIssues = callbacks.getScanIssues(protocol + "://" + host);
//         
//         for (IScanIssue a : allIssues) {
// 
//             if (a.getIssueName().contains(vulnerabilityName)) {
//                 return true;
//             }
//         }
//         
//         return false;
//     }
// }


import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.PrintWriter;
import java.net.URL;
import static burp.HTTPMatcher.getApplicationContext;
import burp.CustomHttpRequestResponse;
import burp.IBurpExtenderCallbacks;
import burp.IExtensionHelpers;
import burp.IHttpRequestResponse;
import burp.IRequestInfo;
import burp.IResponseInfo;
import burp.IScanIssue;
import burp.IScannerInsertionPoint;
import burp.j2ee.Confidence;
import burp.j2ee.CustomScanIssue;
import burp.j2ee.IssuesHandler;
import burp.j2ee.Risk;
import burp.j2ee.issues.IModule;

    private final static List<String[]> API = Arrays.asList(
        new String[]{ "JSON", "Liferay\'s default JSON web service", "https://help.liferay.com/hc/en-us/articles/360018151631-JSON-Web-Services <br />" +
                                                                     "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" },
        new String[]{ "AXIS", "Liferay\'s default SOAP web service", "https://help.liferay.com/hc/en-us/articles/360017872492-SOAP-Web-Services <br />" +
                                                                     "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" },
        new String[]{ "Liferay Tunnel", "Liferay tunnel servlet", "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" },
        new String[]{ "WebDAV", "WebDAV allows access to files in a document library on a remote server", 
                                                                     "https://help.liferay.com/hc/en-us/articles/360018172711-Desktop-Access-to-Documents-and-Media <br />" +
                                                                     "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" }
    );

    // List of host and port system already tested
    private static LinkedHashSet<String> hs = new LinkedHashSet<String>();
    
    IExtensionHelpers helpers;
    PrintWriter stderr;

    public List<IScanIssue> scan(IBurpExtenderCallbacks callbacks, IHttpRequestResponse baseRequestResponse, IScannerInsertionPoint insertionPoint) {
        List<IScanIssue> issues = new ArrayList<>();
        helpers = callbacks.getHelpers();
        stderr = new PrintWriter(callbacks.getStderr(), true);
        IRequestInfo reqInfo = helpers.analyzeRequest(baseRequestResponse);

        URL url = reqInfo.getUrl();
        String host = url.getHost(), protocol = url.getProtocol();
        int port = url.getPort();
        Boolean useHttps = protocol.equals("https");
        
        // Check if Liferay has been found
// Path: src/main/java/burp/HTTPMatcher.java
// public static String getApplicationContext(URL url) {
// 
//     String path = url.getPath();
//     int i = path.indexOf("/", 1);
//     String context = path.substring(0, i + 1);
// 
//     return context;
// }
// 
// Path: src/main/java/burp/j2ee/CustomScanIssue.java
// public class CustomScanIssue implements IScanIssue {
// 
//     private IHttpService httpService;
//     private URL url;
//     private IHttpRequestResponse httpMessages;
//     private String name;
//     private String detail;
//     private Risk severity;
//     private String remedy;
//     private Confidence confidence = Confidence.Certain;
//     
//     public CustomScanIssue(
//             IHttpService httpService,
//             URL url,
//             IHttpRequestResponse httpMessages,
//             String name,
//             String detail,
//             String remedy,
//             Risk severity,
//             Confidence confidence) {
//         this.httpService = httpService;
//         this.url = url;
//         this.httpMessages = httpMessages;
//         this.name = "J2EEScan - " + name;
//         this.detail = detail;
//         this.remedy = remedy;
//         this.severity = severity;
//         this.confidence = confidence;
//     }      
//     
// 
//     @Override
//     public URL getUrl() {
//         return url;
//     }
// 
//     @Override
//     public String getIssueName() {
//         return name;
//     }
// 
//     @Override
//     public int getIssueType() {
//         return 0;
//     }
// 
//     @Override
//     public String getSeverity() {
//         return severity.toString();
//     }
// 
//     @Override
//     // "Certain", "Firm" or "Tentative"
//     public String getConfidence() {
//         return confidence.toString();
//     }
// 
//     @Override
//     public String getIssueBackground() {
//         return null;
//     }
// 
//     @Override
//     public String getRemediationBackground() {
//         return null;
//     }
// 
//     @Override
//     public String getIssueDetail() {
//         return detail;
//     }
// 
//     @Override
//     public String getRemediationDetail() {
//         return remedy;
//     }
// 
//     @Override
//     public IHttpRequestResponse[] getHttpMessages() {
//         return new IHttpRequestResponse[]{httpMessages};
//     }
// 
//     @Override
//     public IHttpService getHttpService() {
//         return httpService;
//     }
// 
// }
// 
// Path: src/main/java/burp/j2ee/IssuesHandler.java
// public class IssuesHandler {
//     
//         
//         /**
//          * 
//          * Verify for a specific protocol - host a specific
//          * issue has been already detected
//          * 
//          * @param callbacks IBurpExtenderCallbacks
//          * @param vulnerabilityName vulnerability to check if it's present into the issues
//          * @param protocol (http/https)
//          * @param host the hostname 
//          * 
//          */
//         public static boolean isvulnerabilityFound(IBurpExtenderCallbacks callbacks, 
//                 String vulnerabilityName,
//                 String protocol,
//                 String host) {
// 
//         IScanIssue[] allIssues;
// 
//         allIssues = callbacks.getScanIssues(protocol + "://" + host);
//         
//         for (IScanIssue a : allIssues) {
// 
//             if (a.getIssueName().contains(vulnerabilityName)) {
//                 return true;
//             }
//         }
//         
//         return false;
//     }
// }


// Path: src/main/java/burp/j2ee/issues/impl/LiferayAPI.java
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.PrintWriter;
import java.net.URL;
import static burp.HTTPMatcher.getApplicationContext;
import burp.CustomHttpRequestResponse;
import burp.IBurpExtenderCallbacks;
import burp.IExtensionHelpers;
import burp.IHttpRequestResponse;
import burp.IRequestInfo;
import burp.IResponseInfo;
import burp.IScanIssue;
import burp.IScannerInsertionPoint;
import burp.j2ee.Confidence;
import burp.j2ee.CustomScanIssue;
import burp.j2ee.IssuesHandler;
import burp.j2ee.Risk;
import burp.j2ee.issues.IModule;


    private final static List<String[]> API = Arrays.asList(
        new String[]{ "JSON", "Liferay\'s default JSON web service", "https://help.liferay.com/hc/en-us/articles/360018151631-JSON-Web-Services <br />" +
                                                                     "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" },
        new String[]{ "AXIS", "Liferay\'s default SOAP web service", "https://help.liferay.com/hc/en-us/articles/360017872492-SOAP-Web-Services <br />" +
                                                                     "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" },
        new String[]{ "Liferay Tunnel", "Liferay tunnel servlet", "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" },
        new String[]{ "WebDAV", "WebDAV allows access to files in a document library on a remote server", 
                                                                     "https://help.liferay.com/hc/en-us/articles/360018172711-Desktop-Access-to-Documents-and-Media <br />" +
                                                                     "https://help.liferay.com/hc/en-us/articles/360017872472-Service-Security-Layers" }
    );

    // List of host and port system already tested
    private static LinkedHashSet<String> hs = new LinkedHashSet<String>();
    
    IExtensionHelpers helpers;
    PrintWriter stderr;

    public List<IScanIssue> scan(IBurpExtenderCallbacks callbacks, IHttpRequestResponse baseRequestResponse, IScannerInsertionPoint insertionPoint) {
        List<IScanIssue> issues = new ArrayList<>();
        helpers = callbacks.getHelpers();
        stderr = new PrintWriter(callbacks.getStderr(), true);
        IRequestInfo reqInfo = helpers.analyzeRequest(baseRequestResponse);

        URL url = reqInfo.getUrl();
        String host = url.getHost(), protocol = url.getProtocol();
        int port = url.getPort();
        Boolean useHttps = protocol.equals("https");
        
        // Check if Liferay has been found
        if (!IssuesHandler.isvulnerabilityFound(callbacks,