-
Notifications
You must be signed in to change notification settings - Fork 10
/
tcp-proxy.php
150 lines (126 loc) · 4.25 KB
/
tcp-proxy.php
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
141
142
143
144
145
146
147
148
149
150
<?php
$options = getopt('l:p:r:q:h', [
'local-host:',
'local-port:',
'remote-host:',
'remote-port:',
'help'
]);
$localHost = '0.0.0.0';
$localPort = 0;
$remoteHost = '';
$remotePort = 0;
if (array_key_exists('h', $options) || array_key_exists('help', $options)) {
echo "PHP TCP Proxy Tool\n";
echo "\n";
echo "Usage: php tcp-proxy.php --local-host <local-host> --local-port <local-port> --remote-host <remote-host> --remote-port <remote-post> --help\n";
echo "\n";
echo "-l, --local-host: The local host ip to listen for connections on\n";
echo "-p, --local-port: The local port to listen for connections on\n";
echo "-r, --remote-host: The remote host to forward connections to\n";
echo "-q, --remote-port: The remote port to forward connectiosn to\n";
echo "-h, --help: Show this help menu\n";
echo "\n";
exit();
}
if (array_key_exists('local-host', $_GET)) {
$localHost = $_GET['local-host'];
} else if (array_key_exists('l', $options)) {
$localHost = $options['l'];
} else if (array_key_exists('local-host', $options)) {
$localHost = $options['local-host'];
}
if (array_key_exists('local-port', $_GET)) {
$localPort = $_GET['local-port'];
} else if (array_key_exists('p', $options)) {
$localPort = $options['p'];
} else if (array_key_exists('local-port', $options)) {
$localPort = $options['local-port'];
}
if (array_key_exists('remote-host', $_GET)) {
$remoteHost = $_GET['remote-host'];
} else if (array_key_exists('r', $options)) {
$remoteHost = $options['r'];
} else if (array_key_exists('remote-host', $options)) {
$remoteHost = $options['remote-host'];
}
if (array_key_exists('remote-port', $_GET)) {
$remotePort = $_GET['remote-port'];
} else if (array_key_exists('q', $options)) {
$remotePort = $options['q'];
} else if (array_key_exists('remote-port' ,$options)) {
$remotePort = $options['remote-port'];
}
$startErrors = [];
if ($localPort == 0) {
$startErrors[] = "Local Port must be set.\n";
}
if (strlen($remoteHost) == 0) {
$startErrors[] = "Remote Host must be set.\n";
}
if ($remotePort == 0) {
$startErrors[] = "Remote Port must be set.\n";
}
if (count($startErrors) > 0) {
echo join('\n', $startErrors);
exit(1);
}
$localSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($localSocket, $localHost, $localPort) or die('Could not bind to address (is it in use?)');
socket_listen($localSocket, 5);
while (true) {
$localClient = socket_accept($localSocket);
$localBuffer = '';
while (true) {
$localInput = socket_read($localClient, 4096);
$localInput = str_replace($localHost . ':' . $localPort, $remoteHost . ':' . $remotePort, $localInput);
$localBuffer .= $localInput;
if ($localInput < 4096) {
break;
}
}
echo "[==>] Local request heading out.\n";
hex_dump($localBuffer);
$remoteSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($remoteSocket, $remoteHost, $remotePort);
socket_write($remoteSocket, $localBuffer);
$remoteBuffer = '';
while (true) {
$remoteResponse = socket_read($remoteSocket, 4096);
$remoteResponse = str_replace($remoteHost . ':' . $remotePort, $localHost . ':' . $localPort, $remoteResponse);
$remoteBuffer .= $remoteResponse;
if ($remoteResponse < 4096) {
break;
}
}
echo "[<==] Remote response coming in.\n";
hex_dump($remoteBuffer);
socket_write($localClient, $remoteBuffer);
socket_close($remoteSocket);
socket_close($localClient);
}
# from http://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php
function hex_dump($data, $newline="\n")
{
static $from = '';
static $to = '';
static $width = 16; # number of bytes per line
static $pad = '.'; # padding for non-visible characters
if ($from==='')
{
for ($i=0; $i<=0xFF; $i++)
{
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
}
$hex = str_split(bin2hex($data), $width*2);
$chars = str_split(strtr($data, $from, $to), $width);
$offset = 0;
foreach ($hex as $i => $line)
{
echo sprintf('%6X',$offset).' : '.implode(' ', str_split($line,2)) . ' [' . $chars[$i] . ']' . $newline;
$offset += $width;
}
}
?>