Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package com.cpjd.hackemos;
  2.  
  3. import java.util.Scanner;
  4. import java.util.logging.Level;
  5.  
  6. import org.apache.commons.logging.LogFactory;
  7.  
  8. import com.gargoylesoftware.htmlunit.BrowserVersion;
  9. import com.gargoylesoftware.htmlunit.WebClient;
  10. import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
  11. import com.gargoylesoftware.htmlunit.html.HtmlButton;
  12. import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
  13. import com.gargoylesoftware.htmlunit.html.HtmlDivision;
  14. import com.gargoylesoftware.htmlunit.html.HtmlForm;
  15. import com.gargoylesoftware.htmlunit.html.HtmlInput;
  16. import com.gargoylesoftware.htmlunit.html.HtmlPage;
  17. import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
  18. import com.gargoylesoftware.htmlunit.html.HtmlSpan;
  19. import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
  20. import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
  21.  
  22. // This class simulates a browser and manages the connection
  23. public class Main implements Runnable {
  24.  
  25. private Thread thread;
  26. private boolean running;
  27. private WebClient webClient;
  28. private HtmlPage toHack;
  29.  
  30. public Main() {
  31. toHack = connect();
  32.  
  33. running = true;
  34. thread = new Thread(this);
  35. thread.start();
  36. }
  37.  
  38. // Manges the main extraction and injection loop
  39. public void run() {
  40. while(running) {
  41. if(toHack == null) stop();
  42.  
  43. // Run the injection / extraction logic
  44. // First, get the problem from the server
  45. HtmlSpan pronoun = (HtmlSpan) toHack.getByXPath("//*[@id=\"pronoun-input\"]").get(0);
  46. HtmlSpan verb = (HtmlSpan) toHack.getByXPath("//*[@id=\"verb-input\"]").get(0);
  47.  
  48. // Get the answer here from daniel's code
  49. Problem p = new Problem(verb.asText());
  50. String answer = p.Answer(pronoun.asText());
  51. System.out.println("Problem: "+pronoun.asText()+", verb: "+verb.asText());
  52. System.out.println("Answer:["+answer+"]");
  53.  
  54. // Inject the answer
  55. HtmlInput toInject = (HtmlInput) toHack.getHtmlElementById("answer-input");
  56. HtmlButtonInput button = (HtmlButtonInput) toHack.getByXPath("//*[@id=\"check-button\"]").get(0);
  57. toInject.setValueAttribute(answer);
  58. System.out.println(toHack.asText());
  59.  
  60. try {
  61. toHack = button.click();
  62. toInject.setValueAttribute("");
  63. Thread.sleep(1000);
  64. } catch(Exception e) {
  65. System.err.println("Could not check the answer. "+e.getMessage());
  66. stop();
  67. }
  68.  
  69. // Get the score
  70. System.out.println("Score: "+toHack.asText());
  71.  
  72. }
  73. }
  74.  
  75. public void stop() {
  76. webClient.close();
  77. try {
  78. thread.join();
  79. } catch(Exception e) {
  80. System.err.println("Could not stop the thread.");
  81. }
  82. System.exit(0);
  83. }
  84.  
  85. private HtmlPage connect() {
  86. try {
  87. webClient = new WebClient(BrowserVersion.FIREFOX_38);
  88. LogFactory.getFactory().setAttribute("org.apache.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
  89. java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
  90. java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
  91.  
  92. // First navigate to the login webpage
  93. HtmlPage page = webClient.getPage(Constants.LOGIN_PAGE);
  94. webClient.getOptions().setJavaScriptEnabled(true);
  95.  
  96. // Submit a password and username and continue to login
  97. System.out.println("Beginning logon injection......");
  98. HtmlForm form = page.getForms().get(0);
  99. HtmlTextInput username = form.getInputByName("identity");
  100. HtmlPasswordInput password = form.getInputByName("password");
  101. HtmlSubmitInput submit = form.getInputByName("submit");
  102. username.setValueAttribute(Constants.USER);
  103. password.setValueAttribute(Constants.PASSWORD);
  104. HtmlPage loggedIn = submit.click();
  105. System.out.println("Logon injection successful, connecting to activity page......");
  106.  
  107. // Progress through the forms to the testing page
  108. HtmlAnchor activity = loggedIn.getAnchorByHref("/verb/1026676");
  109. HtmlPage nextPage = activity.click();
  110. HtmlDivision div = (HtmlDivision) nextPage.getByXPath("//*[@id=\"practice\"]").get(0);
  111. HtmlPage clicked = div.click();
  112. HtmlButton start = (HtmlButton) clicked.getByXPath("//*[@id=\"start-button\"]").get(0);
  113. HtmlPage started = start.click();
  114. return started;
  115. } catch(Exception e) {
  116. System.err.println("Could not connection to conjuegemos. "+e.getMessage());
  117. }
  118. return null;
  119. }
  120.  
  121. // Manages the initial connection
  122. public static void main(String[] args) {
  123. Scanner cmd = new Scanner(System.in);
  124. System.out.println("Welcome to HACKEMOS by Will & Daniel. After updating values, type 'start' to begin a connection.");
  125. String result = cmd.next();
  126. if(result.equals("start")) {
  127. System.out.println("Command 'start' found. Hacking the mainframe.......");
  128. new Main();
  129. }
  130. cmd.close();
  131. }
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement