Wird ein Linux-Gerät an einem unbekannten Ort in Betrieb genommen ändert sich die Umgebung ständig, dabei ist es hilfreich, beim Start schon etwas über das LAN und die öffentliche Umgebung zu erfahren, um schnell über eine Reverse-SSH-Verbindung darauf zugreifen zu können.
Das Szenario besteht aus 4 Komponenten:
startup.php
start_command.sh
ausgeführtvia Crontab beim Start als Benutzer:
crontab -e
@reboot sleep 60 && sh /home/user/start_command.sh
Systemweit (root) in der rc.local aufrufen
nano /etc/rc.local
/root/start_command.sh &
start_command.sh
chmod +x start_command.sh
#!/bin/bash
command="DeviceName erreichbar via SSH cloud-ssh-server.domain.tld dann: ssh user@localhost -p 11111"
ip=$(hostname -I)
param="ip=${ip}&sec=LongPassword&command=${command}"
curl -d "${param}" -X POST https://cloud-http-server.domain.tld/startup.php
/usr/bin/autossh -p2000 -fNC \
-R 11111:localhost:222 \
[email protected]
startup.php
<?php
// TEST
// curl -d "ip=0.0.0.0&sec=LongPassword9&command=testing" -X POST https://cloud-http-server.domain.tld/startup.php
function getUserIP() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_POST['sec']) && (
$_POST['sec'] == 'LongPassword' /* device 1 */ ||
$_POST['sec'] == 'LongPassword1' /* device 2 */ ||
$_POST['sec'] == 'LongPassword2' /* device 3 */
)){
$sec = $_POST['sec'];
unset($_POST['sec']);
$postdata = $_POST;
$postdata['public_ip'] = getUserIP();
$chs = curl_init();
curl_setopt($chs, CURLOPT_URL, "https://discord.com/api/webhooks/{DISCORD_WEBHOOK_TOKEN}");
curl_setopt($chs, CURLOPT_POST, true);
curl_setopt($chs, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json; charset=utf-8' ]);
curl_setopt($chs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($chs, CURLOPT_POSTFIELDS, json_encode([ 'username' => 'startup', 'content' => json_encode($postdata) ]));
curl_exec($chs);
}
}
Als Discord Meldung erhalten wir dann so was:
startup — 17.01.2025 14:33
{
"ip": "192.168.0.56 10.120.11.75",
"command": "DeviceName erreichbar via SSH cloud-ssh-server.domain.tld dann: ssh user@localhost -p 11111",
"public_ip": "194.130.241.55"
}