Last active
June 2, 2023 09:15
-
-
Save mgeeky/2d7f8c2a6ffbfd23301e1e2de0312087 to your computer and use it in GitHub Desktop.
Safe & sound HTTP request implementation for Cobalt Strike 4.0 Aggressor Script. Works with HTTP & HTTPS, GET/POST/etc. + redirections.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Safe & sound HTTP request implementation for Cobalt Strike 4.0 Aggressor Script. | |
# Works with HTTP & HTTPS, GET/POST/etc. + redirections. | |
# | |
# Mariusz B. / mgeeky | |
# | |
import java.net.URLEncoder; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
# | |
# httpRequest($method, $url, $body); | |
# | |
sub httpRequest { | |
$method = $1; | |
$url = $2; | |
$body = $3; | |
$n = 0; | |
if(size(@_) == 4) { $n = $4; } | |
$bodyLen = strlen($body); | |
$maxRedirectsAllowed = 10; | |
if ($n > $maxRedirectsAllowed) { | |
warn("Exceeded maximum number of redirects: $method $url "); | |
return ""; | |
} | |
try | |
{ | |
$urlobj = [new java.net.URL: $url]; | |
$con = $null; | |
$con = [$urlobj openConnection]; | |
[$con setRequestMethod: $method]; | |
[$con setInstanceFollowRedirects: true]; | |
[$con setRequestProperty: "Accept", "*/*"]; | |
[$con setRequestProperty: "Cache-Control", "max-age=0"]; | |
[$con setRequestProperty: "Connection", "keep-alive"]; | |
[$con setRequestProperty: "User-Agent", $USER_AGENT]; | |
if($bodyLen > 0) { | |
[$con setDoOutput: true]; | |
[$con setRequestProperty: "Content-Type", "application/x-www-form-urlencoded"]; | |
} | |
$outstream = [$con getOutputStream]; | |
if($bodyLen > 0) { | |
[$outstream write: [$body getBytes]]; | |
} | |
$inputstream = [$con getInputStream]; | |
$handle = [SleepUtils getIOHandle: $inputstream, $outstream]; | |
$responseCode = [$con getResponseCode]; | |
if(($responseCode >= 301) && ($responseCode <= 304)) { | |
$loc = [$con getHeaderField: "Location"]; | |
return httpRequest($method, $loc, $body, $n + 1); | |
} | |
@content = readAll($handle); | |
$response = ""; | |
foreach $line (@content) { | |
$response .= $line . "\r\n"; | |
} | |
if((strlen($response) > 2) && (right($response, 2) eq "\r\n")) { | |
$response = substr($response, 0, strlen($response) - 2); | |
} | |
return $response; | |
} | |
catch $message | |
{ | |
warn("HTTP Request failed: $method $url : $message "); | |
printAll(getStackTrace()); | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment