This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
SessionIDAnalysis.java
329 lines (284 loc) · 11 KB
/
SessionIDAnalysis.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
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
/***********************************************************************
*
* $CVSHeader$
*
* This file is part of WebScarab, an Open Web Application Security
* Project utility. For details, please see http://www.owasp.org/
*
* Copyright (c) 2002 - 2004 Rogan Dawes
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Getting Source
* ==============
*
* Source for this application is maintained at Sourceforge.net, a
* repository for free software projects.
*
* For details, please see http://www.sourceforge.net/projects/owasp
*
*/
/*
* SessionidAnalysis.java
*
* Created on 16 November 2003, 05:19
*/
package org.owasp.webscarab.plugin.sessionid;
import java.io.BufferedWriter;
import java.io.FileWriter;
import org.owasp.webscarab.httpclient.ConversationHandler;
import org.owasp.webscarab.httpclient.FetcherQueue;
import org.owasp.webscarab.httpclient.HTTPClientFactory;
import org.owasp.webscarab.model.StoreException;
import org.owasp.webscarab.model.Cookie;
import org.owasp.webscarab.model.HttpUrl;
import org.owasp.webscarab.model.NamedValue;
import org.owasp.webscarab.model.ConversationID;
import org.owasp.webscarab.model.Request;
import org.owasp.webscarab.model.Response;
import org.owasp.webscarab.plugin.Framework;
import org.owasp.webscarab.plugin.Plugin;
import org.owasp.webscarab.plugin.Hook;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
*
* @author rdawes
*/
public class SessionIDAnalysis implements Plugin, ConversationHandler {
private SessionIDModel _model;
private FetcherQueue _fetcherQueue;
private int _threads = 4;
private String _name = null;
private String _regex = null;
private int _count = 0;
private Request _request = null;
private Response _response = null;
private Thread _runThread = null;
private Logger _logger = Logger.getLogger(getClass().getName());
/** Creates a new instance of SessionidAnalysis */
public SessionIDAnalysis(Framework framework) {
_model = new SessionIDModel(framework.getModel());
_fetcherQueue = new FetcherQueue("SessionID", this, _threads, 100);
}
public SessionIDModel getModel() {
return _model;
}
public void setSession(String type, Object store, String session) throws StoreException {
if (type.equals("FileSystem") && (store instanceof File)) {
_model.setStore(new FileSystemStore((File) store));
} else {
throw new StoreException("Store type '" + type + "' is not supported in " + getClass().getName());
}
}
/** The plugin name
* @return The name of the plugin
*
*/
public String getPluginName() {
return new String("Session ID Analysis");
}
public void run() {
_model.setStatus("Started");
_model.setRunning(true);
_runThread = Thread.currentThread();
_model.setStopping(false);
while (! _model.isStopping()) {
while (_request != null && _count > 0 && _fetcherQueue.getRequestsQueued() < _threads) {
_fetcherQueue.submit(_request);
}
try {
Thread.sleep(100);
} catch (InterruptedException ie) {}
}
_request = null;
_fetcherQueue.clearRequestQueue();
_model.setRunning(false);
_model.setStatus("Stopped");
}
public void requestError(Request request, IOException ioe) {
_logger.info("Requested " + request.getURL() + " got IOException " + ioe.getMessage());
}
public void responseReceived(Response response) {
if (_count == 0) return;
_count--;
Map<String, SessionID> ids = getIDsFromResponse(response, _name, _regex);
Iterator<String> it = ids.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
SessionID id = ids.get(key);
_model.addSessionID(key, id);
}
}
public Map<String, SessionID> getIDsFromResponse(Response response, String name, String regex) {
Map<String, SessionID> ids = new TreeMap<String, SessionID>();
Request request = response.getRequest();
if (request == null) {
System.out.println("Request was null?");
return ids;
}
HttpUrl url = request.getURL();
Date date = new Date();
NamedValue[] headers = response.getHeaders();
if (name != null && !name.equals("") && regex != null) {
String location = response.getHeader("Location");
if (location != null) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(location);
if (matcher.matches() && matcher.groupCount() > 0) {
for (int j=1; j<=matcher.groupCount(); j++) {
SessionID id = new SessionID(date, matcher.group(j));
ids.put(name + " " + j, id);
}
}
}
String type = response.getHeader("Content-Type");
if (type != null && type.startsWith("text/")) {
String charset = "UTF-8";
String body = null;
try {
body = new String(response.getContent(), charset);
} catch (UnsupportedEncodingException uee) {
body = new String(response.getContent());
}
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(body);
if (matcher.matches() && matcher.groupCount() > 0) {
for (int j=1; j<=matcher.groupCount(); j++) {
SessionID id = new SessionID(date, matcher.group(j));
ids.put(name + " " + j, id);
}
}
}
} else {
Pattern pattern = Pattern.compile("(.*)");
if (regex != null && !regex.equals("")) pattern = Pattern.compile(regex);
for (int i=0; i<headers.length; i++) {
if (headers[i].getName().equalsIgnoreCase("Set-Cookie") || headers[i].getName().equalsIgnoreCase("Set-Cookie2")) {
Cookie cookie = new Cookie(date, url, headers[i].getValue());
Matcher matcher = pattern.matcher(cookie.getValue());
name = cookie.getKey();
if (matcher.matches()) {
SessionID id = new SessionID(date, matcher.group(0));
ids.put(name, id);
if (matcher.groupCount() > 0) {
for (int j=1; j<=matcher.groupCount(); j++) {
if (!matcher.group(j).equals(matcher.group(0))) {
id = new SessionID(date, matcher.group(j));
ids.put(name + " " + j, id);
}
}
}
}
}
}
}
return ids;
}
public void fetch(Request request, String name, String regex, int count) {
// throws a Runtime exception if this failes
Pattern.compile(regex);
_request = request;
_name = name;
_regex = regex;
_count = count;
}
public void setRequest(Request request) {
_request = request;
}
public void fetchResponse() throws IOException {
_response = HTTPClientFactory.getInstance().fetchResponse(_request);
}
public Response getResponse() {
return _response;
}
/** This function provides for setting a different calculator
* i.e. one that calculates the sessionid based on different
* criteria to the DefaultCalculator, such as character ordering, etc
*
* No interface component uses it as yet, though
*/
public void setCalculator(String key, Calculator calc) {
_model.setCalculator(key, calc);
}
public boolean stop() {
_model.setStopping(true);
try {
_runThread.join(5000);
} catch (InterruptedException ie) {
_logger.warning("Interrupted!");
}
return !_model.isRunning();
}
public void flush() throws StoreException {
_model.flush();
}
public boolean isBusy() {
return _count > 0;
}
public String getStatus() {
return _model.getStatus();
}
public boolean isModified() {
return _model.isModified();
}
public boolean isRunning() {
return _model.isRunning();
}
public void analyse(ConversationID id, Request request, Response response, String origin) {
HttpUrl url = request.getURL();
String cookie = request.getHeader("Cookie");
if (cookie != null) _model.addRequestCookie(id, cookie);
String[] setCookie = response.getHeaders("Set-Cookie");
if (setCookie != null) {
for (int i=0; i<setCookie.length; i++) {
Cookie c = new Cookie(new Date(), setCookie[i]);
_model.addResponseCookie(id, url, c);
}
}
}
public Object getScriptableObject() {
return null;
}
public Hook[] getScriptingHooks() {
return new Hook[0];
}
public void clearSessionIDs(String key) {
_model.clearSessionIDs(key);
}
public void exportIDSToCSV(String key, File file) throws IOException {
int count = _model.getSessionIDCount(key);
if (count == 0) return;
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
StringBuffer buff = new StringBuffer();
for (int i=0; i<count; i++) {
SessionID id = _model.getSessionIDAt(key, i);
buff.append(id.getDate().getTime());
buff.append(",").append(_model.getSessionIDValue(key, id));
buff.append(",").append(id.getValue()).append("\n");
bw.write(buff.toString().toCharArray());
buff.delete(0, buff.length());
}
bw.close();
}
}