forked from warecrer/Bettercap-caplets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-req-dump.js
140 lines (107 loc) · 2.85 KB
/
http-req-dump.js
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
var RESET = "\033[0m";
function R(s) {
return "\033[31m" + s + RESET;
}
function G(s) {
return "\033[32m" + s + RESET;
}
function B(s) {
return "\033[34m" + s + RESET;
}
function Y(s) {
return "\033[33m" + s + RESET;
}
function DIM(s) {
return "\033[2m" + s + RESET;
}
function BOLD(s) {
return "\033[1m" + s + RESET;
}
function dumpHeaders(req) {
log( "> " + BOLD(G("Headers")) );
for( var i = 0; i < req.Headers.length; i++ ) {
var header = req.Headers[i];
log( " " + B(header.Name) + " : " + DIM(header.Value) );
}
}
function dumpPlain(req) {
log( " > " + BOLD(G("Text")) );
var body = req.ReadBody();
log( " " + Y(body) );
}
function dumpForm(req) {
log( " > " + BOLD(G("Form")) );
var form = req.ParseForm();
for( var key in form ) {
log( " " + B(key) + " : " + Y(form[key]) );
}
}
function dumpJSON(req) {
log( " > " + BOLD(G("JSON")) );
var body = req.ReadBody();
// TODO: pretty print json
log( " " + Y(body) );
}
function pad(num, size, fill) {
var s = ""+num;
while( s.length < size ) {
s = fill + s;
}
return s;
}
function toHex(n) {
var hex = "0123456789abcdef";
var h = hex[(0xF0 & n) >> 4] + hex[0x0F & n];
return pad(h, 2, '0');
}
function isPrint(c){
if( !c ) { return false; }
var code = c.charCodeAt(0);
return ( code > 31 ) && ( code < 127 );
}
function dumpHex(raw, linePad) {
var DataSize = raw.length;
var Bytes = 16;
for( var address = 0; address < DataSize; address++ ) {
var saddr = pad(address, 8, '0');
var shex = '';
var sprint = '';
var end = address + Bytes;
for( var i = address; i < end; i++ ) {
if( i < DataSize ) {
shex += toHex(raw.charCodeAt(i)) + ' ';
sprint += isPrint(raw[i]) ? raw[i] : '.';
} else {
shex += ' ';
sprint += ' ';
}
}
address = end;
log( linePad + G(saddr) + ' ' + shex + ' ' + sprint );
}
}
function dumpRaw(req) {
var body = req.ReadBody();
log( " > " + BOLD(G("Body")) + " " + DIM("("+body.length + " bytes)") + "\n" );
dumpHex(body, " ");
}
function onRequest(req, res) {
log( BOLD(req.Client) + " > " + B(req.Method) + " " + req.Hostname + req.Path + ( req.Query ? "?" + req.Query : '') );
dumpHeaders(req);
if( req.ContentType ) {
log();
if( req.ContentType.indexOf("text/plain") != -1 ) {
dumpPlain(req);
}
else if( req.ContentType.indexOf("application/x-www-form-urlencoded") != -1 ) {
dumpForm(req);
}
else if( req.ContentType.indexOf("application/json") != -1 ) {
dumpJSON(req);
}
else {
dumpRaw(req);
}
}
log();
}