0

first post. I'm trying to solve an HtmlUnit problem I'm having using the dvwa. When I try to click the submit button in the doFormPost section of the code it is showing me the old page with the text field still full rather than the new page. I have tried literally everything to get this work, from putting a wait in until the page changes to having it wait 3 minutes and then recheck but haven't found what my issue can possibly be. I'm hoping someone here may know what's going on. Thank you in advance.

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 = "[email protected]' 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!");
    }

}

}

1 Answer 1

0

It should work with a simple click on submit button:

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

If not you can try doing a POST request programmatically. Example from here: 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);
3
  • I did try that a couple of times, but for some reason it didn't work. I was troubleshooting this problem for about 8 hours and tried everything I could find online as well as everything I could think of... I think it may have to do with a problem with the site, or possibly the version of HtmlUnit I'm using :/ Mar 20, 2015 at 3:43
  • it works now? Post some stack trace to get helped. cya
    – Adrien
    Mar 23, 2015 at 17:12
  • It sadly doesn't work, it was for a school project, I ended up handing it in in a non-working state, still haven't gotten the grade yet, so I don't know if it was a problem with my environment or the code itself, I will post when I find out. Apr 19, 2015 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.