Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cpjd.hackemos;
- import java.util.Scanner;
- import java.util.logging.Level;
- import org.apache.commons.logging.LogFactory;
- import com.gargoylesoftware.htmlunit.BrowserVersion;
- import com.gargoylesoftware.htmlunit.WebClient;
- import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
- import com.gargoylesoftware.htmlunit.html.HtmlButton;
- import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
- import com.gargoylesoftware.htmlunit.html.HtmlDivision;
- import com.gargoylesoftware.htmlunit.html.HtmlForm;
- import com.gargoylesoftware.htmlunit.html.HtmlInput;
- import com.gargoylesoftware.htmlunit.html.HtmlPage;
- import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
- import com.gargoylesoftware.htmlunit.html.HtmlSpan;
- import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
- import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
- // This class simulates a browser and manages the connection
- public class Main implements Runnable {
- private Thread thread;
- private boolean running;
- private WebClient webClient;
- private HtmlPage toHack;
- public Main() {
- toHack = connect();
- running = true;
- thread = new Thread(this);
- thread.start();
- }
- // Manges the main extraction and injection loop
- public void run() {
- while(running) {
- if(toHack == null) stop();
- // Run the injection / extraction logic
- // First, get the problem from the server
- HtmlSpan pronoun = (HtmlSpan) toHack.getByXPath("//*[@id=\"pronoun-input\"]").get(0);
- HtmlSpan verb = (HtmlSpan) toHack.getByXPath("//*[@id=\"verb-input\"]").get(0);
- // Get the answer here from daniel's code
- Problem p = new Problem(verb.asText());
- String answer = p.Answer(pronoun.asText());
- System.out.println("Problem: "+pronoun.asText()+", verb: "+verb.asText());
- System.out.println("Answer:["+answer+"]");
- // Inject the answer
- HtmlInput toInject = (HtmlInput) toHack.getHtmlElementById("answer-input");
- HtmlButtonInput button = (HtmlButtonInput) toHack.getByXPath("//*[@id=\"check-button\"]").get(0);
- toInject.setValueAttribute(answer);
- System.out.println(toHack.asText());
- try {
- toHack = button.click();
- toInject.setValueAttribute("");
- Thread.sleep(1000);
- } catch(Exception e) {
- System.err.println("Could not check the answer. "+e.getMessage());
- stop();
- }
- // Get the score
- System.out.println("Score: "+toHack.asText());
- }
- }
- public void stop() {
- webClient.close();
- try {
- thread.join();
- } catch(Exception e) {
- System.err.println("Could not stop the thread.");
- }
- System.exit(0);
- }
- private HtmlPage connect() {
- try {
- webClient = new WebClient(BrowserVersion.FIREFOX_38);
- LogFactory.getFactory().setAttribute("org.apache.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
- java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
- java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
- // First navigate to the login webpage
- HtmlPage page = webClient.getPage(Constants.LOGIN_PAGE);
- webClient.getOptions().setJavaScriptEnabled(true);
- // Submit a password and username and continue to login
- System.out.println("Beginning logon injection......");
- HtmlForm form = page.getForms().get(0);
- HtmlTextInput username = form.getInputByName("identity");
- HtmlPasswordInput password = form.getInputByName("password");
- HtmlSubmitInput submit = form.getInputByName("submit");
- username.setValueAttribute(Constants.USER);
- password.setValueAttribute(Constants.PASSWORD);
- HtmlPage loggedIn = submit.click();
- System.out.println("Logon injection successful, connecting to activity page......");
- // Progress through the forms to the testing page
- HtmlAnchor activity = loggedIn.getAnchorByHref("/verb/1026676");
- HtmlPage nextPage = activity.click();
- HtmlDivision div = (HtmlDivision) nextPage.getByXPath("//*[@id=\"practice\"]").get(0);
- HtmlPage clicked = div.click();
- HtmlButton start = (HtmlButton) clicked.getByXPath("//*[@id=\"start-button\"]").get(0);
- HtmlPage started = start.click();
- return started;
- } catch(Exception e) {
- System.err.println("Could not connection to conjuegemos. "+e.getMessage());
- }
- return null;
- }
- // Manages the initial connection
- public static void main(String[] args) {
- Scanner cmd = new Scanner(System.in);
- System.out.println("Welcome to HACKEMOS by Will & Daniel. After updating values, type 'start' to begin a connection.");
- String result = cmd.next();
- if(result.equals("start")) {
- System.out.println("Command 'start' found. Hacking the mainframe.......");
- new Main();
- }
- cmd.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement