-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
httprequest.cna
84 lines (71 loc) · 2.37 KB
/
httprequest.cna
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
#
# Safe & sound HTTP request implementation for Cobalt Strike 4.0 Aggressor Script.
# Works with HTTP & HTTPS, GET/POST/etc. + redirections.
#
# Author: Mariusz Banach / mgeeky, '20
# <mb [at] binary-offensive.com>
#
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 "";
}
}