> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 쿼리 조합하기

> Attack Surface Query에서 배치, 관련 행, 목록, 필터, 집계를 조합해 실제 공격 표면 질문에 답합니다

`select`, `include`, `where`, 기간, 집계 같은 구성 요소는 자유롭게 조합할 수 있습니다. 이 페이지에서는 실제로 자주 묻는 질문에서 출발해 각 답 뒤에 있는 패턴을 보여 주고, 이어서 API에 바로 붙여 넣을 수 있는 완성된 배치 모음을 제공합니다.

## 패턴 1: 질문을 배치로 묶기

질문의 모든 부분을 하나의 배치에 넣습니다. 쿼리는 병렬로 실행되며, 한 쿼리가 실패해도 나머지는 멈추지 않습니다.

```json theme={null}
{
  "window": { "since": "now-7d" },
  "queries": {
    "newHosts": { "from": "hosts", "select": ["value", "kind", "target.value"] },
    "newPorts": { "from": "services", "select": ["host.value", "port", "http.title"] },
    "newFindings": { "from": "vulnerabilities", "select": ["name", "severity", "host.value"],
                     "where": { "logic": "and", "items": [{ "field": "isOpen", "op": "eq", "value": "true" }] } },
    "allTimePorts": { "from": "services", "groupBy": ["port"], "aggregate": { "n": { "fn": "count" } }, "window": null }
  }
}
```

배치의 `window`가 기본값이며, `allTimePorts`는 `"window": null`로 이를 사용하지 않습니다.

## 패턴 2: 관련 필드로 행 넓히기

질문의 대상이 되는 리소스에서 시작해 점 경로로 맥락을 가져옵니다. *"이번 달에 만료되는 TLS 인증서는 무엇이고, 어느 호스트와 포트에 있으며, 사이트에는 무엇이 표시되나요?"*

```json theme={null}
{ "from": "tls",
  "select": ["subjectCn", "notAfter", "daysToExpiry", "service.host.value", "service.port"],
  "where": { "logic": "and", "items": [{ "field": "notAfter", "op": "lt", "value": "now+30d" }] },
  "orderBy": [{ "field": "notAfter", "dir": "asc" }] }
```

## 패턴 3: 한 가지로 필터링하고 다른 것을 반환하기

`where`는 결과에 표시되지 않는 관련 행과 목록도 테스트할 수 있습니다. *"치명 발견 사항이 있는 호스트에서 WordPress를 실행하는 서비스."*

```json theme={null}
{ "from": "services",
  "select": ["host.value", "port", "http.title"],
  "where": { "logic": "and", "items": [
    { "field": "technologies.name", "op": "eq", "value": "WordPress" },
    { "field": "host.vulnerabilities.severity", "op": "eq", "value": "critical" } ] } }
```

필터 경로는 목록 앞뒤로 관련 행을 거칠 수 있지만(`host.vulnerabilities.severity`, `services.http.title`), 거칠 수 있는 목록은 **최대 하나**입니다. `hosts`에서 `services.technologies.name`을 사용하면 거부됩니다. 위 예시처럼 목록 하나만큼 더 가까운 리소스에서 시작하세요.

> [!NOTE]
>
> 각 목록 조건은 따로 검사됩니다. 두 번째 조건으로 `host.vulnerabilities.isOpen`을 추가해도, 호스트에 *어떤* 치명 발견 사항이 있다는 뜻일 뿐 그것이 열린 발견 사항이라는 보장은 없습니다. 여러 조건을 같은 관련 행에 묶으려면 [관계 테스트](https://docs.oasm.io/ko/attack-surface-query/filtering.md#relation-tests)(패턴 9)를 사용하거나, 해당 행의 리소스를 직접 조회하세요(패턴 4).

## 패턴 4: 부모가 아니라 자식을 조회하기

질문이 자식에 관한 것이라면 자식을 직접 읽고 관련 필드로 부모를 가져옵니다. include가 너무 커질 때의 해결 방법이기도 합니다. *"오래된 nginx를 실행하는 모든 서비스와 그 호스트."*

```json theme={null}
{ "from": "technologies",
  "select": ["name", "version", "service.host.value", "service.port"],
  "where": { "logic": "and", "items": [
    { "field": "name", "op": "eq", "value": "nginx" },
    { "field": "version", "op": "in", "value": ["1.18.0", "1.20.1", "1.22.0"] } ] },
  "limit": 200 }
```

## 패턴 5: 행 하나와 그 주변의 모든 것

`include`는 각 행에 목록을 붙입니다. 중첩하면 두 단계 아래까지 내려갈 수 있습니다. 부모의 `limit`는 작게 유지하세요. *"api.example.com에 대해 알려진 모든 것."*

```json theme={null}
{
  "queries": {
    "host": {
      "from": "hosts",
      "select": ["value", "kind", "firstSeen", "lastSeen", "target.value"],
      "limit": 1,
      "where": { "logic": "and", "items": [{ "field": "value", "op": "eq", "value": "api.example.com" }] },
      "include": {
        "services": {
          "select": ["port", "http.title", "http.statusCode", "http.webserver"],
          "limit": 20,
          "include": { "technologies": { "select": ["name", "version"] } }
        },
        "vulnerabilities": { "select": ["name", "severity", "detectionStatus"], "limit": 20 }
      }
    },
    "ips": {
      "from": "ips",
      "select": ["ip", "source"],
      "where": { "logic": "and", "items": [{ "field": "host.value", "op": "eq", "value": "api.example.com" }] }
    }
  }
}
```

IP는 세 번째 include가 아니라 같은 배치의 두 번째 쿼리로 가져옵니다. 쿼리 하나에는 중첩된 것을 포함해 include가 최대 다섯 개까지(AI 에이전트는 세 개) 허용됩니다.

## 패턴 6: 포함된 자식 필터링하기

include 자체의 `where`는 부모를 필터링하지 않고 어떤 자식을 붙일지만 제한합니다. *"모든 호스트와, 관리자 페이지처럼 보이는 서비스만."*

```json theme={null}
{ "from": "hosts", "select": ["value"], "limit": 50,
  "include": { "services": {
    "select": ["port", "http.title"], "limit": 5,
    "where": { "logic": "and", "items": [{ "field": "http.title", "op": "contains", "value": "admin" }] } } } }
```

그런 서비스가 *있는* 호스트만 남기려면 같은 조건을 목록 경로로 부모의 `where`에 추가합니다.

```json theme={null}
{ "from": "hosts", "select": ["value"], "limit": 50,
  "where": { "logic": "and", "items": [{ "field": "services.http.title", "op": "contains", "value": "admin" }] },
  "include": { "services": {
    "select": ["port", "http.title"], "limit": 5,
    "where": { "logic": "and", "items": [{ "field": "http.title", "op": "contains", "value": "admin" }] } } } }
```

## 패턴 7: 목록과 집계를 함께

목록과 그 분포는 같은 필터를 쓰는 한 배치의 두 쿼리입니다.

```json theme={null}
{
  "queries": {
    "critical": { "from": "vulnerabilities", "select": ["name", "cvssScore", "cve", "host.value"],
      "where": { "logic": "and", "items": [
        { "field": "isOpen", "op": "eq", "value": "true" },
        { "field": "severity", "op": "eq", "value": "critical" } ] },
      "orderBy": [{ "field": "cvssScore", "dir": "desc" }], "limit": 100 },
    "criticalPerHost": { "from": "vulnerabilities", "groupBy": ["host.value"], "aggregate": { "n": { "fn": "count" } },
      "where": { "logic": "and", "items": [
        { "field": "isOpen", "op": "eq", "value": "true" },
        { "field": "severity", "op": "eq", "value": "critical" } ] } }
  }
}
```

## 패턴 8: id로 후속 조회하기

모든 행에는 `id`가 있습니다. 다음 배치에서 이를 사용해 더 깊이 조회하거나 다른 OASM API에 전달하세요.

```json theme={null}
{ "from": "services", "select": ["port", "http.title"],
  "where": { "logic": "and", "items": [{ "field": "host.id", "op": "in", "value": ["1396ad91-…", "e4e5c8ca-…"] }] } }
```

## 패턴 9: 없는 것에 대해 묻기

가장 유용한 보안 질문 중 일부는 부재에 관한 것입니다. HTTP는 제공하지만 HTTPS는 제공하지 않는 호스트, 남은 호스트가 없는 대상, 아무도 핑거프린팅하지 않은 서비스 등입니다. 관계 테스트로 이를 직접 표현할 수 있습니다. *"80번 포트에서는 응답하지만 443번에는 아무것도 없는 호스트."*

```json theme={null}
{ "from": "hosts", "select": ["value", "target.value"], "limit": 100,
  "where": { "logic": "and", "items": [
    { "relation": "services", "where": { "logic": "and", "items": [{ "field": "port", "op": "eq", "value": "80" }] } },
    { "relation": "services", "quantifier": "none", "where": { "logic": "and", "items": [{ "field": "port", "op": "eq", "value": "443" }] } } ] } }
```

같은 형태로 *모두*(`"quantifier": "every"`: 호스트의 모든 포트가 웹 포트)와 *개수*(`"count": { "op": "gte", "value": "10" }`: 열린 포트가 10개 이상인 호스트)도 표현할 수 있습니다.

## 패턴 10: 쿼리 하나로 매트릭스 만들기

표에 카테고리별 열이 필요하다면 카테고리마다 쿼리를 실행하는 대신 각 집계에 자체 `where`를 지정합니다. 그런 다음 `having`으로 의미 있는 행만 남깁니다. *"열린 치명 및 높음 발견 사항 수로 순위를 매긴 대상, 해당 발견 사항이 있는 대상만."*

```json theme={null}
{ "from": "vulnerabilities",
  "groupBy": ["host.target.value"],
  "where": { "logic": "and", "items": [{ "field": "isOpen", "op": "eq", "value": "true" }] },
  "aggregate": {
    "critical": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "critical" }] } },
    "high": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "high" }] } },
    "hosts": { "fn": "countDistinct", "field": "host.id" } },
  "having": { "logic": "or", "items": [
    { "field": "critical", "op": "gt", "value": "0" },
    { "field": "high", "op": "gt", "value": "0" } ] },
  "orderBy": [{ "field": "critical", "dir": "desc" }, { "field": "high", "dir": "desc" }] }
```

## 패턴 11: 빈틈 없는 차트

차트용 시계열에는 조용한 날을 포함해 보는 사람의 시간대 기준으로 하루에 한 점씩 필요합니다. `fill`과 `options.timezone`이 정확히 이를 제공합니다. *"최근 30일 동안 하루별 새 포트와 새 발견 사항, 하노이 시간 기준."*

```json theme={null}
{
  "window": { "since": "now-30d" },
  "options": { "timezone": "Asia/Ho_Chi_Minh" },
  "queries": {
    "ports": { "from": "services", "groupBy": [{ "field": "firstSeen", "bucket": "day" }],
               "aggregate": { "n": { "fn": "count" } }, "fill": true },
    "findings": { "from": "vulnerabilities", "groupBy": [{ "field": "firstSeen", "bucket": "day" }],
                  "aggregate": { "n": { "fn": "count" },
                                 "critical": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "critical" }] } } },
                  "fill": true }
  }
}
```

두 시계열은 같은 날짜를 같은 순서로 가지므로 하나의 차트에 나란히 표시됩니다.

## 예제 모음

> [accordion] 대상과 대상별 호스트 수
>
> ```json theme={null}
> { "queries": {
>   "targets": { "from": "targets", "select": ["value", "type", "firstSeen"], "limit": 200 },
>   "hostsPerTarget": { "from": "hosts", "groupBy": ["target.value"], "aggregate": { "hosts": { "fn": "count" } },
>                       "orderBy": [{ "field": "hosts", "dir": "desc" }], "limit": 200 } } }
> ```

> [accordion] 최근 5일간의 새 도메인과 포트
>
> ```json theme={null}
> { "window": { "since": "now-5d" }, "queries": {
>   "domains": { "from": "hosts", "select": ["value", "firstSeen", "target.value"],
>                "where": { "logic": "and", "items": [{ "field": "kind", "op": "eq", "value": "domain" }] } },
>   "ports": { "from": "services", "select": ["host.value", "port", "http.title", "http.statusCode"] } } }
> ```

> [accordion] 노출된 위험 서비스
>
> ```json theme={null}
> { "from": "services", "select": ["host.value", "port", "http.title"], "limit": 200,
>   "where": { "logic": "and", "items": [
>     { "field": "port", "op": "in", "value": ["21", "23", "445", "3306", "3389", "5432", "6379", "9200", "27017"] } ] } }
> ```

> [accordion] 로그인 및 관리자 페이지
>
> ```json theme={null}
> { "from": "services", "select": ["host.value", "port", "http.url", "http.title"], "limit": 100,
>   "where": { "logic": "or", "items": [
>     { "field": "http.title", "op": "contains", "value": "admin" },
>     { "field": "http.title", "op": "contains", "value": "login" },
>     { "field": "technologies.name", "op": "in", "value": ["Jenkins", "Grafana", "Kibana", "phpMyAdmin"] } ] } }
> ```

> [accordion] 네트워크 대역 안의 호스트
>
> ```json theme={null}
> { "from": "ips", "select": ["ip", "host.value", "host.target.value"],
>   "where": { "logic": "and", "items": [{ "field": "ip", "op": "inSubnet", "value": ["203.0.113.0/24", "198.51.100.0/24"] }] } }
> ```

> [accordion] 사용 중인 기술 버전
>
> ```json theme={null}
> { "from": "technologies", "groupBy": ["name", "version"], "aggregate": { "services": { "fn": "count" } },
>   "orderBy": [{ "field": "services", "dir": "desc" }], "limit": 50 }
> ```

> [accordion] CVE가 존재하는 위치
>
> ```json theme={null}
> { "from": "vulnerabilities", "select": ["name", "severity", "host.value", "affectedUrl", "detectionStatus"],
>   "where": { "logic": "and", "items": [{ "field": "cve", "op": "eq", "value": "CVE-2024-3400" }] } }
> ```

> [accordion] 30일 동안 어떤 스캔에서도 발견되지 않은 호스트
>
> ```json theme={null}
> { "from": "hosts", "select": ["value", "lastSeen", "target.value"], "limit": 200,
>   "where": { "logic": "and", "items": [{ "field": "lastSeen", "op": "lt", "value": "now-30d" }] },
>   "orderBy": [{ "field": "lastSeen", "dir": "asc" }] }
> ```

> [accordion] 심각도별 주간 발견 사항
>
> ```json theme={null}
> { "window": { "since": "now-90d" }, "queries": { "trend": {
>   "from": "vulnerabilities",
>   "groupBy": [{ "field": "firstSeen", "bucket": "week" }, "severity"],
>   "aggregate": { "n": { "fn": "count" } }, "limit": 100 } } }
> ```

> [accordion] 30일 안에 만료되는 인증서, 와일드카드 우선
>
> ```json theme={null}
> { "from": "tls", "select": ["subjectCn", "notAfter", "wildcard", "service.host.value", "service.port"],
>   "where": { "logic": "and", "items": [{ "field": "notAfter", "op": "between", "value": ["now", "now+30d"] }] },
>   "orderBy": [{ "field": "notAfter", "dir": "asc" }] }
> ```

> [accordion] HTTP 응답이 없는 서비스
>
> ```json theme={null}
> { "from": "services", "select": ["host.value", "port", "lastSeen"], "limit": 200,
>   "where": { "logic": "and", "items": [{ "field": "http.title", "op": "isNull" }, { "field": "http.statusCode", "op": "isNull" }] } }
> ```

> [accordion] 프로덕션 호스트만(dev, staging, 내부 이름 제외)
>
> ```json theme={null}
> { "from": "hosts", "select": ["value", "target.value"], "limit": 200,
>   "where": { "logic": "and", "items": [{ "not": { "logic": "or", "items": [
>     { "field": "value", "op": "startsWith", "value": "dev" },
>     { "field": "value", "op": "startsWith", "value": "staging" },
>     { "field": "value", "op": "endsWith", "value": ".internal" } ] } }] } }
> ```

> [accordion] 두 CVE를 모두 가진 발견 사항
>
> ```json theme={null}
> { "from": "vulnerabilities", "select": ["name", "cve", "host.value"],
>   "where": { "logic": "and", "items": [{ "field": "cve", "op": "containsAll", "value": ["CVE-2024-3400", "CVE-2024-3401"] }] } }
> ```

> [accordion] 알려진 대역 밖의 호스트
>
> ```json theme={null}
> { "from": "ips", "select": ["ip", "host.value"], "limit": 200,
>   "where": { "logic": "and", "items": [{ "field": "ip", "op": "notInSubnet", "value": ["10.0.0.0/8", "172.16.0.0/12", "203.0.113.0/24"] }] } }
> ```

> [accordion] 특정 공급자를 가리키는 DNS 레코드
>
> ```json theme={null}
> { "from": "dnsRecords", "select": ["recordType", "value", "host.value"],
>   "where": { "logic": "and", "items": [
>     { "field": "recordType", "op": "eq", "value": "CNAME" },
>     { "field": "value", "op": "contains", "value": "cloudfront.net" } ] } }
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.