Jenkins 任意文件读取漏洞复现与分析-CVE-2018-1999002

一、SECURITY-914 / CVE-2018-1999002

An arbitrary file read vulnerability in the Stapler web framework used by Jenkins allowed unauthenticated users to send crafted HTTP requests returning the contents of any file on the Jenkins master file system that the Jenkins master process has access to.

Input validation in Stapler has been improved to prevent this.

漏洞影响版本:

Jenkins weekly up to and including 2.132
Jenkins LTS up to and including 2.121.1

二、漏洞复现

测试环境: win平台

通过查找commit记录可知需要将其检出至 29ca81dd59c255ad633f1bd86cf1be40a5f02c64之前

> git clone https://github.com/jenkinsci/jenkins.git
> git checkout 40250f08aca7f3f8816f21870ee23463a52ef2f2

检查core/pom.xml的第41行,确保版本为1.250

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;"><staplerFork>true</staplerFork>
<stapler.version>1.250</stapler.version>
</pre>

然后命令行下编译war包

mvn clean install -pl war -am -DskipTests

jenkins\war\target目录下获得编译好的jenkins.war,同目录下启动:

java -jar jenkins.war

在管理员登陆(有cookie)的情况下

在没有登陆(未授权,cookie清空)的情况下,只有当管理员开启了allow anonymous read access的时候,才能实现任意文件读取,否则仍需登陆。

开启:

未开启:

而在linux下利用条件会更加苛刻,见后文。

三、漏洞分析

以payload为例,请求的url为/plugin/credentials/.ini。而在hudson/Plugin.java:227

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;">/**

  • This method serves static resources in the plugin under <tt>hudson/plugin/SHORTNAME</tt>.
    **/
    public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    String path = req.getRestOfPath();

    String pathUC = path.toUpperCase(Locale.ENGLISH);
    if (path.isEmpty() || path.contains("..") || path.startsWith(".") || path.contains("%") || pathUC.contains("META-INF") || pathUC.contains("WEB-INF")) {
    LOGGER.warning("rejecting possibly malicious " + req.getRequestURIWithQueryString());
    rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
    return;
    }

    // Stapler routes requests like the "/static/.../foo/bar/zot" to be treated like "/foo/bar/zot"
    // and this is used to serve long expiration header, by using Jenkins.VERSION_HASH as "..."
    // to create unique URLs. Recognize that and set a long expiration header.
    String requestPath = req.getRequestURI().substring(req.getContextPath().length());
    boolean staticLink = requestPath.startsWith("/static/");

    long expires = staticLink ? TimeUnit2.DAYS.toMillis(365) : -1;

    // use serveLocalizedFile to support automatic locale selection
    rsp.serveLocalizedFile(req, new URL(wrapper.baseResourceURL, '.' + path), expires);
    }
    </pre>

doDynamic函数用于处理类似/plugin/xx的请求,serveLocalizedFilestapler-1.250-sources.jar!/org/kohsuke/stapler/ResponseImpl.java第209行左右:

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;">public void serveLocalizedFile(StaplerRequest request, URL res, long expiration) throws ServletException, IOException {
if(!stapler.serveStaticResource(request, this, stapler.selectResourceByLocale(res,request.getLocale()), expiration))
sendError(SC_NOT_FOUND);
}
</pre>

先看最里面的request.getLocale(),然后再来分析stapler.selectResourceByLocale()

跟入request.getLocale(),至jetty-server-9.2.15.v20160210-sources.jar!/org/eclipse/jetty/server/Request.java:692:

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;">@Override
public Locale getLocale()
{
...

if (size > 0)
{
    String language = (String)acceptLanguage.get(0);
    language = HttpFields.valueParameters(language,null);
    String country = "";
    int dash = language.indexOf('-');
    if (dash > -1)
    {
        country = language.substring(dash + 1).trim();
        language = language.substring(0,dash).trim();
    }
    return new Locale(language,country);
}

return Locale.getDefault();

}
</pre>

这里用于处理HTTP请求中的Accept-Language头部。比如zh-cn,则会根据-的位置被分为两部分,languagezhcountrycn,然后返回Locale(language,country)对象。倘若不存在-,则country为空,language即对应我们的payload:../../../../../../../../../../../../windows/win,则此时返回一个Locale(language,"")

返回后即进入selectResourceByLocale(URL url, Locale locale),这里的locale参数即上一步返回的locale对象。

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;">OpenConnection selectResourceByLocale(URL url, Locale locale) throws IOException {
// hopefully HotSpot would be able to inline all the virtual calls in here
return urlLocaleSelector.open(url.toString(),locale,url);
}
</pre>

urlLocaleSelector对象的声明见stapler-1.250-sources.jar!/org/kohsuke/stapler/Stapler.java:390:

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;">private final LocaleDrivenResourceSelector urlLocaleSelector = new LocaleDrivenResourceSelector() {
@Override
URL map(String url) throws IOException {
return new URL(url);
}
};
</pre>

stapler-1.250-sources.jar!/org/kohsuke/stapler/Stapler.java:324实现了LocaleDrivenResourceSelector类的open方法:

<pre style="background: rgb(247, 247, 247); font: 12.75px/1.6 Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 16px; overflow: auto; max-height: 400px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; word-break: normal; overflow-wrap: normal; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box;">private abstract class LocaleDrivenResourceSelector {
/**

  • The 'path' is divided into the base part and the extension, and the locale-specific
  • suffix is inserted to the base portion. {@link #map(String)} is used to convert
  • the combined path into {@link URL}, until we find one that works.
  • <p>
  • The syntax of the locale specific resource is the same as property file localization.
  • So Japanese resource for <tt>foo.html</tt> would be named <tt>foo_ja.html</tt>.
  • @param path
  •  path/URL-like string that represents the path of the base resource,
    
  •  say "foo/bar/index.html" or "file:///a/b/c/d/efg.png"
    
  • @param locale
  •  The preferred locale
    
  • @param fallback
  •  The {@link URL} representation of the {@code path} parameter
    
  •  Used as a fallback.
    

*/
OpenConnection open(String path, Locale locale, URL fallback) throws IOException {
String s = path;
int idx = s.lastIndexOf('.');
if(idx<0) // no file extension, so no locale switch available
return openURL(fallback);
String base = s.substring(0,idx);
String ext = s.substring(idx);
if(ext.indexOf('/')>=0) // the '.' we found was not an extension separator
return openURL(fallback);

    OpenConnection con;

    // try locale specific resources first.
    con = openURL(map(base + '_' + locale.getLanguage() + '_' + locale.getCountry() + '_' + locale.getVariant() + ext));
    if(con!=null)   return con;
    con = openURL(map(base+'_'+ locale.getLanguage()+'_'+ locale.getCountry()+ext));
    if(con!=null)   return con;
    con = openURL(map(base+'_'+ locale.getLanguage()+ext));
    if(con!=null)   return con;
    // default
    return openURL(fallback);
}

/**
  • Maps the 'path' into {@link URL}.
    */
    abstract URL map(String path) throws IOException;
    }
    </pre>

先看看开头的注释,这段代码本意是想根据对应的语言(Accept-Language)来返回不同的文件,比如在ja的条件下请求foo.html,则相当于去请求foo_ja.html,这个过程会先把foo.html分成两部分:文件名foo和扩展名.html,然后根据具体的语言/国家来组合成最终的文件名。

结合payload来看,我们请求的url为/plugin/credentials/.ini,则base为空,扩展名(ext变量)即为.ini,然后通过一系列的尝试openURL,在此例中即最后一个情形con = openURL(map(base+'_'+ locale.getLanguage()+ext));,会去请求_../../../../../../../../../../../../windows/win.ini ,尽管目录_..并不存在,但在win下可以直接通过路径穿越来绕过。但在linux,则需要一个带有_的目录来想办法绕过。

四、 补丁分析

Jenkins官方修改了pom.xml,同时增加一个测试用例文件。真正的补丁在stapler这个web框架中,见commit记录: https://github.com/stapler/stapler/commit/8e9679b08c36a2f0cf2a81855d5e04e2ed2ac2b3

对从locale取出的language,country,variant均做了正则的校验,只允许字母数字以及特定格式的出现。在接下来的openUrl中,根据三种变量的不同检查情况来调用不同的请求,从而防止了路径穿越漏洞造成的任意文件读取漏洞。

五 、Reference

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351

推荐阅读更多精彩内容