
ホスト、サービス、およびコンテンツのパターン認識
# Recog: 認識フレームワーク
[](https://github.com/rapid7/recog/actions/workflows/ci.yml)
[](https://github.com/rapid7/recog/actions/workflows/verify.yml)
Recogは、さまざまなネットワークプローブから返されるデータに対してフィンガープリントをマッチングすることで、製品、サービス、オペレーティングシステム、ハードウェアを識別するためのフレームワークです。Recogを使用すると、Webサーバーのバナー、SNMPシステム記述フィールド、その他多数から有用な情報を簡単に抽出できます。
Recogはオープンソースです。詳細については、[LICENSE](https://github.com/rapid7/recog/blob/main/LICENSE)ファイルを参照してください。
## 目次
1. [リポジトリの分割](#repository-split)
1. [デフォルトブランチの名称変更](#default-branch-rename)
1. [Recogライブラリの言語実装](#recog-library-language-implementations)
1. [機能の同等性](#feature-parity)
1. [インストール](#installation)
1. [成熟度](#maturity)
1. [フィンガープリント](#fingerprints)
1. [マッチのテスト](#testing-matches)
1. [ftp_banners](#ftp_banners)
1. [http_cookies](#http_cookies)
1. [http_servers](#http_servers)
1. [favicons](#favicons)
1. [http_wwwauth](#http_wwwauth)
1. [tls_jarm](#tls_jarm)
1. [コントリビューション](#contributing)
## リポジトリの分割
2022年3月31日、Recogのコンテンツ(XMLフィンガープリントファイルとユーティリティ)は、Recogフレームワークライブラリの実装から分割されました。元の[Recog](https://github.com/rapid7/recog)リポジトリにはRecogのコンテンツが含まれ、[Recog-Ruby](https://github.com/rapid7/recog-ruby)リポジトリにはRuby言語実装が含まれています。Recogのコンテンツは、gitサブモジュールとしてRecog-Rubyに含まれ、`recog`ディレクトリの下に配置されています。分割後のRecog gemバージョン3.0.0以上では、1. `recog`ディレクトリの下にXMLフィンガープリントディレクトリが含まれ、2. 他のツールはフィンガープリント管理に焦点を当てているため、`recog_match`ツールのみが含まれます。
[^back to top](#recog-a-recognition-framework)
### デフォルトブランチの名称変更
リポジトリの分割に伴い、デフォルトブランチは`master`から`main`に名称変更されました。これらの変更より前に作成されたクローンは、ローカル環境で手動で更新する必要があります。フォークを使用している場合は、フォークの設定に移動し、[ブランチの名称変更](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-branches-in-your-repository/renaming-a-branch)の手順に従って、デフォルトブランチを`main`に変更してください。
```
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
```
必要に応じて、次のコマンドを実行して、以前のブランチ名への追跡参照を削除します。
```
# dry-run to confirm stale references that will be deleted before proceeding
git remote prune origin --dry-run
git remote prune origin
```
以前に上流追跡ブランチ`upstream-master`を使用していた場合は、次のコマンドを実行して古いブランチを削除し、新しい上流追跡ブランチを作成してください。
```
git branch -d upstream-master
git checkout -b upstream-main --track upstream/main
```
必要に応じて、次のコマンドを実行して、以前の上流ブランチ名への追跡参照を削除します。
```
# dry-run to confirm stale references that will be deleted before proceeding
git remote prune upstream --dry-run
git remote prune upstream
```
[^back to top](#recog-a-recognition-framework)
## Recogライブラリの言語実装
* Ruby: [rapid7/recog-ruby](https://github.com/rapid7/recog-ruby)
* Java: [rapid7/recog-java](https://github.com/rapid7/recog-java)
* Go: [runZeroInc/recog-go](https://github.com/runZeroInc/recog-go)
[^back to top](#recog-a-recognition-framework)
### 機能の同等性
| 機能 :sparkles: | rapid7/recog-ruby | rapid7/recog-java | runZeroInc/recog-go |
|---------------------------------------------|:------------------:|:------------------:|:-------------------:|
| フィンガープリント検証CLIツール | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| フィンガープリントマッチCLIツール | :white_check_mark: | | :white_check_mark: |
| base64エンコードされた例のサポート | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| ファイルシステムベースの外部例のサポート | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| フィンガープリントマッチCPEパラメータ補間 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
[^back to top](#recog-a-recognition-framework)
## インストール
Recogは、XMLフィンガープリントファイルと、主にRubyで書かれたさまざまなコードで構成されており、含まれているフィンガープリントの開発、テスト、使用を容易にします。含まれているRubyコードを使用するには、Rubyの最近のバージョン(2.31+)に加えて、Rubygemsと`bundler` gemが必要です。これらの依存関係が整ったら、次のコマンドを使用して最新のソースコードを取得し、追加の依存関係をインストールします。
```shell
$ git clone [email protected]:rapid7/recog.git
$ cd recog
$ bundle install
```
[^back to top](#recog-a-recognition-framework)
## 成熟度
XMLフィンガープリント自体は非常に安定しており十分にテストされていますが、Rubyコードベースはまだかなり新しく、急速に変更される可能性があることに注意してください。本番プロジェクトでRecogコードを利用する前に、私たち(research[at]rapid7.com)までご連絡ください。
[^back to top](#recog-a-recognition-framework)
## フィンガープリント
Recog内のフィンガープリントはXMLファイルに格納されており、各ファイルは特定のプロトコル応答文字列またはフィールドにマッチするように設計されています。たとえば、[ssh_banners.xml](https://github.com/rapid7/recog/blob/main/xml/ssh_banners.xml)ファイルは、SSHデーモンの初期バナー文字列をマッチングすることで、OS、ベンダー、場合によってはハードウェア製品を特定できます。
フィンガープリントファイルは、次のようなXMLドキュメントで構成されています:
```xml
<fingerprints matches="ssh.banner">
<fingerprint pattern="^RomSShell_([\d\.]+)$">
<description>Allegro RomSShell SSH</description>
<example service.version="4.62">RomSShell_4.62</example>
<param pos="0" name="service.vendor" value="Allegro"/>
<param pos="0" name="service.product" value="RomSShell"/>
<param pos="1" name="service.version"/>
</fingerprint>
</fingerprints>
```
最初の行は常にXMLバージョン宣言で構成する必要があります。最初の要素は常に`fingerprints`ブロックであり、このフィンガープリントファイルがマッチすることを想定しているデータを示す`matches`属性を持つ必要があります。`matches`属性は通常、`protocol.field`の形式です。
`fingerprints`要素の中には、1つ以上の`fingerprint`要素が必要です。各`fingerprint`には、データのマッチングに使用する正規表現を含む`pattern`属性が必須です。正規表現の解釈方法を制御するために、オプションの`flags`属性を指定できます。詳細については、[`FLAG_MAP`に関するRecogのドキュメント](http://www.rubydoc.info/gems/recog/Recog/Fingerprint/RegexpFactory#FLAG_MAP-constant)を参照してください。
フィンガープリント内の`description`要素には、このフィンガープリントを説明する人間が読める文字列を含める必要があります。
少なくとも1つの`example`要素が存在する必要がありますが、複数の`example`要素が推奨されます。これらの要素は、提供されたデータが指定された正規表現にマッチすることを検証する`rspec`のテストカバレッジの一部として使用されます。さらに、フィンガープリントが`param`要素を使用してデータからフィールド値を抽出する場合(次で説明)、これらの期待される抽出値を`example`要素の属性として追加できます。上記の例では、これ:
```xml
<example service.version="4.62">RomSShell_4.62</example>
```
は、`RomSShell_4.62`が提供された正規表現にマッチし、`service.version`の値が4.62であることをテストします。
`example`文字列は、印刷できない文字を使用できるようにbase64エンコードできます。これをRecogに通知するには、値が`base64`の`_encoding`属性を`example`要素に追加します。80文字を超えるBase64エンコードされたテキストは、読みやすくするために、下に示すように改行で折り返すことができます。
```xml
<example _encoding="base64">
dGllczGEAAAAlQQWMS4yLjg0MC4xMTM1NTYuMS40LjgwMAQuZGF0YS5yZW1vdmVkLjCEAAAAK
AQdZG9tYWluQ29udHJvbGxlckZ1bmN0aW9uYWxpdHkxhAAAAAMEATc=
</example>
```
さらに、例はXMLファイルと同じディレクトリ内の、XMLファイルと同名のディレクトリに配置できます:
```
xml/services.xml
xml/services/file1
xml/services/file2
...
```
その後、`_filename`属性を使用して読み込むことができます:
```xml
<example _filename="file1"/>
```
これは長い例に役立ちます。
`param`要素には`pos`属性が含まれており、`pattern`から抽出するキャプチャフィールド、または静的文字列の場合は`0`を示します。`name`属性は、マッチが成功した場合に報告されるキーであり、`value`は`pos`の値が`0`の場合は静的文字列、または欠落している場合はキャプチャされたフィールドから取得されます。
`value`属性は、他のフィールドからのデータの補間をサポートしています。これは、正規表現で`hw.product`の値をキャプチャし、その値を`os.product`で再利用する場合に役立ちます。
`http_servers.xml`からの例では、`hw.product`がキャプチャされ再利用されています。
```xml
<fingerprint pattern="^Eltex (TAU-\d+[A-Z]*(?:\.IP)?)$">
<description>Eltex TAU model VoIP gateway</description>
<example hw.product="TAU-72">Eltex TAU-72</example>
<example hw.product="TAU-1.IP">Eltex TAU-1.IP</example>
<param pos="0" name="os.vendor" value="Eltex"/>
<param pos="0" name="os.product" value="{hw.product} Firmware"/>
<param pos="0" name="os.device" value="VoIP Gateway"/>
<param pos="0" name="hw.vendor" value="Eltex"/>
<param pos="1" name="hw.product"/>
<param pos="0" name="hw.device" value="VoIP Gateway"/>
</fingerprint>
```
`_tmp.`で始まる名前を持つ一時属性には特別な処理があります。これらの属性は補間に使用できますが、出力には出力されません。これは、特定の製品名がさまざまなバナー、ベンダーのマーケティング、またはCPEを生成しようとするときのNIST値と一貫性がない場合に役立ちます。これらの場合、バナーの有用な部分を抽出し、マッチによって出力されるデータを散らかすことなく新しい値を生成できます。
```xml
<fingerprint pattern="^foo baz switchThing-(\d{4})$">
<description>NetCorp NX series switches</description>
<example hw.product="NX8200">foo baz switchThing-8200</example>
<param pos="0" name="hw.vendor" value="NetCorp"/>
<param pos="0" name="hw.product" value="NX{_tmp.001}"/>
<param pos="2" name="_tmp.001"/>
</fingerprint>
```
これらの一時属性は、`identifiers/fields.txt`では追跡されません。
[^back to top](#recog-a-recognition-framework)
### マッチのテスト
次の例では、Recogのフィンガープリントデータを使用するシンプルなRubyコマンドラインツールである`bin/recog_match`を使用します。Recogを実行する前に前処理が一般的に必要です。つまり、HTTPヘッダー値の抽出などです。すべてのフィンガープリントデータは`xml/*.xml`にあります。
#### ftp_banners
接続後のサーバーのバナー応答に基づいてFTPサーバーをフィンガープリントします:
```
# Example plaintext input
echo -n '220 Microsoft FTP Service' | bin/recog_match xml/ftp_banners.xml -
# Example command using nmap
nmap -sV -script=banner -p 21 192.168.123.13 | grep --color=never '_banner' | cut -d: -f2- | bin/recog_match xml/ftp_banners.xml -
# Example output
MATCH: {"matched"=>"Microsoft FTP Server on Windows XP, 2003 or later without version", "service.vendor"=>"Microsoft", "service.product"=>"IIS", "service.family"=>"IIS", "service.cpe23"=>"cpe:/a:microsoft:iis:-", "os.vendor"=>"Microsoft", "os.family"=>"Windows", "os.product"=>"Windows", "os.cpe23"=>"cpe:/o:microsoft:windows:-", "host.name"=>"220", "service.protocol"=>"ftp", "fingerprint_db"=>"ftp.banner", "data"=>"220 Microsoft FTP Service"}
```
#### http_cookies
HTTP `Set-Cookie`ヘッダー値を使用してHTTPサーバーをフィンガープリントします:
```
# Example plaintext input
echo 'laravel_session=eyJ...etc..etc...%3D; expires=Mon, 13-Mar-2023 16:48:58 GMT; Max-Age=7200; path=/; httponly; samesite=lax' | bin/recog_match xml/http_cookies.xml -
# Example cURL command
curl --silent -I http://localhost:9001 | grep --color=never -i '^Set-Cookie:' | cut -d: -f2- | bin/recog_match xml/http_cookies.xml -
# Example output
MATCH: {"matched"=>"Laravel PHP web application framework", "service.vendor"=>"Laravel", "service.product"=>"Laravel", "service.cpe23"=>"cpe:/a:laravel:laravel:-", "service.protocol"=>"http", "fingerprint_db"=>"http_header.cookie", "data"=>"laravel_session=eyJ...etc..etc...%3D; expires=Mon, 13-Mar-2023 16:48:58 GMT; Max-Age=7200; path=/; httponly; samesite=lax"}
```
#### http_servers
HTTP `Server`ヘッダー値を使用してHTTPサーバーをフィンガープリントします:
```
# Example plaintext input
echo -n 'Apache/2.4.38 (Debian)' | bin/recog_match xml/http_servers.xml -
# Example cURL command
curl --silent -I http://localhost:9001 | grep --color=never -i '^Server:' | cut -d: -f2- | bin/recog_match xml/http_servers.xml -
# Example output
MATCH: {"matched"=>"Apache", "service.vendor"=>"Apache", "service.product"=>"HTTPD", "service.family"=>"Apache", "service.version"=>"2.4.38", "service.cpe23"=>"cpe:/a:apache:http_server:2.4.38", "apache.info"=>"(Debian)", "service.protocol"=>"http", "fingerprint_db"=>"http_header.server", "data"=>"Apache/2.4.38 (Debian)"}
```
#### favicons
ファビコンのmd5sumを使用して実行中のサービスを識別します:
```
# Example plaintext input
echo -n fe22dd2bb09daccf58256611ac491469 | bin/recog_match xml/favicons.xml -
# Example cURL command
curl --silent http://localhost:8000/favicon.ico | md5sum | awk '{ print $1 }' | bin/recog_match xml/favicons.xml -
# Example output
MATCH: {"matched"=>"Drupal CMS", "service.vendor"=>"Drupal", "service.product"=>"CMS", "service.certainty"=>"0.5", "service.cpe23"=>"cpe:/a:drupal:drupal:-", "service.protocol"=>"", "fingerprint_db"=>"favicon.md5", "data"=>"fe22dd2bb09daccf58256611ac491469"}
```
#### http_wwwauth
HTTP `WWW-Authenticate`ヘッダー値を使用してHTTPサーバーをフィンガープリントします:
```
# Example plaintext input
echo -n 'Basic realm="monit"' | bin/recog_match xml/http_wwwauth.xml -
# Example cURL command
curl --silent -I http://localhost:9001 | grep --color=never -i '^WWW-Authenticate:' | cut -d: -f2- | bin/recog_match xml/http_wwwauth.xml -
# Example output
MATCH: {"matched"=>"Minot", "service.vendor"=>"Tildeslash", "service.product"=>"Monit", "service.cpe23"=>"cpe:/a:tildeslash:monit:-", "service.protocol"=>"http", "fingerprint_db"=>"http_header.wwwauth", "data"=>"Basic realm=\"monit\""}
```
#### tls_jarm
10個のTLS Client Helloパケットに対するサーバーの応答に基づいてTLSサーバーをフィンガープリントします。https://github.com/salesforce/jarm に基づくフィンガープリント
```
# Example plaintext input
echo -n 07d14d16d21d21d07c42d43d000000f50d155305214cf247147c43c0f1a823 | bin/recog_match xml/jarm.xml -
# Example command using Salesforce's JARM against a running Metasploit listener
python3 $code/jarm/jarm.py -p 8443 192.168.123.1 | grep --color=never 'JARM: ' | awk -F: '{ print $2 }' | bin/recog_match xml/tls_jarm.xml -
# Example output
MATCH: {"matched"=>"Metasploit listener", "service.vendor"=>"Rapid7", "service.product"=>"Metasploit", "service.cpe23"=>"cpe:/a:rapid7:metasploit:-", "service.protocol"=>"tls", "fingerprint_db"=>"tls.jarm", "data"=>"07d14d16d21d21d07c42d43d000000f50d155305214cf247147c43c0f1a823"}
```
## コントリビューション
Recogのユーザーとメンテナーは、プロジェクトへのあらゆる貢献を大いに歓迎します。ガイドラインと手順については、[CONTRIBUTING.MD](https://github.com/rapid7/recog/blob/main/CONTRIBUTING.md)を参照してください。
[^back to top](#recog-a-recognition-framework)