Wednesday, July 9, 2008

WebScarab Template - Bean Shell














In the spirit of WebScarab templates, here is a fresh template for the bean shell. Wait, you just put out a template for Manual Request, how is this different? Good question. The Scripted feature allows you to build and send custom requests. The bean shell lets you modify all requests/responses sent through WebScarab. So, if you turn on WebScarab and start browsing the web, your bean shell code will execute for each request/response.


Copy the source code below into the bean shell and hit 'Commit'. The output file prints to c:\ so change that as desired.

-Michael Coates





/* ======================================= */
/* Provided by http://michael-coates.blogspot.com */
/* ======================================= */
/* Please read the JavaDoc and/or the source to understand what methods are available */

import org.owasp.webscarab.model.Request;
import org.owasp.webscarab.model.Response;
import org.owasp.webscarab.httpclient.HTTPClient;
import java.io.IOException;
import java.io.*;

public Response fetchResponse(HTTPClient nextPlugin, Request request) throws IOException {

//=====Make changes to the requests=========
//=====Remember: These changes will be applied to all requests while the bean is enabled. ============
//request.setHeader("User-Agent","MySuperBrowser");
//request.setHeader("Accept"," text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
//request.setHeader("Accept-Language"," en-us,en;q=0.5");
//request.setHeader("Accept-Encoding"," gzip,deflate");
//request.setHeader("Accept-Charset"," ISO-8859-1,utf-8;q=0.7,*;q=0.7");
//request.setHeader("Keep-Alive"," 300");
//request.setHeader("Proxy-Connection"," keep-alive");
//request.setHeader("Cookie"," Some cookie values here");
//==============


//Send the request and fetch the response - this is required for requests to work
Response response = nextPlugin.fetchResponse(request);

//=====Print some stuff to a file=========
FileWriter fstream = new FileWriter("c:\\webscarab_bean_out.txt", true);
BufferedWriter bw = new BufferedWriter(fstream);
// printRequest(request,bw);
// printResponse(response,bw);
// PrintToFile("some string",out2);
bw.close();
//==============

return response;
}

// ============= Add any additional supporting methods below===============
void printRequest(Request request,BufferedWriter out2){
out2.write("========\r\n");
out2.write(request.getMethod()+"\r\n");
out2.write(request.getURL()+"\r\n");
out2.write(request.getVersion()+"\r\n");
String[] headers=request.getHeaderNames();
for(String header : headers){
out2.write(header+" : " + request.getHeader(header)+"\r\n");
}
out2.write("\r\n====
====\r\n");

}

void printResponse(Response response, BufferedWriter out2){
out2.write("========\r\n");
out2.write(response.getStatus()+"\r\n");
out2.write(response.getMessage()+"\r\n");
//print the headers
String[] headers=response.getHeaderNames();
for(String header : headers){
out2.write(header+" : " + response.getHeader(header)+"\r\n");
}
out2.write("\r\n");
//print the content - this could be a lot of content
byte[] data=response.getContent();
String data_response=new String(data);
out2.write(data_response);

out2.write("\r\n====
====\r\n");

}

public static void PrintToFile(String string_value,BufferedWriter out2){
//Write arbitary data to the file
out2.write("=======================================\r\n");
out2.write(string_value);
out2.close();
}