Skip to main content
summaryrefslogtreecommitdiffstats
blob: 42222e66b4c71506b280cb76c04a83eb635fcae9 (plain) (blame)
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
package org.eclipse.jetty.nested;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.CountDownLatch;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.TypeUtil;

public class TestServlet extends HttpServlet
{

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(final HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        resp.setContentType("text/plain");
        final PrintStream out = new PrintStream(resp.getOutputStream());
        
        out.println("Try out evil things.");

        try
        {
            out.println("\nList home dir...");
            for (File f : new File("/home").listFiles())
                out.println(f);
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        try
        {
            out.println("\nList tmp dir...");
            for (File f : new File("/var/tmp").listFiles())
                out.println(f);
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        
        try
        {
            out.println("\nCreate a /var/tmp file...");
            File file = new File("/var/tmp/eviltest");

            out.println(file+" exists="+file.exists());
            file.createNewFile();
            file.deleteOnExit();
            out.println(file+" exists="+file.exists());
            file.delete();
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        

        try
        {
            out.println("\nOpen a localhost server socket ...");
            
            ServerSocket socket = new ServerSocket();
            socket.bind(new InetSocketAddress("localhost",0));
            out.println("local port = "+socket.getLocalPort());
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        
        try
        {
            out.println("\nOpen a any server socket ...");
            
            ServerSocket socket = new ServerSocket();
            socket.bind(new InetSocketAddress(0));
            out.println("local port = "+socket.getLocalPort());
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        try
        {
            out.println("\nTalk to any server socket ...");
            
            final ServerSocket server = new ServerSocket();
            server.bind(new InetSocketAddress(0));
            out.println("local port = "+server.getLocalPort());
            final int port = server.getLocalPort();
            
            final CountDownLatch latch = new CountDownLatch(1);
            
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        Socket inbound = server.accept();
                        out.println("accepted "+inbound);
                        BufferedReader in = new BufferedReader(new InputStreamReader(inbound.getInputStream()));
                        String data= in.readLine();
                        out.println("read "+data);
                    }
                    catch(Throwable e)
                    {
                        e.printStackTrace(out);
                    }
                    finally
                    {
                        latch.countDown();
                    }
                }
            }.start();
            
           
            Socket socket = new Socket("localhost",port); 
            socket.getOutputStream().write("Hello World\n".getBytes());
            
            latch.await();
            socket.close();
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        
        try
        {
            out.println("\nRead to own content ...");
            out.println("Real path / = "+getServletContext().getRealPath("/"));
           
            for (File f : new File(getServletContext().getRealPath("/")).listFiles())
                out.println(f);
            
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
        
        
        try
        {
            out.println("\nWrite own content ...");
            
            File wibble = new File(getServletContext().getRealPath("/wibble.txt"));
            if (!wibble.exists())
                wibble.createNewFile();
            
            for (File f : new File(getServletContext().getRealPath("/")).listFiles())
                out.println(f);
            
        }
        catch(Throwable e)
        {
            e.printStackTrace(out);
        }
     
        out.flush();
        out.close();
    }
    

}

Back to the top