megは、多数のURLを取得しながらも、サーバーに対して「優しく」振る舞うためのツールです。
多くのホストに対して多くのパスを取得するために使用できます。すべてのホストに対して1つのパスを取得してから、次のパスに移り、それを繰り返します。
結果は大量に素早く得られますが、個々のホストがトラフィックで溢れることはありません。
megはGoで書かれており、実行時の依存関係はありません。Go 1.9以降がインストールされ設定されていれば、go installでmegをインストールできます。
▶ go install github.com/tomnomnom/meg@latest
またはバイナリをダウンロードして、$PATHの通った場所(例:/usr/bin/)に配置してください。
次のようなエラーが表示される場合、Goのバージョンが古すぎます。
# github.com/tomnomnom/rawhttp
/root/go/src/github.com/tomnomnom/rawhttp/request.go:102: u.Hostname undefined (
type *url.URL has no field or method Hostname)
/root/go/src/github.com/tomnomnom/rawhttp/request.go:103: u.Port undefined (type
*url.URL has no field or method Port)
/root/go/src/github.com/tomnomnom/rawhttp/request.go:259: undefined: x509.System
CertPool
Goのバージョンをアップデートするか、お使いのプラットフォーム向けのバイナリリリースを使用してください。
パスが列挙されたファイルがあるとします:
/robots.txt
/.well-known/security.txt
/package.json
そして、ホスト(プロトコル付き)が列挙されたファイルがあります:
http://example.com
https://example.com
http://example.net
megは、すべてのホストに対して各パスをリクエストします:
▶ meg --verbose paths hosts
out/example.com/45ed6f717d44385c5e9c539b0ad8dc71771780e0 http://example.com/robots.txt (404 Not Found)
out/example.com/61ac5fbb9d3dd054006ae82630b045ba730d8618 https://example.com/robots.txt (404 Not Found)
out/example.net/1432c16b671043271eab84111242b1fe2a28eb98 http://example.net/robots.txt (404 Not Found)
out/example.net/61deaa4fa10a6f601adb74519a900f1f0eca38b7 http://example.net/.well-known/security.txt (404 Not Found)
out/example.com/20bc94a296f17ce7a4e2daa2946d0dc12128b3f1 http://example.com/.well-known/security.txt (404 Not Found)
...
そして、出力を./outディレクトリに保存します:
▶ head -n 20 ./out/example.com/45ed6f717d44385c5e9c539b0ad8dc71771780e0
http://example.com/robots.txt
> GET /robots.txt HTTP/1.1
> Host: example.com
< HTTP/1.1 404 Not Found
< Expires: Sat, 06 Jan 2018 01:05:38 GMT
< Server: ECS (lga/13A2)
< Accept-Ranges: bytes
< Cache-Control: max-age=604800
< Content-Type: text/*
< Content-Length: 1270
< Date: Sat, 30 Dec 2017 01:05:38 GMT
< Last-Modified: Sun, 24 Dec 2017 06:53:36 GMT
< X-Cache: 404-HIT
<!doctype html>
<html>
<head>
引数なしで実行すると、megは./pathsファイルからパスを、./hostsファイルからホストを読み込みます。出力は表示されません:
▶ meg
▶
ただし、./out/indexにインデックスファイルが保存されます:
▶ head -n 2 ./out/index
out/example.com/538565d7ab544bc3bec5b2f0296783aaec25e756 http://example.com/package.json (404 Not Found)
out/example.com/20bc94a296f17ce7a4e2daa2946d0dc12128b3f1 http://example.com/.well-known/security.txt (404 Not Found)
インデックスファイルを使用してレスポンスの保存場所を見つけることもできますが、多くの場合grepを使う方が簡単です:
▶ grep -Hnri '< Server:' out/
out/example.com/61ac5fbb9d3dd054006ae82630b045ba730d8618:14:< Server: ECS (lga/13A2)
out/example.com/bd8d9f4c470ffa0e6ec8cfa8ba1c51d62289b6dd:16:< Server: ECS (lga/13A3)
パスを1つだけリクエストしたい場合は、引数として直接指定できます:
▶ meg /admin.php
megのヘルプ出力は実際に役立つように作られています:
▶ meg --help
Request many paths for many hosts
Usage:
meg [options] [path|pathsFile] [hostsFile] [outputDir]
Options:
-c, --concurrency <val> Set the concurrency level (defaut: 20)
-d, --delay <val> Milliseconds between requests to the same host (default: 5000)
-H, --header <header> Send a custom HTTP header
-r, --rawhttp Use the rawhttp library for requests (experimental)
-s, --savestatus <status> Save only responses with specific status code
-v, --verbose Verbose mode
-X, --method <method> HTTP method (default: GET)
Defaults:
pathsFile: ./paths
hostsFile: ./hosts
outputDir: ./out
Paths file format:
/robots.txt
/package.json
/security.txt
Hosts file format:
http://example.com
https://example.edu
https://example.net
Examples:
meg /robots.txt
meg -s 200 -X HEAD
meg -c 30 /
meg hosts.txt paths.txt output
デフォルトでは、megは20の同時リクエストを試みます。-cまたは--concurrencyオプションで変更できます:
▶ meg --concurrency 5
同時実行数をホスト数よりも高く保つのはあまり好ましくありません。1つのホストに一度に大量のリクエストを送信する可能性があります。
デフォルトでは、megは同一ホストへのリクエスト間に5000ミリ秒の待機時間を設けます。-dまたは--delayオプションで上書きできます:
▶ meg --delay 10000
警告: 遅延を短縮する前に、ターゲットとするホストに大量のリクエストを送信する許可があることを確認してください。
-Hまたは--headerオプションを使用して、リクエストに追加のヘッダーを設定できます:
▶ meg --header "Origin: https://evil.com"
▶ grep -h '^>' out/example.com/*
> GET /.well-known/security.txt HTTP/1.1
> Origin: https://evil.com
> Host: example.com
...
無効なリクエスト(例えば無効なURLエンコーディング)を送信したい場合、GoのHTTPクライアントは失敗します:
▶ meg /%%0a0afoo:bar
request failed: parse https://example.org/%%0a0afoo:bar: invalid URL escape "%%0"
-rまたは--rawhttpフラグを使用して、rawhttpライブラリを有効にできます。このライブラリはリクエストに対してほとんど検証を行いません:
▶ meg --verbose --rawhttp /%%0a0afoo:bar
out/example.com/eac3a4978bfb95992e270c311582e6da4568d83d https://example.com/%%0a0afoo:bar (HTTP/1.1 404 Not Found)
rawhttpライブラリとその使用は実験的です。とりわけ、まだチャンク転送エンコーディングをサポートしていないため、使用すると出力にチャンク長が混ざることがあります。
特定のステータスコードを返した結果だけを保存したい場合は、-sまたは--savestatusオプションを使用します:
▶ meg --savestatus 200 /robots.txt
-Xまたは--methodオプションで使用するHTTPメソッドを指定できます:
▶ meg --method TRACE
▶ grep -nri 'TRACE' out/
out/example.com/61ac5fbb9d3dd054006ae82630b045ba730d8618:3:> TRACE /robots.txt HTTP/1.1
out/example.com/bd8d9f4c470ffa0e6ec8cfa8ba1c51d62289b6dd:3:> TRACE /.well-known/security.txt HTTP/1.1
...