Java 端口映射辅助

来源:互联网 发布:中国信网域名注册 编辑:程序博客网 时间:2024/04/28 05:43

刚写好这小玩意,发现好像有小伙伴儿在捣乱,博客一直以来都是拿来随意记录下平时的代码什么的。之前都随便托管在一主机商,前段时间买了几个VPS一直没用上,现在就随便找个VPS腾了下。

身处内网,操作什么的很不方便反弹个shell什么的都很麻烦。远控之类的有端口映射功能,可以先在公网IP上开一个端口(如9527),然后反弹shell到目标9527端口。9527端口再转发到自己的1080端口做数据交互,同时用本地程序和数据交互端口1080通信再次交换数据即可。本地再提供一个1080代理,代码如下:

1

RemoteConnector.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
 
public class RemoteConnector extendsThread {
 
    InputStream is;
    OutputStream os;
     
    staticfinal Logger logger = Logger.getLogger(RemoteConnector.class.getName());
     
    publicRemoteConnector() {
    }
 
    publicRemoteConnector(InputStream is, OutputStream os) {
        this.is = is;
        this.os = os;
    }
 
    publicvoid run() {
        InputStream in =null;
        OutputStream out =null;
        try{
            in =this.is;
            byte[] b =new byte[8192];
            intlength;
            out =this.os;
            while((length = in.read(b)) > 0) {
                out.write(b,0, length);
                out.flush();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try{
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.close();
                }
            }catch (Exception e) {
                logger.info(e.toString());
            }
        }
    }
 
    publicvoid startX(){
        newSb1().start();
        newSb2().start();
    }
     
    publicstatic void main(String[] args) {
        newRemoteConnector().startX();
    }
     
    classSb1 extends Thread{
         
        Socket local;
         
        publicvoid startX() throwsException{
            intport = 1080;
            ServerSocket ss =new ServerSocket(port);
            logger.info("监听端口:"+port);
            while(true){
                Socket s = ss.accept();
                String ip = s.getInetAddress().getHostAddress();
                if("127.0.0.1".equals(ip)){
                    logger.info("建立本地连接成功!");
                    this.local = s;
                }else{
                    if(this.local!=null){
                        try{
                            logger.info("开始传输数据到:"+ip);
                            (newRemoteConnector(s.getInputStream(), this.local.getOutputStream())).start();
                            (newRemoteConnector(this.local.getInputStream(), s.getOutputStream())).start();
                        }catch (Exception e) {
                            throwe;//抛出异常,等待重新连接
                        }
                    }
                }
            }
        }
         
        publicvoid run(){
            try{
                startX();
            }catch (Exception e) {
                logger.info(this.getClass()+"-通信异常:"+e.toString()+"\n重新启动....");
            }
        }
         
    }
    classSb2 extends Thread{
         
        publicvoid startX() throwsException{
            intport = 9527;
            String forwardHost ="127.0.0.1";
            intforwardPort = 1080;
            ServerSocket ss =new ServerSocket(port);
            logger.info("监听端口:"+port);
            while(true){
                Socket s = ss.accept();
                String ip = s.getInetAddress().getHostAddress();
                logger.info("主机:"+ip+",连接成功!");
                Socket socket =new Socket(forwardHost, forwardPort);
                logger.info("转发到:"+forwardHost+",端口:"+forwardPort);
                (newRemoteConnector(socket.getInputStream(), s.getOutputStream())).start();
                (newRemoteConnector(s.getInputStream(), socket.getOutputStream())).start();
            }
        }
         
        publicvoid run(){
            try{
                startX();
            }catch (Exception e) {
                logger.info(this.getClass()+"-通信异常:"+e.toString()+"\n重新启动....");
            }
        }
    }
}

LocalConnector.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
 
public class LocalConnector extendsThread {
 
    InputStream is;
    OutputStream os;
     
    staticfinal Logger logger = Logger.getLogger(LocalConnector.class.getName());
 
    publicLocalConnector(InputStream is, OutputStream os) {
        this.is = is;
        this.os = os;
    }
 
    publicvoid run() {
        InputStream in =null;
        OutputStream out =null;
        try{
            in =this.is;
            out =this.os;
            bytebuffer[] = new byte[8192];
            inta;
            while((a = in.read(buffer)) > 0) {
                out.write(buffer,0, a);
                out.flush();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try{
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.close();
                }
            }catch (Exception e) {
                logger.info(e.toString());
            }
        }
         
    }
 
    publicstatic void main(String[] args) {
        try{
            intport = 1080;
            String forwardHost ="23.244.180.74";
            intforwardPort = 1080;
            ServerSocket ss =new ServerSocket(port);
            logger.info("监听端口:"+port);
            while(true){
                try{
                    Socket s = ss.accept();
                    String ip = s.getInetAddress().getHostAddress();
                    logger.info("Host:"+ip+",连接成功!");
                    Socket socket =new Socket(forwardHost, forwardPort);
                    logger.info("转发到:"+forwardHost+",端口:"+forwardPort);
                    (newLocalConnector(socket.getInputStream(), s.getOutputStream())).start();
                    (newLocalConnector(s.getInputStream(), socket.getOutputStream())).start();
                }catch (Exception e) {
                    logger.info(e.toString());
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
0 0
原创粉丝点击