The previous blocks of code showed all of the extension content and the graphical interface, but the following lines show the operation of the extension itself:
First, we set up the initial variables and components, as follows:
@Override public String getTabCaption() { return "Burp UserAgent"; } @Override public Component getUiComponent() { return bUAPanel; } @Override public String getActionName(){ return "Burp UserAgent"; } @Override public void performAction(IHttpRequestResponse currentRequest, IHttpRequestResponse[] macroItems) { IRequestInfo requestInfo = extHelpers.analyzeRequest(currentRequest); List<String> headers = requestInfo.getHeaders(); String reqRaw = new String(currentRequest.getRequest()); String reqBody = reqRaw.substring(requestInfo.getBodyOffset()); Integer uaInHeader = 0; if (!newUA.startsWith("Current Browser")) { for (int i = 0; i < headers.size(); i++) {
The following code is the main part of the extension. Here, a loop is created to add the values, that is, to substitute the user-agents:
if (headers.get(i).startsWith("User-Agent:") && !headers.get(i).startsWith("User-Agent: " + newUA)) { headers.set(i, "User-Agent: " + newUA); uaInHeader = 1; } else if (headers.get(i).startsWith("User-Agent: " + newUA)) { uaInHeader = 1; } } } if (uaInHeader == 0 && !newUA.startsWith("Current Browser")) { headers.add("User-Agent: " + newUA); } byte[] message = extHelpers.buildHttpMessage(headers, reqBody.getBytes()); currentRequest.setRequest(message); } }
Now that we are done writing the extension, let's go ahead and execute it.