Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rkn2019;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.cli.CommandLine;
- import org.apache.commons.cli.CommandLineParser;
- import org.apache.commons.cli.DefaultParser;
- import org.apache.commons.cli.HelpFormatter;
- import org.apache.commons.cli.Option;
- import org.apache.commons.cli.Options;
- import org.apache.commons.cli.ParseException;
- public class Proxy extends Thread{
- protected final String dumpPath;
- protected final Map<String, String> contentReplacements, headerReplacements, redirections;
- protected final String jsInjectPath;
- protected final List<String> stripDomains;
- protected final String mitmCertificatePath;
- protected final boolean sopSwitch;
- private Socket client_socket;
- private Socket server_client_socket;
- private InputStream client_send_data;
- private OutputStream client_receive_data;
- private ServerSocket server_socket;
- Map<String, String> http_fields_map;
- public final int LINE_FEED = 0xA;
- public final int CARRIAGE_RETURN = 0xD;
- public final int ASCII_ZERO = 0x30;
- boolean check_connection;
- String header_line;
- boolean parsed = false;
- public Proxy(String dumpPath, String jsInjectPath, Map<String, String> headerReplacements, boolean sopSwitch,
- Map<String, String> contentReplacements, Map<String, String> redirections, List<String> stripDomains,
- String mitmCertificatePath) {
- this.dumpPath = dumpPath;
- this.jsInjectPath = jsInjectPath;
- this.headerReplacements = headerReplacements;
- this.sopSwitch = sopSwitch;
- this.contentReplacements = contentReplacements;
- this.redirections = redirections;
- this.stripDomains = stripDomains;
- this.mitmCertificatePath = mitmCertificatePath;
- }
- public void runProxy() throws Exception {
- // TODO: This is the starting point of your proxy implementation
- server_socket = new ServerSocket(8080);
- while(true)
- {
- /*
- Proxycontroller control_proxy = new Proxycontroller(server_socket, this);
- control_proxy.start();
- */
- this.run();
- }
- }
- @Override
- public void run()
- {
- try
- {
- client_socket = server_socket.accept();
- client_socket.setSoTimeout(10000);
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- try{
- client_send_data = client_socket.getInputStream();
- client_receive_data = client_socket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- }
- check_connection = false;
- ByteArrayOutputStream writer = new ByteArrayOutputStream();
- byte[] reader = new byte[8092];
- try
- {
- int bytes_number = 0;
- http_fields_map = new HashMap<String, String>();
- header_line = "";
- while((bytes_number = client_send_data.read(reader)) != -1)
- {
- writer.write(reader,0,bytes_number);
- if(!parsed)
- {
- if(parseHeader(writer))
- {
- String host = http_fields_map.get("Host");
- parsed = true;
- if(check_connection == false)
- {
- String[] splitted_string = host.split(": ");
- int port = 80;
- if(splitted_string.length == 2)
- {
- host = splitted_string[0].trim();
- port = Integer.parseInt(splitted_string[1].trim());
- }
- }
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- boolean parseHeader(ByteArrayOutputStream string) throws IOException {
- byte[] string_parser = string.toByteArray();
- String[] parse = null;
- boolean check_header_end = false;
- for(int i = string_parser.length-1; i >= 3; i--)
- {
- if(string_parser[i] == LINE_FEED && string_parser[i-1] == CARRIAGE_RETURN
- && string_parser[i-2] == LINE_FEED && string_parser[i-3] == CARRIAGE_RETURN)
- check_header_end = true;
- }
- if(!check_header_end)
- return false;
- InputStream is = null;
- is = new ByteArrayInputStream(string_parser);
- BufferedReader bf = new BufferedReader(new InputStreamReader(is));
- header_line += bf.readLine() + CARRIAGE_RETURN + LINE_FEED;
- if(header_line.split(" ").equals("CONNECT"))
- {
- http_fields_map.put("CONNECT", "present");
- }
- String temp = null;
- while((temp = bf.readLine()) != null && !temp.isEmpty())
- {
- header_line += temp + CARRIAGE_RETURN + LINE_FEED;
- parse = temp.split(": ");
- if(parse.length == 2)
- http_fields_map.put(parse[0].trim(), parse[1].trim());
- header_line += "" + CARRIAGE_RETURN + LINE_FEED;
- }
- if(string_parser[string_parser.length-1] != LINE_FEED || string_parser[string_parser.length-2] != CARRIAGE_RETURN
- || string_parser[string_parser.length-3] != LINE_FEED || string_parser[string_parser.length-4] != CARRIAGE_RETURN
- || string_parser[string_parser.length-5] != ASCII_ZERO)
- return false;
- return true;
- }
- public static void main(String[] args) throws Exception {
- Options options = new Options();
- options.addOption("help", "print this message");
- // header replacement
- options.addOption(Option.builder("header")
- .hasArg()
- .argName("new headerline")
- .desc("Replace header fields of requests and responses")
- .build());
- // SOP switch
- options.addOption(Option.builder("SOP")
- .hasArg(false)
- .desc("If set, the SOP mechanism should be circumvented")
- .build());
- // content replacement
- options.addOption(Option.builder("content")
- .hasArg()
- .argName("regex^replacement")
- .desc("Replace content strings. Also regular expressions should work")
- .build());
- // dump
- options.addOption(Option.builder("dump")
- .hasArg()
- .argName("outfile")
- .desc("Activate dumping to given outfile")
- .build());
- // SSL strip
- options.addOption(Option.builder("strip")
- .hasArg()
- .argName("domain to strip")
- .desc("SSL strip given domain. Is no domain given -> strip all domains")
- .build());
- // JS Injector
- options.addOption(Option.builder("jsinject")
- .hasArg()
- .argName("js file")
- .desc("Inject given JavaScript")
- .build());
- // Redirection
- options.addOption(Option.builder("redirect")
- .hasArg()
- .argName("domain^redirection")
- .desc("Redirect a domain to another")
- .build());
- // Certificates
- options.addOption(Option.builder("mitm")
- .hasArg()
- .argName("rootCA.pfx")
- .desc("Provide root CA certificate with private key")
- .build());
- CommandLineParser parser = new DefaultParser();
- try {
- CommandLine cmd = parser.parse(options, args);
- if (cmd.hasOption("help"))
- throw new ParseException("help");
- ProxyBuilder builder = new ProxyBuilder();
- builder.dumpTo(cmd.getOptionValue("dump", ""));
- builder.injectJS(cmd.getOptionValue("jsinject", ""));
- builder.setMitmCertificate(cmd.getOptionValue("mitm", ""));
- builder.setContentReplacements(cmd.getOptionValues("content"));
- builder.setRedirections(cmd.getOptionValues("redirect"));
- builder.setHeaderReplacements(cmd.getOptionValues("header"));
- builder.setStripDomains(cmd.getOptionValues("strip"));
- builder.setSOP(cmd.hasOption("SOP"));
- Proxy proxy = builder.build();
- proxy.runProxy();
- } catch (ParseException e) {
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp("proxy", options);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement