2015年10月27日火曜日

Orvibo S20

電源をWifi(LAN)上で操作できるデバイスです
外出することが多くなりそうなので導入
ただ、心配なのが操作するアプリがLAN外から操作する場合に
製造元のサーバーを経由するということ
中国のよくわからないメーカーなので心もとない
ということでハックされたスクリプトで操作することに



情報元はここです
https://stikonas.eu/wordpress/2015/02/24/reverse-engineering-orvibo-s20-socket/


結論から言うと下記PHPファイルで操作できる

<?php
ob_start();
error_reporting(E_ALL | E_STRICT);

$server = 'IPADDRESS';
$port = 10000;
$macAddress = 'AC:CF:23:56:6C:24';
$twenties = '202020202020';
$ma = strtolower(implode('',explode(':',$macAddress)));
$maReverse = strtolower(implode('',array_reverse(explode(':',$macAddress))));
$subscribe = pack('H*','6864001e636c' . $ma . $twenties . $maReverse . $twenties);
$on = pack('H*','686400176463' . $ma . $twenties . '0000000001');
$off = pack('H*','686400176463' . $ma . $twenties . '0000000000');

//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);
    die("Couldn't create socket: [$errorcode] $errormsg <br />");
}

echo "Socket created <br />";

// Bind the source address
if(!socket_bind($sock, "0.0.0.0" , $port)) {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);
    die("Error binding socket. Error code was " . $errorcode . " and error message was " . $errormsg);
}

echo "Socket bound. Waiting for incoming data.<br />";

echo "Sending subscribe packet ..";
socket_sendto($sock, $subscribe, strlen($subscribe), 0, $server, $port);
sleep(2);
echo "Sending ON packet ..";
socket_sendto($sock, $on, strlen($on), 0, $server, $port);
sleep(2);
echo "Sending OFF packet ..";
//socket_sendto($sock, $off, strlen($off), 0, $server, $port);



ob_flush();
//Do some communication, this loop can handle multiple clients
while(1) {
    //Receive some data
    $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
    $buf = ascii2hex($buf);
    echo "Data from " . $remote_ip . ": " . $buf . "<br />";
       ob_flush();
}

function ascii2hex($ascii) {
    $hexadecimal = '';
    for ($i = 0; $i < strlen($ascii); $i++) {
        $byte = strtoupper(dechex(ord($ascii{$i})));
        $byte = str_repeat('0', 2 - strlen($byte)).$byte;
        $hexadecimal.=$byte." ";
    }
    return $hexadecimal;
}

socket_close($sock);

?>

TODO Ctrl+Cしないと終了しない

2015年10月26日月曜日

iPhoneでのOpenSSHの安全な設定

いつもiPhoneにOpenSSH入れる時に見ていたサイトから
いつの間にか記事がなくなっていたのでメモしておく


●ssh でユーザーmobileにアクセスする


ssh mobile@ipaddress

デフォルトのパスワードはalpine


●rootのパスワード変更


su
passwd
新しいパスワード


●共通鍵の生成


ssh-keygen -t rsa
保存先は空白
任意のパスフレーズ
 

autorized_keyに保存する
 
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
rm ~/.ssh/id_rsa.pub
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys


 ●秘密キーの設置

作成したid_rsaをPCもしくはMacに持ってくる
WinSCPやFileZilla等利用

 OpenSSHの場合はそのまま、puttyの場合はputty-genで置換する必要がある



●SSHdの設定を変更する

 該当行を次のように変更

sudo vim /etc/ssh/sshd_config #vimはcydiaからインストールしておく

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key #version1は使わない   
PermitRootLogin no #rootユーザではログインさせない   

#RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys #公開鍵方式を使う   

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no #パスワード認証は行わない



ちなみにviのコマンド(i:インサートモード,esc:ノーマルモード,x:一文字削除,dd:行削除,:wq:保存,:q!:保存せずに閉じる)


● サービスの再起動


su 
launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist
launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist