繁体   English   中英

HtmlUnit单击submitinput,但页面未更新

[英]HtmlUnit click the submitinput but the page doesn't update

第一篇文章。 我正在尝试解决使用dvwa的HtmlUnit问题。 当我尝试单击代码的doFormPost部分中的Submit按钮时,它向我显示的是文本字段仍为满的旧页面,而不是新页面。 从字面上的等待直到页面更改到让它等待3分钟,然后重新检查,但我并未尝试找出一切可能的解决方法。 我希望这里的人可能知道发生了什么事。 先感谢您。

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class BasicFuzzer {

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    WebClient webClient = new WebClient();
    webClient.setJavaScriptEnabled(true);
    discoverLinks(webClient);
    System.out.println("\n\n\n\n\n\n");
    doFormPost(webClient);
    webClient.closeAllWindows();
}

/**
 * This code is for showing how you can get all the links on a given page, and visit a given URL
 * @param webClient
 * @throws IOException
 * @throws MalformedURLException
 */
private static void discoverLinks(WebClient webClient) throws IOException, MalformedURLException {
    HtmlPage page = webClient.getPage("http://localhost:8080/bodgeit");
    List<HtmlAnchor> links = page.getAnchors();
    for (HtmlAnchor link : links) {
        System.out.println("Link discovered: " + link.asText() + " @URL=" + link.getHrefAttribute());
    }
}

/**
 * This code is for demonstrating techniques for submitting an HTML form. Fuzzer code would need to be
 * more generalized
 * @param webClient
 * @throws FailingHttpStatusCodeException
 * @throws MalformedURLException
 * @throws IOException
 */
private static void doFormPost(WebClient webClient) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    login(webClient,"dvwa");
    HtmlPage page = webClient.getPage("http://127.0.0.1/dvwa/vulnerabilities/sqli/");
    List<HtmlForm> forms = page.getForms();
    for (HtmlForm form : forms) {
        HtmlInput input = form.getInputByName("id");
        input.setValueAttribute("$id=3' OR '1'='1");
        HtmlSubmitInput submit = (HtmlSubmitInput) form.getFirstByXPath("//input[@value='Submit']");
        System.out.println(submit.<HtmlPage> click().getWebResponse().getContentAsString());
    }
}

/**
 * This method logs the user in based on the site URL.
 * 
 * @param loginType
 */
private static void login(WebClient client,String loginType) {

    System.out.println("Login type: " + loginType + "\n");

    //Log in on dwva
    if(loginType.toLowerCase().contentEquals("dvwa")){

        ///////////////////////////////Navigate to page/////////////////////////////////////////////////
        HtmlPage thisPage = null;
        String pagename = "http://127.0.0.1/dvwa/login.php";
        try {
            thisPage = client.getPage(pagename);
        } catch (FailingHttpStatusCodeException e) {
            System.out.println("Failing HTTP Status Code Exception: " + pagename);
            return;
        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception: " + pagename);
            return;
        } catch (IOException e) {
            System.out.println("I/O Exception: " + pagename);
            return;
        }

        //The strings to login
        String username = "admin";
        String password = "password";

        //Get the forms on the page
        List<HtmlForm> forms = thisPage.getForms();
        for(HtmlForm form : forms){

            //Input username
            HtmlInput usernameInput = form.getInputByName("username");
            usernameInput.setValueAttribute(username);

            //Input password
            HtmlInput passwordInput = form.getInputByName("password");
            passwordInput.setValueAttribute(password);

            //Click submit button
            HtmlSubmitInput submit = (HtmlSubmitInput) form.getInputByName("Login");
            try {
                submit.<HtmlPage> click().getWebResponse().getContentAsString();
            } catch (IOException e) {
                System.out.println("Something went wrong when trying to log in!");
            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////
        //Navigate to security page
        thisPage = null;
        pagename = "http://127.0.0.1/dvwa/security.php";
        try {
            thisPage = client.getPage(pagename);
        } catch (FailingHttpStatusCodeException e) {
            System.out.println("Failing HTTP Status Code Exception: " + pagename);
            return;
        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception: " + pagename);
            return;
        } catch (IOException e) {
            System.out.println("I/O Exception: " + pagename);
            return;
        }

        //Change security
        HtmlSelect securitySelect = (HtmlSelect) thisPage.getElementByName("security");
        HtmlOption option = securitySelect.getOptionByValue("low");
        securitySelect.setSelectedAttribute(option, true);

        //Press Submit
        HtmlSubmitInput submitButton = thisPage.getElementByName("seclev_submit");
        try {
            submitButton.click();
        } catch (IOException e) {
            e.printStackTrace();
        }

    //Log in on bodgeit
    } else if(loginType.toLowerCase().contentEquals("bodgeit")){

        //Navigate to page
        HtmlPage thisPage = null;
        String pagename = "http://localhost:8080/bodgeit/login.jsp";
        try {
            thisPage = client.getPage(pagename);
        } catch (FailingHttpStatusCodeException e) {
            System.out.println("Failing HTTP Status Code Exception: " + pagename);
            return;
        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception: " + pagename);
            return;
        } catch (IOException e) {
            System.out.println("I/O Exception: " + pagename);
            return;
        }

        //The string to login as the admin
        String username = "admin@thebodgeitstore.com' or '1'='1";

        //Get the forms on the page
        List<HtmlForm> forms = thisPage.getForms();
        for(HtmlForm form : forms){

            //Input username
            HtmlInput usernameInput = form.getInputByName("username");
            usernameInput.setValueAttribute(username);

            //Click submit button
            HtmlSubmitInput submit = (HtmlSubmitInput) form.getFirstByXPath("//input[@id='submit']");
            try {
                submit.<HtmlPage> click().getWebResponse().getContentAsString();
            } catch (IOException e) {
                System.out.println("Something went wrong when trying to log in!");
            }
        }


    } else {
        System.out.println("Invalid Login type! Only \"dvwa\" and \"bodgeit\" logins are supported!");
    }

}

}

只需单击提交按钮即可使用:

HtmlSubmitInput submit = (HtmlSubmitInput) form.getFirstByXPath("//input[@value='Submit']");
HtmlPage myPage = submit.click();

如果没有,您可以尝试以编程方式发出POST请求。 此处的示例: https : //colinhowe.wordpress.com/2009/06/24/htmlunit-how-to-do-a-post/

Final WebClient webClient = new WebClient();

// Instead of requesting the page directly we create a WebRequestSettings object
WebRequestSettings requestSettings = new WebRequestSettings(
  new URL("URL GOES HERE"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(new ArrayList());
requestSettings.getRequestParameters().add(new NameValuePair("name of value to post", "value"));

// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM