모두의 dream

SSH (Secure Shell) 본문

분야/Red Teaming

SSH (Secure Shell)

오리꽥이로 2024. 6. 7. 16:25
Contents 접기

SSH (Secure Shell)

보호되지 않는 네트워크를 통해 컴퓨터에 연결할 때 안전한 연결이 가능하도록 하는 네트워크 프로토콜. 

원격 시스템에 액세스할 때 데이터의 기밀성과 무결성을 유지하는데 필수적임.

작동 방식

언젠간 공부하지 않을까요~

SSH 활성화

user@user-None:~$ sudo service ssh start
user@user-None:~$ service ssh status
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; disabled; preset: enabled)
    Drop-In: /etc/systemd/system/ssh.service.d
             └─00-socket.conf
     Active: active (running) since Mon 2024-06-10 10:09:03 KST; 4s ago
TriggeredBy: ● ssh.socket
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 3807 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 3808 (sshd)
      Tasks: 1 (limit: 19063)
     Memory: 1.4M
        CPU: 31ms
     CGroup: /system.slice/ssh.service
             └─3808 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jun 10 10:09:03 user-None systemd[1]: Starting ssh.service - OpenBSD Secure She>
Jun 10 10:09:03 user-None sshd[3808]: Server listening on :: port 22.
Jun 10 10:09:03 user-None systemd[1]: Started ssh.service - OpenBSD Secure Shel>

SSH 설정 파일

파일 1: /etc/ssh/sshd_config

파일 2: /lib/systemd/system/ssh.socket

 

일부 옵션을 정리해봤다.

1. PermitRootLogin 

root 계정 로그인 허용 여부는 PermitRootLogin 옵션을 변경해주면 된다.

PermitRootLogin yes

# 옵션 정보
prohibit-password: Key 파일을 통해 로그인이 가능. (비밀번호 로그인은 안됨)
yes : root 계정 비밀번호 입력 로그인 가능
no : root 계정 로그인 불가능

2. Port 변경

Port 변경은 Port 옵션을 변경해주면 되는데 Ubuntu 22.10 부터는 /sshd_config 파일이 아닌 /lib/systemd/system/ssh.socket 파일을 수정해야 된다. (ssh.socket의 ListenStream 옵션을 수정해주면 된다.)

/etc/ssh/sshd_config

Port 22

/lib/systemd/system/ssh.socket

ListenStream=3237
user@user-None:~$ sudo systemctl daemon-reload
user@user-None:~$ sudo systemctl restart ssh.socket
user@user-None:~$ systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; disabled; preset: enabled)
    Drop-In: /etc/systemd/system/ssh.service.d
             └─00-socket.conf
     Active: active (running) since Mon 2024-06-10 14:18:15 KST; 11s ago
TriggeredBy: ● ssh.socket
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 13810 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 13811 (sshd)
      Tasks: 1 (limit: 19063)
     Memory: 1.4M
        CPU: 23ms
     CGroup: /system.slice/ssh.service
             └─13811 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jun 10 14:18:15 user-None systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Jun 10 14:18:15 user-None sshd[13811]: Server listening on :: port 3237.
Jun 10 14:18:15 user-None systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
user@user-None:~$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
---- 중략 ----
tcp6       0      0 :::3237                 :::*                    LISTEN      1/systemd

3. SSH 공개키 자동 로그인

공개키를 이용한 로그인을 허용하려면 아래 내용을 주석 해제한다.

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile     .ssh/authorized_keys

서버 연결 방법

일반적인 연결

user 계정으로 연결하는 방법은 다음과 같다.

포트를 기본 포트에서 3237로 변경했기 때문에 포트도 함께 지정한다.

user@DESKTOP-HBLKCUU:~$ ssh user@192.168.111.129
ssh: connect to host 192.168.111.129 port 22: Connection refused
user@DESKTOP-HBLKCUU:~$ ssh user@192.168.111.129 -p 3237
user@192.168.111.129's password:
Welcome to Ubuntu 23.10 (GNU/Linux 6.5.0-28-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

44 updates can be applied immediately.
10 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

New release '24.04 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

*** System restart required ***
Last login: Mon Jun 10 14:15:39 2024 from 192.168.111.1
user@user-None:~$ whoami
user
user@user-None:~$ pwd
/home/user

 

root 계정으로 연결하는 방법은 다음과 같다.

root 계정 접속도 허용했으므로 테스트 해본다.

user@DESKTOP-HBLKCUU:~$ ssh root@192.168.111.129 -p 3237
root@192.168.111.129's password:
Welcome to Ubuntu 23.10 (GNU/Linux 6.5.0-28-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

44 updates can be applied immediately.
10 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

New release '24.04 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

*** System restart required ***
Last login: Mon Jun 10 10:10:42 2024 from 192.168.111.1
root@user-None:~# whoami
root
root@user-None:~# pwd
/root

key(public, private) 를 이용한 연결

ssh 는 클라이언트 검증 과정에서 비대칭키 방식을 이용한다.

서버 혹은 클라이언트에서 발급한 공개키와 개인키를 이용하면 비밀번호 입력 없이(비밀번호 설정 가능) 접속할 수 있다.

레드 티밍을 진행할 때 공격 대상이 비밀번호를 바꿨을 경우에도 접속이 가능하다.

따라서 key 파일을 백도어로 활용하여 persistence를 유지할 수 있다. 

키 생성

키 생성시 따로 암호를 입력하지 않았기 때문에 ssh로 접속할 때 암호를 묻지 않게 된다.

 

1. 클라이언트에서 키 생성

user@user-None:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): ^C
user@user-None:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub

user@user-None:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts  known_hosts.old

 

생성된 공개키를 서버에 등록한다. 

user@user-None:~/.ssh$ cat >> authorized_keys
공개키 입력
Ctrl + D (저장)

 

이제 접속해보면 비밀번호 입력 없이 바로 접속할 수 있다.

user@DESKTOP-HBLKCUU:~/.ssh$ ssh -i id_rsa user@192.168.111.129 -p 3237
Welcome to Ubuntu 23.10 (GNU/Linux 6.5.0-28-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

44 updates can be applied immediately.
10 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

New release '24.04 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

*** System restart required ***
Last login: Mon Jun 10 14:31:30 2024 from 192.168.111.1
user@user-None:~$ whoami
user
user@user-None:~$ pwd
/home/user

 

2. 서버에서 키 생성

서버 본인의 공개키를 등록한다.

user@user-None:~/.ssh$ cat >> authorized_keys
공개키 입력
Ctrl + D (저장)

 

서버의 개인키를 복사한 후 클라이언트에 복사한다.

user@DESKTOP-HBLKCUU:~/.ssh$ cat >> server_private
개인키 입력
Ctrl + D (저장)

 

여기서 이런 오류가 출력될 수 있다.

user@DESKTOP-HBLKCUU:~/.ssh$ ssh -i server_private user@192.168.111.129 -p 3237
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'server_private' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "server_private": bad permissions
user@192.168.111.129's password:

 

해결 방법은 클라이언트에 생성한 비밀키 파일의 권한을 600으로 변경시켜주면 된다.

user@DESKTOP-HBLKCUU:~/.ssh$ chmod 600 server_private
user@DESKTOP-HBLKCUU:~/.ssh$ ssh -i server_private user@192.168.111.129 -p 3237
Welcome to Ubuntu 23.10 (GNU/Linux 6.5.0-28-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

44 updates can be applied immediately.
10 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

New release '24.04 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

*** System restart required ***
Last login: Mon Jun 10 16:22:09 2024 from 192.168.111.1

 

근데 만약 백도어 용도로 키를 생성한다면 클라이언트에서 생성한 공개키를 서버로 전송하는 것 보단, 서버에서 생성한 비밀키를 클라이언트에게 보내는 방법이 더 좋지 않을까 생각된다.

Comments