目的:讓你的Android設備支持遠程SSH功能
主要分幾個模塊實現(xiàn):
- Android移植openssh功能
- 內網(wǎng)穿透
安卓平臺環(huán)境:rk3399
內網(wǎng)穿透服務器:阿里云
編譯環(huán)境:ubuntu14
一、Android移植openssh功能
ssh源碼位置:rk3399_android7.1/external/openssh
1、添加到打包編譯
rk3399_android7.1/device/rockchip/rk3399/rk3399_box.mk
# Openssh
PRODUCT_PACKAGES += \
libssh \
ssh \
sftp \
scp \
sshd \
sshd_config \
ssh-keygen \
start-ssh
編譯燒錄system.img后,板子上已經(jīng)有ssh相關命令了
安卓系統(tǒng)目錄

2、重啟android設備進入先創(chuàng)建出幾個key
mkdir -p /data/ssh
chmod 700 /data/ssh
cd /data/ssh/
ssh-keygen -t rsa -f id_rsa -N “自定義秘鑰”
這里生成id_rsa和id_rsa.pub文件
3、將我們電腦上的公鑰push進去
//注意,authorized_keys是pc客戶端的公鑰文件
adb push id_rsa.pub /data/ssh/authorized_keys
4、更改sshd服務配置文件/system/etc/ssh/sshd_config
#Port 22改為Port 22
#PermitRootLogin yes改為PermitRootLogin without-password
#RSAAuthentication yes改為RSAAuthentication yes
#PubkeyAuthentication yes改為PubkeyAuthentication yes
PasswordAuthentication no改為#PasswordAuthentication no
#PermitEmptyPasswords no改為PermitEmptyPasswords yes
#ChallengeResponseAuthentication yes改為ChallengeResponseAuthentication yes
#UsePrivilegeSeparation yes改為UsePrivilegeSeparation no
5、啟動ssh服務
#android系統(tǒng)內啟動服務
start-ssh
//不中斷的在后臺運行:nohup start-ssh &
6、selinux的安全策略配置
#1、修改device/rockchip/common/sepolicy/file_contexts 添加
/system/bin/start-ssh u:object_r:start-ssh_exec:s0
#2、修改device/rockchip/common/sepolicy/start-ssh.te添加
type start-ssh, domain;
type start-ssh_exec, exec_type, file_type;
init_daemon_domain(start-ssh)
allow start-ssh start-ssh:tcp_socket { read write getopt getattr setopt accept create bind listen name_bind node_bind };
allow start-ssh fwmarkd_socket:sock_file { write };
allow start-ssh netd:unix_stream_socket { connectto };
allow start-ssh start-ssh:fd { use };
allow start-ssh port:tcp_socket { name_bind };
allow start-ssh node:tcp_socket { node_bind };
allow start-ssh system_file:file { execute_no_trans };
allow start-ssh start-ssh:capability { setgid net_raw setuid dac_override net_bind_service };
allow start-ssh start-ssh:udp_socket { create };
allow start-ssh system_data_file:file { read open getattr create write };
allow start-ssh system_data_file:dir { read write open getattr add_name };
allow start-ssh rootfs:lnk_file { getattr };
allow start-ssh shell_exec:file { getattr execute read open execute_no_trans };
allow start-ssh devpts:chr_file { open ioctl getattr read write setattr getattr };
#3、修改system/sepolicy/domain.te
neverallow {
-system_server
-system_app
-init
+ -start-ssh
-installd # for relabelfrom and unlink, check for this in explicit neverallow
} system_data_file:file no_w_file_perms;
# do not grant anything greater than r_file_perms and relabelfrom unlink
7、編譯運行燒錄設備
8、SSH連接(可以使用xshell進行連接默認端口22)
運行起來后,用電腦連接,連接進去后直接就是root用戶
ssh root@192.168.0.1
運行ok時的/data/ssh目錄
rk3399_box:/data/ssh # ls -l
total 56
-rw------- 1 root root 412 2021-04-13 06:51 authorized_keys
-rw------- 1 root root 668 2021-04-13 06:51 ssh_host_dsa_key
-rw------- 1 root root 604 2021-04-13 06:51 ssh_host_dsa_key.pub
-rw------- 1 root root 1675 2021-04-13 06:51 ssh_host_rsa_key
-rw------- 1 root root 396 2021-04-13 06:51 ssh_host_rsa_key.pub
-rw------- 1 root root 3342 2021-04-13 06:51 sshd_config
9、收尾工作
a、把啟動服務做進固件里面去
b、然后將/data/ssh/里面的文件全部拷貝出來,編譯的時候拷貝到system/etc/ssh/目錄,開機再拷貝到data/ssh目錄,并設置好相關的權限
修改:rk3399_android7.1/device/rockchip/rk3399/init.rk3399.rc
# For ssh 開機拷貝配置文件
mkdir /data/ssh
chmod 777 /data/ssh
copy /system/etc/ssh/authorized_keys /data/ssh/authorized_keys
copy /system/etc/ssh/ssh_host_dsa_key /data/ssh/ssh_host_dsa_key
copy /system/etc/ssh/ssh_host_dsa_key.pub /data/ssh/ssh_host_dsa_key.pub
copy /system/etc/ssh/ssh_host_ecdsa_key /data/ssh/ssh_host_ecdsa_key
copy /system/etc/ssh/ssh_host_ecdsa_key.pub /data/ssh/ssh_host_ecdsa_key.pub
copy /system/etc/ssh/ssh_host_rsa_key /data/ssh/ssh_host_rsa_key
copy /system/etc/ssh/ssh_host_rsa_key.pub /data/ssh/ssh_host_rsa_key.pub
copy /system/etc/ssh/sshd_config /data/ssh/sshd_config
mkdir /data/ssh/empty
chmod 600 /data/ssh/empty
chmod 600 /data/ssh/authorized_keys
chmod 600 /data/ssh/ssh_host_dsa_key
chmod 600 /data/ssh/ssh_host_dsa_key.pub
chmod 600 /data/ssh/ssh_host_ecdsa_key
chmod 600 /data/ssh/ssh_host_ecdsa_key.pub
chmod 600 /data/ssh/ssh_host_rsa_key
chmod 600 /data/ssh/ssh_host_rsa_key.pub
chmod 600 /data/ssh/sshd_config
#open ssh 開機啟動ssh服務
service daemonssh /system/bin/start-ssh
class main
user root
group root
修改:device/rockchip/rk3399/rk3399_box.mk 添加
PRODUCT_COPY_FILES += \
$(call find-copy-subdir-files,*,$(LOCAL_PATH)/ssh,system/etc/ssh)
第一階段完工,現(xiàn)在已經(jīng)完全能在局域網(wǎng)內遠程ssh安卓設備
二、內網(wǎng)穿透
這里需要到自己購買服務器,或者使用花生殼之類的,不建議使用免費,存在安全問題,最好自己搭建;
代碼:FRP開源項目
服務器:阿里云
文檔:https://gofrp.org/docs/setup/
1、下載源碼
https://github.com/fatedier/frp/releases
根據(jù)云服務器、安卓的cpu架構選擇對應的包下載;
(客戶端)RK3399 Cpu架構選:frp_0.36.2_linux_arm64.tar.gz
(服務端)阿里云服務器CPU架構選:frp_0.36.2_linux_amd64.tar.gz
2、安裝
服務端:
#home目錄下創(chuàng)建frp目錄并拷貝源碼到此目錄,一定要修改執(zhí)行權限
[root@ frp]# ll
total 22628
-rwxrwxrwx 1 root root 9953280 Mar 18 11:24 frpc
-rwxrwxrwx 1 root root 9433 Mar 18 11:26 frpc_full.ini
-rwxrwxrwx 1 root root 126 Mar 18 11:26 frpc.ini
-rwxrwxrwx 1 root root 13172736 Mar 18 11:24 frps
-rwxrwxrwx 1 root root 5051 Mar 18 11:26 frps_full.ini
-rwxrwxrwx 1 root root 26 Mar 18 11:26 frps.ini
-rw-rw-r-- 1 root root 11358 Mar 18 11:26 LICENSE
drwxrwxr-x 2 root root 4096 Apr 13 14:19 systemd
修改配置,在具有公網(wǎng) IP 的機器上部署 frps,修改 frps.ini 文件,這里使用了最簡化的配置,設置了 frp 服務器用戶接收客戶端連接的端口:
[common]
bind_port = 7000
啟動服務
./frps -c ./frps.ini
客戶端(RK3399):
在system目錄下創(chuàng)建frpc并復制代碼到該目錄下,一定要修改執(zhí)行權限
rk3399_box:/system/frpc # ls -l
total 42200
__bionic_open_tzdata_path: ANDROID_DATA not set!
__bionic_open_tzdata_path: ANDROID_ROOT not set!
-rw-r--r-- 1 root root 11358 2021-03-19 06:05 LICENSE
-rwxrwxrwx 1 root root 9240576 2021-03-19 06:04 frpc
-rwxrwxrwx 1 root root 130 2021-04-13 06:34 frpc.ini
-rwxrwxrwx 1 root root 9433 2021-03-19 06:05 frpc_full.ini
-rw-r--r-- 1 root root 12320768 2021-03-19 06:04 frps
-rw-r--r-- 1 root root 26 2021-03-19 06:05 frps.ini
-rw-r--r-- 1 root root 5051 2021-03-19 06:05 frps_full.ini
drwxrwxrwx 2 root root 4096 2021-04-13 06:41 systemd
修改配置:在需要被訪問的內網(wǎng)機器上(SSH 服務通常監(jiān)聽在 22 端口)部署 frpc,修改 frpc.ini 文件,假設 frps 所在服務器的公網(wǎng) IP 為 x.x.x.x:
[common]
server_addr = x.x.x.x 你的阿里云ip地址
server_port = 7000
tls_enable = true //解決內網(wǎng)防火墻問題導致無法連接服務器
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
[adb]
type = tcp
local_ip = 127.0.0.1
local_port = 5555
remote_port = 8555
local_ip 和 local_port 配置為本地需要暴露到公網(wǎng)的服務地址和端口。remote_port 表示在 frp 服務端監(jiān)聽的端口,訪問此端口的流量將會被轉發(fā)到本地服務對應的端口。
啟動客戶端:
./frpc -c ./frpc.ini
最后
通過xshell連接服務器ip,協(xié)議為SSH,端口號為6000,5555為安卓adb調試網(wǎng)絡端口完美連上Rk3399(你的手機)
~ ~~~~~~~~大功告成
遺留問題:1、使用用戶名和密碼登錄ssh 2、將frpc客戶端代碼一起打包到rom里