
CVE-2022-32532を再現するための最小限のJava Webアプリケーション。Apache ShiroのRegExPatternMatcherにおける、URL内の改行文字による認証バイパス。
これは CVE-2022-32532(Apache Shiro RegExPatternMatcher 認証バイパス)を再現するための最小限のWebアプリケーションです。
RegExPatternMatcher が正規表現を正しくアンカーしておらず、パスバイパスが発生する可能性があります。具体的には、Javaのデフォルトの正規表現マッチングロジックを使用しており、正規表現で.記号が使用された場合、\r(%0d)や\n(%0a)のような特殊記号が無視されます。\r、\n記号を正しく処理するには、PATTERN.DOTALLモードに基づく正規表現マッチングルールを明示的に使用する必要があります。shiro-1.9.1未満のバージョンはすべてデフォルトの正規表現マッチングロジックを使用しているため、\r、\nを正しく処理できず、認証バイパスが発生します。アプリケーションを起動します。
启动ShiroCve202232532Application
通常のShiro認証で access denied を返すURLは以下のとおりです。
http://localhost:8080/permit/xxx、最後のxxxは任意の文字に置き換えることができます。
Shiroの認証をバイパスして success を返すURLは以下のとおりです。
http://localhost:8080/permit/xxx、つまり最後のxxxに改行文字\n(%0a)、復帰文字\r(%0d)を挿入します。
解決方法
1)https://github.com/apache/shiro/blob/shiro-root-1.9.1/core/src/main/java/org/apache/shiro/util/ から RegExPatternMatcher.java と PatternMatcher.java の全内容をコピーします。
2)jdk11 を使用して、これら2つのJavaファイルを RegExPatternMatcher.class と PatternMatcher.class にコンパイルします。
3)WinRAR を使用して、この2つの class ファイルを shiro-core-1.6.0.jar の org/apache/shiro/util/ の下に配置します。
4)修正テストは、shiro-core-1.9.1 の RegExPatternMatcher.java のコードを本ケースにコピーして RegExPatternMatcher191.java に変更し、その後 MyFilter の15行目と MyShiroFilterFactoryBean の29行目の new RegExPatternMatcher() を new RegExPatternMatcher191() に変更するだけです。
5)shiro-core-1.9.1 の RegExPatternMatcher の実装ロジックは以下のとおりです。
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.util;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* {@code PatternMatcher} implementation that uses standard {@link java.util.regex} objects.
*
* @see Pattern
* @since 1.0
*/
public class RegExPatternMatcher implements PatternMatcher {
private static final int DEFAULT = Pattern.DOTALL;
private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;
private boolean caseInsensitive = false;
/**
* Simple implementation that merely uses the default pattern comparison logic provided by the
* JDK.
* <p/>This implementation essentially executes the following:
* <pre>
* Pattern p = Pattern.compile(pattern, Pattern.DOTALL);
* Matcher m = p.matcher(source);
* return m.matches();</pre>
* @param pattern the pattern to match against
* @param source the source to match
* @return {@code true} if the source matches the required pattern, {@code false} otherwise.
*/
public boolean matches(String pattern, String source) {
if (pattern == null) {
throw new IllegalArgumentException("pattern argument cannot be null.");
}
Pattern p = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
Matcher m = p.matcher(source);
return m.matches();
}
/**
* Returns true if regex match should be case-insensitive.
* @return true if regex match should be case-insensitive.
*/
public boolean isCaseInsensitive() {
return caseInsensitive;
}
/**
* Adds the Pattern.CASE_INSENSITIVE flag when compiling patterns.
* @param caseInsensitive true if patterns should match case-insensitive.
*/
public void setCaseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
}
}