> ## 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: 疑問をバッチにまとめる

1 つの疑問を構成するすべての要素を 1 つのバッチに入れます。クエリは並列に実行され、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`）が、たどれるリストは **最大 1 つ** です。`hosts` からの `services.technologies.name` は拒否されます。その場合は、上の例のようにリスト 1 つ分近いリソースから始めてください。

> [!NOTE]
>
> 各リスト条件は個別に評価されます。`host.vulnerabilities.isOpen` を 2 つ目の条件として追加しても、ホストに *何らかの* 緊急の検出結果があるという意味であり、それが未解決とは限りません。複数の条件を同じ関連行に結びつけるには、[リレーションテスト](https://docs.oasm.io/ja/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: 1 行とその周辺のすべて

`include` は各行にリストを付加します。ネストすると 2 階層下までたどれます。親の `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 は 3 つ目の include ではなく、同じバッチ内の 2 つ目のクエリにしています。1 つのクエリの include は、ネストしたものも含めて合計 5 つまで（AI エージェントの場合は 3 つまで）です。

## パターン 6: include した子をフィルターする

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: 一覧と件数を同時に取得する

一覧とその分布は、同じフィルターを持つ 1 つのバッチ内の 2 つのクエリにします。

```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 は提供していないホスト、ホストが 1 つも残っていないターゲット、誰もフィンガープリントを取得していないサービスなどです。リレーションテストはこれを直接表現します。*「ポート 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"`: ホストのすべてのポートが Web ポート）や *件数*（`"count": { "op": "gte", "value": "10" }`: 開いているポートが 10 個以上のホスト）も表現できます。

## パターン 10: 1 つのクエリでマトリクスを作る

表にカテゴリごとの列が必要な場合は、カテゴリごとに 1 つのクエリを実行する代わりに、各集計に独自の `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: 欠けのないグラフ

グラフ用の系列には、閲覧者のタイムゾーンで、動きのない日も含めて 1 日 1 点が必要です。`fill` と `options.timezone` でまさにそれが得られます。*「過去 30 日間の 1 日ごとの新しいポートと新しい検出結果（ハノイ時間）。」*

```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 }
  }
}
```

両方の系列が同じ日を同じ順序で持つため、1 つのグラフ上で揃います。

## クックブック

> [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] 2 つの 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.