工具收集

部署

  1. 启动 ssh 服务

    1
    
    sudo systemctl enable sshd.service
  2. 权限设置

    • /home/user 设置成 755
    • /home/user/.ssh 设置成 700
    • /home/user/.ssh/* 设置成 600
    • eg:

      1
      2
      3
      
      chmod 755 ~
      chmod 700 ~/.ssh/
      chmod 600 ~/.ssh/*

Google VPS

Hosts

bigwave

1
2
3
Host                bigwave
HostName        104.197.43.118
Port            22
mosh

MOSH CONNECT 60001 g3uQ5AZyxoDZifgvSdaHow

Office

Hosts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Host         office-desktop
HostName        172.17.223.83
User            sawyer
IdentityFile    ~/.ssh/id_rsa

Host            office-server
HostName        172.17.237.71
Port            2244
PreferredAuthentications publickey
IdentityFile    ~/.ssh/xulei

使用跳板机 间接 ssh

https://blog.csdn.net/DiamondXiao/article/details/52474104

命令法

1
ssh username@目标机器ip -p 22 -o ProxyCommand='ssh -p 22 username@跳板机ip -W %h:%p'

设置 config 配置

1
2
3
4
5
6
7
Host 10.10.0.*      #可以用*通配符

Port 22   #服务器的端口

User username   #服务器的用户

ProxyCommand ssh username_spring@spring -W %h:%p

eg:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Host            springboard
User            matgene
HostName        60.168.134.27
Port            6061
IdentityFile    ~/.ssh/SpringBoard


Host            office
User            sawyer
HostName        192.168.1.127
ProxyCommand    ssh springboard -W %h:%p

端口映射法

  • 先通过跳板机端口映射到本机
  • 在访问本机端口

port mapping

  • -N

    • 不登录,只做 mapping
  • -f

    • 后台运行

-L

1
ssh -L [local_ip:]local_port:to_visit_ip:to_visit_port Remote_Machine
  • 作用

    • 访问 (local_ip: local_port), 相当于 访问 (to_visit_ip: to_visit_port)
  • 用途

    • 问题解说

      • 本人是 A
      • 不能访问的网站 google.com 是 B
      • 外网机器 C 是能访问 B 的
    • 破防操作

      • 通过 C 来访问 B(google.com)
    1
    
      ssh (A)localhost:443:(B)www.google.com:443  name@C_machine
  • 记忆口诀

    • 抢端口,抢别人的端口

      • 用别人的端口
      • 好访问别人

-R

  • 解决问题

    • 在家中访问公司内网,但是公司可以访问外网(即家中)

      • 实现
    • 在公司做 ssh -R
    • 把能够访问服务的机器的端口好,映射到不能访问给定服务的机器上

      • 举例:学校的机器被禁用访问外网,
  • 格式

    1
    
    ssh -R  [remote_host:]remote_port:local_ip:local_port Remote_Machine(提供sshd 的机器)
    • 注解

      • remote_host
    • 省略

      • 表示 localhost, 即 只有 Remote_Machine 的本机 127.0.0.1
    • 其它

      • Remote_Machine 的 其它有效地址,如:192.168.1.127
  • 作用

    • 把本机(或内网的端口),送到远处

      • 即把内网的端口绑定到远程机器
  • 记忆口诀

    • 抛射端口,扔出去自己的端口

      • 给别人用
      • 让别人可以访问我们

调试 sshd

1
sshd -t

sshd 错误

没有 hostkeys

ssh 建立连接的密钥验证机制

参考:

涉及概念:

  • 会话 id
  • 会话密钥
  • 密码验证

    • 中间人攻击

防黑客

工具

cowrie

使用:

1
2
3
4
5
# 测试
docker run -p 2222:2222 cowrie/cowrie:latest

# 长期运行
docker run -d --name ssh-cowrie -p 2222:2222 cowrie/cowrie:latest

autossh 用法

功能:

  1. 支持 remote map 和 local map
  2. 自动 keep alive

systemd 用法

下述例子 -R, 把本地的 127.0.0.1:22 端口映射到服务器 theuser@server-ip 登录后的 server 上 127.0.0.1:9422 端口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[Unit]
Description=Keeps a tunnel to 'wsl sshd remote map' open
After=network.target

[Service]
# -p [PORT]
# -l [user]
# -M 0 --> no monitoring
# -N Just open the connection and do nothing (not interactive)
# LOCALPORT:IP_ON_EXAMPLE_COM:PORT_ON_EXAMPLE_COM
# ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -p 22 -l autossh host-name -R 10023:127.0.0.1:22
ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -l theuser server-ip -R 9422:127.0.0.1:22
Restart=on-failure

[Install]
WantedBy=default.target

实战

wsl + win10 rdp + remote map

功能:把宿主机 Win10 的 3389 端口,remote map (-R) 映射到远端(Home)机器上

注意事项:

  1. win10 宿主机

    • 需要在设置开启 rdp 远程桌面功能
    • 开启防火墙(如果失败,需要开启防火墙)
  2. wsl 设置 ssh -R 或者 auotssh -R

    • /usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" home -R :13389:<win10-host-ip>:3389
    • 注意这里在 wsl 上映射时,需要使用 win10 宿主机的 ip (eg: 192.168.1.100), 不能使用 localhost, 避免无效映射
  3. 无法映射到远端机器外网 ip 解决办法

    • 使用 socat 把本地端口转发到外网 ip 端口
    • sudo socat TCP-LISTEN:23389,fork,reuseaddr,bind=<home-pc-ip> TCP:localhost:13389
    • 这样在远端内容中,直接 rdp <home-pc-ip>:3389 就可以连接远端 win10 主机登录了

实现例子:

  • autossh win-host-ip:3389 -> home-pc-localhost:3389

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    [Unit]
    Description=Keeps a tunnel to 'office_win rdp remote map to laptop' open
    After=network.target
    
    [Service]
    # User=autossh
    # -p [PORT]
    # -l [user]
    # -M 0 --> no monitoring
    # -N Just open the connection and do nothing (not interactive)
    # LOCALPORT:IP_ON_EXAMPLE_COM:PORT_ON_EXAMPLE_COM
    ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" home -R :13389:192.168.1.100:3389
    
    [Install]
    WantedBy=default.target
  • socat 把本地端口 3389 转发到 wsl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    [Unit]
    Description=Socat Script service
    After=network.target
    
    [Service]
    ExecStart=socat TCP-LISTEN:23389,fork,reuseaddr,bind=<home-pc-ip> TCP:localhost:13389
    
    [Install]
    WantedBy=default.target

ssh + socks5 代理

启动代理服务

1
2
3
4
ssh -D ip:port user@remote_server

# eg:
ssh -D 192.168.1.100:7890 user@172.168.1.200
  • 服务 192.168.1.100:7890 会把流量转发到 172.168.1.200 机器上,再转到互联网上

使用代理服务

  • 使用本地 dns服务

    1
    
    https_proxy=socks5://192.168.1.100:7890 curl https://www.baidu.com
  • 使用远程 dns服务

    1
    
    https_proxy=socks5h://192.168.1.100:7890 curl https://www.baidu.com
    • 注意: socks5h –> 多了一个 h

ssh -R 如何允许绑定到 ip 上

命令:

1
2
3
4
ssh -R remote_ip:1080:localhost:1080 user@remote_server

ssh -R 0.0.0.0:1080:localhost:1080 user@remote_server
ssh -R 172.16.10.22:1080:localhost:1080 user@remote_server

绑定失败修复:

  • 修改 /etc/ssh/sshd_config

    1
    
    GatewayPorts yes