Java编写代理服务器(Burp拦截Demo)一

大家都知道大名鼎鼎的BurpSuite代理神器,对于抓取HTTP请求非常好用,偶然,一朋友问我Java应该如何去编写代理服务器(因为他想做某些东西),有没有相关的API 去实现,我想说,差不多你能想到的,JAVA都可以做到,没有任何一门成熟的语言是垃圾的。

在编写代理服务器之前,首先应该明白一点,Java的代理机制,如图1-1所示。

那么Java就处于中间这层代理服务器,代理服务器所作的事情如下:

1、接收客户端请求,进行处理,然后发送给服务端

2、接收服务端响应,进行处理,然后发送给客户端

这样,就更清晰了,Java给我们提供了代理的API为,java.net.Proxy类。此类表示代理设置,通常为类型(http、socks)和套接字地址。Proxy 是不可变对象。

也就是说Java可以制作高级协议的代理,如 HTTP 或 FTP。也可以制作SOCKS(V4 或 V5)代理。

在基本的概念说完之后,来实际操作一把,分为两个步骤,第一部分,让JAVA程序使用代理服务器,第二步部分,让我们的Java程序像BurpSuite一样,来做一个HTTP的代理服务器吧。

首先,使用到了URL类,HttpURLConnection类及其我们的代理类Proxy类。他们都位于java.net包中。

第一步:生成代理,指定端口为8888:

Proxy proxy = null ;
proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",8888));  // 实例化本地代理对象,端口为8888

第二步:使用URLConnection类进行连接www.moonsos.com

URL url = new URL("http://www.moonsos.com");  //实例化米安网URL类
HttpURLConnection action = (HttpURLConnection)url.openConnection(proxy);  //使用代理打开网页

第三步:打开URL,并且读取HTML源码

HttpURLConnection action = (HttpURLConnection)url.openConnection(proxy);  //使用代理打开网页
InputStream in =action.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
StringBuilder sb = new StringBuilder();
String lin = System.getProperty("line.separator") ;
for(String temp = br.readLine() ; temp!=null;temp = br.readLine() ){
sb.append(temp+lin);
}
br.close();
in.close();
System.out.println(sb);

效果执行图,如图1-2所示。

完整代码示例如下:

import java.net.* ;
import java.io.* ;
public class ProxyTest{
public static void main(String args[])throws Exception{
Proxy proxy = null ;
proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",8888));  // 实例化本地代理对象,端口为8888
URL url = new URL("http://www.moonsos.com");
HttpURLConnection action = (HttpURLConnection)url.openConnection(proxy);  //使用代理打开网页
InputStream in =action.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
StringBuilder sb = new StringBuilder();
String lin = System.getProperty("line.separator") ;
for(String temp = br.readLine() ; temp!=null;temp = br.readLine() ){
sb.append(temp+lin);
}
br.close();
in.close();
System.out.println(sb);
}
}

第一部分我们学会了Java如何使用代理程序,那么第二部分就看Java制作代理服务器。

第一步,生成Socket类,作为代理服务器

ServerSocket server  = new ServerSocket(8888);  //建立本地代理服务器,端口为8888

第二步,等待连接,也就是等待使用代理程序的用户进入,如果没有用户进入那么,将会一直在此等待。

Socket socket =server. accept();   //等待客户端连接

第三步,当用户进来后,查看用户数据发送的请求,这里新做了一个ActionScoket类,多线程,专门用来处理Scoket输入流,代码如下所所示。

ServerSocket server  = new ServerSocket(8888);
while(true){
Socket socket = server.accept();
ActionSocket ap = new ActionSocket(socket);
ap.start();
}
ActionSocket代码如下:
class ActionSocket extends Thread{
private Socket socket = null ;
public ActionSocket(Socket s){
this.socket = s ;
}
public void run(){
try{
this.action() ;
}catch(Exception e){
e.printStackTrace();
}
}
public void action() throws Exception {
if (this.socket == null){
return ;
}
BufferedReader br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
for(String temp = br.readLine() ; temp!=null;temp = br.readLine() ){
System.out.println(temp);
}
br.close();
}
}

完成代码如下:

import java.net.* ;
import java.io.* ;
class ActionSocket extends Thread{
private Socket socket = null ;
public ActionSocket(Socket s){
this.socket = s ;
}
public void run(){
try{
this.action() ;
}catch(Exception e){
e.printStackTrace();
}
}
public void action() throws Exception {
if (this.socket == null){
return ;
}
BufferedReader br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
for(String temp = br.readLine() ; temp!=null;temp = br.readLine() ){
System.out.println(temp);
}
br.close();
}
}
public class ServerPrxoy{
public static void main(String args[])throws Exception{
ServerSocket server  = new ServerSocket(8888);
while(true){
Socket socket = server.accept();
ActionSocket ap = new ActionSocket(socket);
ap.start();
}
}
}

给火狐,搜狗等浏览器配置代理,如图1-3所示:

OK,配置完毕,进行访问http://www.moonsos.com,可以发现我们写的小程序已经能够进行抓取到HTTP协议信息,如图1-4所示。

当获取HTTP请求之后,我想后面的东西就不用说了吧。无非就是对HTTP请求进行分析,封装。然后在时候Socket发送。获取到信息之后,在使用当前的Socket以打印流的方式输出到浏览器。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基本原理: 代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过URL对象建立输入流读取相应的网页内容,最后按照web服务器的工作方式将网页内容发送给用户浏览器 源程序: import java.net.*; import java.io.*; public class MyProxyServer { public static void main(String args[]) { try { ServerSocket ss=new ServerSocket(8080); System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } } }; class process implements Runnable { Socket s; public process(Socket s1) { s=s1; } public void run() { String content=" "; try { PrintStream out=new PrintStream(s.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); String info=in.readLine(); System.out.println("now got "+info); int sp1=info.indexOf(' '); int sp2=info.indexOf(' ',sp1+1); String gotourl=info.substring(sp1,sp2); System.out.println("now connecting "+gotourl); URL con=new URL(gotourl); InputStream gotoin=con.openStream(); int n=gotoin.available(); byte buf[]=new byte[1024]; out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); out.println("Content_Length:"+n); out.println(" "); while ((n=gotoin.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); } catch (IOException e) { System.out.println("Exception:"+e); } } };
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值