summaryrefslogtreecommitdiffstats
path: root/m2usenet-covertraffic.php
blob: 28125098f133016df601f028517c621c816d5b19 (plain) (blame)
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
#!/usr/bin/env php
<?php
/**
 * m2usenet Cover Traffic Generator
 * Sends random dummy posts to break traffic analysis
 * 
 * Usage: Run via cron every hour
 * Crontab: 0 * * * * /usr/local/bin/m2usenet-covertraffic.php >> /var/log/m2usenet/cover.log 2>&1
 */

// Configuration
define('SOCKS5_PROXY', '127.0.0.1');
define('SOCKS5_PORT', 9050);
define('SOCKS5_TIMEOUT', 120);
define('SMTP_READ_TIMEOUT', 30);
define('SMTP_FINAL_TIMEOUT', 30);
define('MAX_SMTP_RESPONSE_SIZE', 8192);

// Primary relay (fog)
define('PRIMARY_RELAY', [
    'host' => 'qee4i7sags6phsvb2yodwecfj7noimfhhalsjktsvikrwotxzis3raad.onion',
    'port' => 25,
    'mail2news' => 'mail2news@xilb7y4kj6u6qfo45o3yk2kilfv54ffukzei3puonuqlncy7cn2afwyd.onion'
]);

// Logging
function coverLog($message) {
    echo "[" . gmdate('Y-m-d H:i:s') . " UTC] $message\n";
}

// Crypto functions
function cryptoRandBytes($length) {
    return random_bytes($length);
}

function cryptoRandInt($min, $max) {
    return random_int($min, $max);
}

// Generate random realistic text
function generateRandomText($minLength, $maxLength) {
    $length = cryptoRandInt($minLength, $maxLength);
    
    $sentences = [
        "This is a test message for network connectivity.",
        "Anonymous communication systems require regular testing.",
        "Usenet remains an important decentralized network.",
        "Privacy-focused tools help protect user anonymity.",
        "Secure messaging relies on proper implementation.",
        "Network latency can vary significantly over Tor.",
        "Distributed systems provide resilience and redundancy.",
        "Cryptographic signatures ensure message integrity.",
        "Random traffic patterns improve privacy protection.",
        "Testing infrastructure is essential for reliability."
    ];
    
    $text = '';
    while (strlen($text) < $length) {
        $text .= $sentences[array_rand($sentences)] . " ";
    }
    
    return trim(substr($text, 0, $length));
}

// Generate dummy hashcash token
function generateDummyHashcash() {
    $now = new DateTime('now', new DateTimeZone('UTC'));
    $timestamp = $now->format('ymdHis');
    $resource = 'covertraffic@m2usenet.invalid';
    $rand = cryptoRandInt(100000, 999999);
    
    // Simple hashcash format (not mined, just for structure)
    return "1:20:{$timestamp}:{$resource}::{$rand}:0";
}

// Generate Message-ID
function generateMessageID() {
    $dateTime = new DateTime('now', new DateTimeZone('UTC'));
    $jitterSeconds = cryptoRandInt(-30, 30);
    $dateTime->modify("{$jitterSeconds} seconds");
    
    $dateComponent = $dateTime->format('Ymd.His');
    $randomComponent = bin2hex(cryptoRandBytes(4));
    $combined = $dateComponent . '.' . $randomComponent;
    $hash = md5($combined);
    
    return sprintf("<%s@m2usenet.local>", $hash);
}

// SOCKS5 connection (copied from send.php)
function socks5Connect($destHost, $destPort) {
    $socket = @stream_socket_client(
        "tcp://" . SOCKS5_PROXY . ":" . SOCKS5_PORT,
        $errno,
        $errstr,
        SOCKS5_TIMEOUT,
        STREAM_CLIENT_CONNECT
    );
    
    if (!$socket) {
        coverLog("ERROR: Failed to connect to SOCKS5 proxy");
        return false;
    }
    
    stream_set_timeout($socket, SOCKS5_TIMEOUT);
    stream_set_blocking($socket, true);
    
    // SOCKS5 handshake
    $request = pack('C3', 0x05, 0x01, 0x00);
    if (fwrite($socket, $request) === false) {
        fclose($socket);
        return false;
    }
    
    $response = fread($socket, 2);
    if (strlen($response) !== 2) {
        fclose($socket);
        return false;
    }
    
    $data = unpack('Cver/Cmethod', $response);
    if ($data['ver'] !== 0x05 || $data['method'] !== 0x00) {
        fclose($socket);
        return false;
    }
    
    // Connect request
    $domainLen = strlen($destHost);
    $request = pack('C4', 0x05, 0x01, 0x00, 0x03);
    $request .= pack('C', $domainLen);
    $request .= $destHost;
    $request .= pack('n', $destPort);
    
    if (fwrite($socket, $request) === false) {
        fclose($socket);
        return false;
    }
    
    $response = fread($socket, 4);
    if (strlen($response) < 4) {
        fclose($socket);
        return false;
    }
    
    $data = unpack('Cver/Crep/Crsv/Catyp', $response);
    if ($data['ver'] !== 0x05 || $data['rep'] !== 0x00) {
        fclose($socket);
        return false;
    }
    
    // Read bind address
    if ($data['atyp'] === 0x01) {
        fread($socket, 6);
    } elseif ($data['atyp'] === 0x03) {
        $len = ord(fread($socket, 1));
        fread($socket, $len + 2);
    } elseif ($data['atyp'] === 0x04) {
        fread($socket, 18);
    }
    
    return $socket;
}

// Send SMTP command
function smtpCommand($socket, $command, $expectedCode = null) {
    if (!empty($command)) {
        if (fwrite($socket, $command) === false) {
            return false;
        }
        fflush($socket);
    }
    
    $response = '';
    $bytesRead = 0;
    
    while (!feof($socket) && $bytesRead < MAX_SMTP_RESPONSE_SIZE) {
        $line = fgets($socket, 1024);
        if ($line === false) break;
        
        $response .= $line;
        $bytesRead += strlen($line);
        
        if (preg_match('/^\d{3} /', $line)) {
            break;
        }
    }
    
    if (empty($response)) {
        return false;
    }
    
    if ($expectedCode !== null) {
        if (!preg_match("/^$expectedCode/", $response)) {
            return false;
        }
    }
    
    return $response;
}

// Send dummy message
function sendDummyMessage() {
    coverLog("Sending dummy message via cover traffic");
    
    $relay = PRIMARY_RELAY;
    $socket = socks5Connect($relay['host'], $relay['port']);
    
    if (!$socket) {
        coverLog("ERROR: SOCKS5 connection failed");
        return false;
    }
    
    stream_set_timeout($socket, SMTP_READ_TIMEOUT);
    
    try {
        // SMTP session
        if (smtpCommand($socket, '', 220) === false) {
            throw new Exception("No greeting");
        }
        
        if (smtpCommand($socket, "HELO m2usenet.local\r\n", 250) === false) {
            throw new Exception("HELO failed");
        }
        
        $fromEmail = 'covertraffic' . cryptoRandInt(1000, 9999) . '@m2usenet.invalid';
        
        if (smtpCommand($socket, "MAIL FROM:<$fromEmail>\r\n", 250) === false) {
            throw new Exception("MAIL FROM failed");
        }
        
        if (smtpCommand($socket, "RCPT TO:<{$relay['mail2news']}>\r\n", 250) === false) {
            throw new Exception("RCPT TO failed");
        }
        
        if (smtpCommand($socket, "DATA\r\n", 354) === false) {
            throw new Exception("DATA failed");
        }
        
        // Build message
        $messageId = generateMessageID();
        $jitter = cryptoRandInt(-1800, 1800);
        
        $headers = [
            "From: Anonymous <$fromEmail>",
            "To: {$relay['mail2news']}",
            "Subject: Test message " . bin2hex(cryptoRandBytes(4)),
            "Message-ID: $messageId",
            "Date: " . gmdate('r', time() + $jitter),
            "Newsgroups: alt.test",
            "MIME-Version: 1.0",
            "Content-Type: text/plain; charset=utf-8",
            "Content-Transfer-Encoding: 8bit",
            "User-Agent: m2usenet-web v2.1.0",
            "X-No-Archive: Yes"
        ];
        
        $body = generateRandomText(500, 1500);
        $fullMessage = implode("\r\n", $headers) . "\r\n\r\n" . $body;
        
        // Dot stuffing
        $lines = explode("\r\n", $fullMessage);
        $stuffedLines = [];
        foreach ($lines as $line) {
            if (isset($line[0]) && $line[0] === '.') {
                $stuffedLines[] = '.' . $line;
            } else {
                $stuffedLines[] = $line;
            }
        }
        
        $stuffedMessage = implode("\r\n", $stuffedLines) . "\r\n";
        
        // Send message
        if (fwrite($socket, $stuffedMessage) === false) {
            throw new Exception("Failed to write message");
        }
        fflush($socket);
        
        // Send terminator
        if (fwrite($socket, ".\r\n") === false) {
            throw new Exception("Failed to write terminator");
        }
        fflush($socket);
        
        stream_set_timeout($socket, SMTP_FINAL_TIMEOUT);
        
        // Wait for response
        $response = '';
        $bytesRead = 0;
        while (!feof($socket) && $bytesRead < MAX_SMTP_RESPONSE_SIZE) {
            $read = [$socket];
            $write = null;
            $except = null;
            $selectResult = @stream_select($read, $write, $except, 1);
            
            if ($selectResult === false) break;
            if ($selectResult === 0) {
                if ((microtime(true) - $startTime) > SMTP_FINAL_TIMEOUT) break;
                continue;
            }
            
            $line = fgets($socket, 1024);
            if ($line === false) break;
            
            $response .= $line;
            $bytesRead += strlen($line);
            
            if (preg_match('/^\d{3} /', $line)) {
                break;
            }
        }
        
        if (!preg_match('/^250/', $response)) {
            throw new Exception("Message rejected");
        }
        
        coverLog("SUCCESS: Dummy message sent (Message-ID: $messageId)");
        
        @smtpCommand($socket, "QUIT\r\n", 221);
        fclose($socket);
        
        return true;
        
    } catch (Exception $e) {
        coverLog("ERROR: " . $e->getMessage());
        if (is_resource($socket)) {
            @fclose($socket);
        }
        return false;
    }
}

// Main execution
coverLog("=== m2usenet Cover Traffic Generator v2.1.0 ===");

// 50% chance to send dummy
if (cryptoRandInt(1, 100) > 50) {
    coverLog("Skipping this round (random decision)");
    exit(0);
}

// Send 1-2 dummy messages
$dummyCount = cryptoRandInt(1, 2);
coverLog("Sending $dummyCount dummy message(s)");

for ($i = 0; $i < $dummyCount; $i++) {
    $success = sendDummyMessage();
    
    if ($success && $i < $dummyCount - 1) {
        // Delay between dummies
        $delay = cryptoRandInt(300, 900);
        coverLog("Waiting {$delay}s before next dummy");
        sleep($delay);
    }
}

coverLog("=== Cover traffic round complete ===");
exit(0);