Advertisement
Guest User

Untitled

a guest
May 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. package rkn2019;
  2.  
  3. import java.io.*;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. import org.apache.commons.cli.CommandLine;
  11. import org.apache.commons.cli.CommandLineParser;
  12. import org.apache.commons.cli.DefaultParser;
  13. import org.apache.commons.cli.HelpFormatter;
  14. import org.apache.commons.cli.Option;
  15. import org.apache.commons.cli.Options;
  16. import org.apache.commons.cli.ParseException;
  17.  
  18. public class Proxy extends Thread{
  19. protected final String dumpPath;
  20. protected final Map<String, String> contentReplacements, headerReplacements, redirections;
  21. protected final String jsInjectPath;
  22. protected final List<String> stripDomains;
  23. protected final String mitmCertificatePath;
  24. protected final boolean sopSwitch;
  25.  
  26. private Socket client_socket;
  27. private Socket server_client_socket;
  28.  
  29.  
  30. private InputStream client_send_data;
  31. private OutputStream client_receive_data;
  32. private ServerSocket server_socket;
  33.  
  34. Map<String, String> http_fields_map;
  35. public final int LINE_FEED = 0xA;
  36. public final int CARRIAGE_RETURN = 0xD;
  37. public final int ASCII_ZERO = 0x30;
  38.  
  39. boolean check_connection;
  40.  
  41. String header_line;
  42.  
  43. boolean parsed = false;
  44.  
  45.  
  46. public Proxy(String dumpPath, String jsInjectPath, Map<String, String> headerReplacements, boolean sopSwitch,
  47. Map<String, String> contentReplacements, Map<String, String> redirections, List<String> stripDomains,
  48. String mitmCertificatePath) {
  49. this.dumpPath = dumpPath;
  50. this.jsInjectPath = jsInjectPath;
  51. this.headerReplacements = headerReplacements;
  52. this.sopSwitch = sopSwitch;
  53. this.contentReplacements = contentReplacements;
  54. this.redirections = redirections;
  55. this.stripDomains = stripDomains;
  56. this.mitmCertificatePath = mitmCertificatePath;
  57. }
  58.  
  59. public void runProxy() throws Exception {
  60. // TODO: This is the starting point of your proxy implementation
  61.  
  62. server_socket = new ServerSocket(8080);
  63.  
  64. while(true)
  65. {
  66. /*
  67. Proxycontroller control_proxy = new Proxycontroller(server_socket, this);
  68.  
  69. control_proxy.start();
  70.  
  71. */
  72. this.run();
  73.  
  74. }
  75. }
  76.  
  77. @Override
  78. public void run()
  79. {
  80.  
  81. try
  82. {
  83. client_socket = server_socket.accept();
  84. client_socket.setSoTimeout(10000);
  85. }
  86. catch(IOException e)
  87. {
  88. e.printStackTrace();
  89. }
  90. try{
  91.  
  92.  
  93. client_send_data = client_socket.getInputStream();
  94. client_receive_data = client_socket.getOutputStream();
  95.  
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. check_connection = false;
  100. ByteArrayOutputStream writer = new ByteArrayOutputStream();
  101. byte[] reader = new byte[8092];
  102.  
  103.  
  104. try
  105. {
  106. int bytes_number = 0;
  107. http_fields_map = new HashMap<String, String>();
  108. header_line = "";
  109. while((bytes_number = client_send_data.read(reader)) != -1)
  110. {
  111. writer.write(reader,0,bytes_number);
  112. if(!parsed)
  113. {
  114. if(parseHeader(writer))
  115. {
  116. String host = http_fields_map.get("Host");
  117. parsed = true;
  118. if(check_connection == false)
  119. {
  120. String[] splitted_string = host.split(": ");
  121. int port = 80;
  122. if(splitted_string.length == 2)
  123. {
  124. host = splitted_string[0].trim();
  125. port = Integer.parseInt(splitted_string[1].trim());
  126. }
  127.  
  128.  
  129.  
  130. }
  131.  
  132. }
  133.  
  134.  
  135. }
  136.  
  137.  
  138.  
  139.  
  140. }
  141. } catch (IOException e) {
  142. e.printStackTrace();
  143. }
  144.  
  145.  
  146. }
  147. boolean parseHeader(ByteArrayOutputStream string) throws IOException {
  148.  
  149.  
  150.  
  151. byte[] string_parser = string.toByteArray();
  152. String[] parse = null;
  153. boolean check_header_end = false;
  154. for(int i = string_parser.length-1; i >= 3; i--)
  155. {
  156.  
  157. if(string_parser[i] == LINE_FEED && string_parser[i-1] == CARRIAGE_RETURN
  158. && string_parser[i-2] == LINE_FEED && string_parser[i-3] == CARRIAGE_RETURN)
  159. check_header_end = true;
  160.  
  161. }
  162. if(!check_header_end)
  163. return false;
  164. InputStream is = null;
  165. is = new ByteArrayInputStream(string_parser);
  166. BufferedReader bf = new BufferedReader(new InputStreamReader(is));
  167. header_line += bf.readLine() + CARRIAGE_RETURN + LINE_FEED;
  168.  
  169. if(header_line.split(" ").equals("CONNECT"))
  170. {
  171. http_fields_map.put("CONNECT", "present");
  172. }
  173. String temp = null;
  174.  
  175.  
  176.  
  177. while((temp = bf.readLine()) != null && !temp.isEmpty())
  178. {
  179.  
  180. header_line += temp + CARRIAGE_RETURN + LINE_FEED;
  181.  
  182. parse = temp.split(": ");
  183. if(parse.length == 2)
  184. http_fields_map.put(parse[0].trim(), parse[1].trim());
  185.  
  186. header_line += "" + CARRIAGE_RETURN + LINE_FEED;
  187.  
  188.  
  189.  
  190. }
  191.  
  192. if(string_parser[string_parser.length-1] != LINE_FEED || string_parser[string_parser.length-2] != CARRIAGE_RETURN
  193. || string_parser[string_parser.length-3] != LINE_FEED || string_parser[string_parser.length-4] != CARRIAGE_RETURN
  194. || string_parser[string_parser.length-5] != ASCII_ZERO)
  195. return false;
  196.  
  197. return true;
  198. }
  199. public static void main(String[] args) throws Exception {
  200. Options options = new Options();
  201. options.addOption("help", "print this message");
  202. // header replacement
  203.  
  204. options.addOption(Option.builder("header")
  205. .hasArg()
  206. .argName("new headerline")
  207. .desc("Replace header fields of requests and responses")
  208. .build());
  209. // SOP switch
  210. options.addOption(Option.builder("SOP")
  211. .hasArg(false)
  212. .desc("If set, the SOP mechanism should be circumvented")
  213. .build());
  214. // content replacement
  215. options.addOption(Option.builder("content")
  216. .hasArg()
  217. .argName("regex^replacement")
  218. .desc("Replace content strings. Also regular expressions should work")
  219. .build());
  220. // dump
  221. options.addOption(Option.builder("dump")
  222. .hasArg()
  223. .argName("outfile")
  224. .desc("Activate dumping to given outfile")
  225. .build());
  226. // SSL strip
  227. options.addOption(Option.builder("strip")
  228. .hasArg()
  229. .argName("domain to strip")
  230. .desc("SSL strip given domain. Is no domain given -> strip all domains")
  231. .build());
  232. // JS Injector
  233. options.addOption(Option.builder("jsinject")
  234. .hasArg()
  235. .argName("js file")
  236. .desc("Inject given JavaScript")
  237. .build());
  238. // Redirection
  239. options.addOption(Option.builder("redirect")
  240. .hasArg()
  241. .argName("domain^redirection")
  242. .desc("Redirect a domain to another")
  243. .build());
  244. // Certificates
  245. options.addOption(Option.builder("mitm")
  246. .hasArg()
  247. .argName("rootCA.pfx")
  248. .desc("Provide root CA certificate with private key")
  249. .build());
  250. CommandLineParser parser = new DefaultParser();
  251. try {
  252. CommandLine cmd = parser.parse(options, args);
  253. if (cmd.hasOption("help"))
  254. throw new ParseException("help");
  255.  
  256. ProxyBuilder builder = new ProxyBuilder();
  257. builder.dumpTo(cmd.getOptionValue("dump", ""));
  258. builder.injectJS(cmd.getOptionValue("jsinject", ""));
  259. builder.setMitmCertificate(cmd.getOptionValue("mitm", ""));
  260. builder.setContentReplacements(cmd.getOptionValues("content"));
  261. builder.setRedirections(cmd.getOptionValues("redirect"));
  262. builder.setHeaderReplacements(cmd.getOptionValues("header"));
  263. builder.setStripDomains(cmd.getOptionValues("strip"));
  264. builder.setSOP(cmd.hasOption("SOP"));
  265.  
  266. Proxy proxy = builder.build();
  267. proxy.runProxy();
  268. } catch (ParseException e) {
  269. HelpFormatter formatter = new HelpFormatter();
  270. formatter.printHelp("proxy", options);
  271. }
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement