3.1 USB 连接
3.1.1 python脚本映射端口
ssh root@192.168.2.186 其实也就是 ssh -p 22 root@172.20.10.11,默认22端口省略了,我们可以通过ssh -p 12345 root@localhost连接,只要将本地的12345端口映射到usb端口,只要usb端口连接哪个设备就相当于给哪个设备发送请求。
有个python-client工具可以映射端口:
`
python tcprelay.py -t 要映射端口:本地端口
`
python-client python tcprelay.py -t 22:12345
Forwarding local port 12345 to remote port 22
将本地的 12345 端口映射到设备的 TCP 端口 22。这样就可以通过本地的 12345 端口建立连接了。
3.1.2 通过USB进行SSH连接
映射成功后想要登录直接:
//也可以 ssh -p 12345 root@127.0.0.1
ssh -p 12345 root@localhost
这里有个注意点是映射端口成功后不能关闭窗口,否则映射就没有了。
ssh 连接本地的 12345,由于做了端口映射所以会通过 usb 连接对面设备的22端口。
ssh -p 12345 root@localhost
The authenticity of host '[localhost]:12345 ([127.0.0.1]:12345)' can't be established.
RSA key fingerprint is SHA256:3vASLTEmYAr3ngQJ6b8MmLD82/LhxP0PuNHFQNPhBGl0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:12345' (RSA) to the list of known hosts.
zaizai:~ root#
这里会重新进行 rsa 本地记录( ip 变了),免密登录仍然有效。(我这里为了测试其他方便, 直接使用的 2222 )
ps: ip 变了,相当于登录一个新的服务器。所以保存 rsa。
3.1.3 验证中间人攻击
这个时候换一台设备进行 ssh -p 12345 root@localhost 登录就会提示中间人攻击了,由于本地 localhost 对应的 rsa 和新手机返回的hash值对应不上。如果只有一台手机可以通过修改 know_hosts 对应的 localhost 的 rsa 公钥模拟:
ssh -p 12345 root@localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:pIPlaWYd9wT2MfpRqvP/WOe1wVXfVVKiCKttyPHK3f0.
Please contact your system administrator.
Add correct host key in /Users/zaizai/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/zaizai/.ssh/known_hosts:4
RSA host key for [localhost]:12345 has changed and you have requested strict checking.
Host key verification failed.
所以如果有两台手机可以分别通过 sh -p 12345 root@localhost 和 ssh -p 12345 root@127.0.0.1 登录,就能区分开了。
3.2 配置USB快捷登录
这个时候在 config 中取别名就不行了,因为有端口的映射,并且地址也不是真实的地址。
在自己的脚本目录创建一个 iPhoneX.sh 文件(最好给这个目录配置环境变量),脚本内容如下:
ssh -p 12345 root@localhost
那么这个时候还需要端口映射的脚本 usbConnect.sh,内容如下:
python /Users/zaizai/HPShell/python-client/tcprelay.py -t 22:12345
端口映射脚本和连接脚本分开是为了方便多个设备切换,由于映射只需要一次。
使用:
//映射端口
usbConnect.sh
//链接
iPhoneX.sh
这样就连接上手机了。
需要两个窗口执行,映射完窗口一直存在的。
脚本目录文件:
3.3 Iproxy 端口映射
Iproxy 也是一个映射工具。
- 安装
libimobiledevice
brew install libimobiledevice
- 映射端口
iproxy 本地端口 要映射端口
iproxy 12345 22
这个映射和 python 脚本是反过来的。左边是本地端口,右边是要映射端口。其它的使用方式相同。
映射终端:
iproxy 12345 22
Creating listening port 12345 for device port 22
waiting for connection
New connection for 12345->22, fd = 5
waiting for connection
Requesting connecion to USB device handle 3 (serial: 5d38c0a07ffa912050c2cbc05da5436e10a2d5d7), port 22
连接终端:
iPhoneX.sh
iPhoneX:~ root#