> For the complete documentation index, see [llms.txt](https://h08.gitbook.io/undefined/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://h08.gitbook.io/undefined/custom-rule-and-active-response/3./3.2-ssh.md).

# 3.2 비인가 이동 및 SSH 접근 통제 및 차단

## 1. 개요

* 공격자가 탈취한 프라이빗 키를 이용하여 관리자 접점인 Bastion Host에 SSH 접속하는 경우 감지하고 차단

***

## 2. 룰

### 109997 (접속 성공 탐지)&#x20;

```xml
<rule id="109997" level="3">
    <decoded_as>bastion_ssh_accepted</decoded_as>
    <description>Inbound SSH accepted (base rule)</description>
    <match>Accepted publickey</match>
    <group>bastion, ssh, inbound</group>
    <mitre>
      <id>T1078</id>
    </mitre>
  </rule>
```

### 109996 (SSH 명령 실행시 감지)

```xml
<rule id="109996" level="15">
    <decoded_as>bastion_outbound_ssh_execve</decoded_as>
    <match>ubuntu@</match>
    <description>Detected SSH command execution from Bastion Host</description>
    <group>bastion, ssh, outbound</group>
    <mitre>
      <id>T1021.004</id>
    </mitre>
  </rule>
```

***

## 3. 환경 설정

### 3.1 Manager

* Active Response (/var/ossec/etc/ossec.conf/)

  ```xml
  <ossec_config>
    <command>
      <name>firewall-drop</name>
      <executable>external_and_reboot.sh</executable> <timeout_allowed>yes</timeout_allowed>
    </command>

    <active-response>
      <command>firewall-drop</command>
      <location>local</location>
      <rules_id>109999</rules_id> <timeout>600</timeout>
    </active-response>
  </ossec_config>
  ```

* 위 xml 탐지룰

### 3.2 Agent

* Active Response

  ```bash
  #!/bin/bash
  # Script: external_and_reboot.sh

  LOG="/var/ossec/logs/active-responses.log"
  WHITELIST="/etc/wazuh/bastion_whitelist.txt"
  BLOCKED_LIST="/etc/wazuh/blocked_ip_list.txt"
  NOW=$(date "+%Y-%m-%d %H:%M:%S")

  # 1. Wazuh Alert Input 읽기
  read INPUT

  # 2. TTY 및 실제 접속 IP(REAL_IP) 추출
  TTY=$(echo "$INPUT" | grep -oP 'pts[0-9]+' | sed 's/pts/pts\//' | head -n1)
  REAL_IP=$(who | grep "$TTY" | awk '{print $5}' | tr -d '()' | tail -n1)

  if [[ -z "$REAL_IP" ]]; then
      echo "$NOW [ERROR] Could not resolve IP from TTY: $TTY" >> "$LOG"
      exit 1
  fi

  IP="$REAL_IP"

  # 3. 화이트리스트 확인 함수
  is_whitelisted() {
      grep -qFx "$IP" "$WHITELIST" 2>/dev/null
  }

  # 4. 차단 로직 실행
  if is_whitelisted; then
      echo "$NOW [INFO] $IP is in whitelist. No action taken." >> "$LOG"
      exit 0
  else
      # 차단 리스트 추가
      echo "$IP" >> "$BLOCKED_LIST"
      echo "$NOW [ALERT] Unauthorized SSH access from $IP. Blocking..." >> "$LOG"

      # 4-1. SSH 세션 강제 종료
      pkill -KILL -t "$TTY"

      # 4-2. 방화벽(iptables) 차단
      iptables -I INPUT -s "$IP" -j DROP
  fi
  ```

***

## 4. 작동원리

1. 접속 시도 및 성공

* 공격자가 탈취한 키를 사용하여 Bastion Host에 SSH 접속을 성공

2. Active Response 실행

* Wazuh Manager는 접속 이벤트를 감지하고 `external_and_reboot.sh` 스크립트를 트리거

3. IP 검증 및 판단

* 스크립트는 현재 로그인한 세션(`pts/x`)을 역추적하여 접속 IP를 파악
* 사전에 정의된 `bastion_whitelist.txt` 파일과 대조하여 인가된 IP인지 확인

4. 즉각적인 차단

* 화이트리스트에 없는 IP일 경우, 즉시 `iptables` DROP 규칙을 추가하여 통신을 끊는다
* 동시에 `pkill` 명령어로 현재 활성화된 SSH 쉘 프로세스를 강제 종료하여 공격자를 추방

<figure><img src="https://3550524140-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMQqlpxjtA7kpPBgieO9x%2Fuploads%2FRCnO9qV4fE1s4FV0Q6H8%2Fimage.png?alt=media&amp;token=b4e985e7-8da1-44c7-b076-dd96ca9a70b8" alt=""><figcaption></figcaption></figure>
