java - HtmlUnit click the submitinput but the page doesn't update -
first post. i'm trying solve htmlunit problem i'm having using dvwa. when try click submit button in doformpost section of code showing me old page text field still full rather new page. have tried literally work, putting wait in until page changes having wait 3 minutes , recheck haven't found issue can possibly be. i'm hoping here may know what's going on. thank 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(); } /** * code showing how can links on given page, , visit 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(); (htmlanchor link : links) { system.out.println("link discovered: " + link.astext() + " @url=" + link.gethrefattribute()); } } /** * code demonstrating techniques submitting html form. fuzzer code need * 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(); (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()); } } /** * method logs user in based on 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 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 login string username = "admin"; string password = "password"; //get forms on 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 log in!"); } } /////////////////////////////////////////////////////////////////////////////////////////// //navigate 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 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 login admin string username = "admin@thebodgeitstore.com' or '1'='1"; //get forms on 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 log in!"); } } } else { system.out.println("invalid login type! \"dvwa\" , \"bodgeit\" logins supported!"); } }
}
it should work simple click on submit button:
htmlsubmitinput submit = (htmlsubmitinput) form.getfirstbyxpath("//input[@value='submit']"); htmlpage mypage = submit.click();
if not can try doing post request programmatically. example here: https://colinhowe.wordpress.com/2009/06/24/htmlunit-how-to-do-a-post/
final webclient webclient = new webclient(); // instead of requesting page directly create webrequestsettings object webrequestsettings requestsettings = new webrequestsettings( new url("url goes here"), httpmethod.post); // set request parameters requestsettings.setrequestparameters(new arraylist()); requestsettings.getrequestparameters().add(new namevaluepair("name of value post", "value")); // finally, can page htmlpage page = webclient.getpage(requestsettings);
Comments
Post a Comment