Java 反序列化 CC7链

发布于 2025-10-03 · 技术


cc7主要是入口点不一样,入口点用的是Hashtable 看看Hashtable.readObject

private void readObject(java.io.ObjectInputStream s)  
     throws IOException, ClassNotFoundException  
{  
    ObjectInputStream.GetField fields = s.readFields();  
  
    // Read and validate loadFactor (ignore threshold - it will be re-computed)  
    float lf = fields.get("loadFactor", 0.75f);  
    if (lf <= 0 || Float.isNaN(lf))  
        throw new StreamCorruptedException("Illegal load factor: " + lf);  
    lf = Math.min(Math.max(0.25f, lf), 4.0f);  
  
    // Read the original length of the array and number of elements  
    int origlength = s.readInt();  
    int elements = s.readInt();  
  
    // Validate # of elements  
    if (elements < 0)  
        throw new StreamCorruptedException("Illegal # of Elements: " + elements);  
  
    // Clamp original length to be more than elements / loadFactor  
    // (this is the invariant enforced with auto-growth)    origlength = Math.max(origlength, (int)(elements / lf) + 1);  
  
    // Compute new length with a bit of room 5% + 3 to grow but  
    // no larger than the clamped original length.  Make the length    // odd if it's large enough, this helps distribute the entries.    // Guard against the length ending up zero, that's not valid.    int length = (int)((elements + elements / 20) / lf) + 3;  
    if (length > elements && (length & 1) == 0)  
        length--;  
    length = Math.min(length, origlength);  
  
    if (length < 0) { // overflow  
        length = origlength;  
    }  
  
    // Check Map.Entry[].class since it's the nearest public type to  
    // what we're actually creating.    SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, length);  
    Hashtable.UnsafeHolder.putLoadFactor(this, lf);  
    table = new Entry<?,?>[length];  
    threshold = (int)Math.min(length * lf, MAX_ARRAY_SIZE + 1);  
    count = 0;  
  
    // Read the number of elements and then all the key/value objects  
    for (; elements > 0; elements--) {  
        @SuppressWarnings("unchecked")  
            K key = (K)s.readObject();  
        @SuppressWarnings("unchecked")  
            V value = (V)s.readObject();  
        // sync is eliminated for performance  
        reconstitutionPut(table, key, value);  // 关键在这里
    }  
}

最后这里调了reconstitutionPut

private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)  
    throws StreamCorruptedException  
{  
    if (value == null) {  
        throw new java.io.StreamCorruptedException();  
    }  
    // Makes sure the key is not already in the hashtable.  
    // This should not happen in deserialized version.    
    int hash = key.hashCode();  
    int index = (hash & 0x7FFFFFFF) % tab.length;  
    for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {  
        if ((e.hash == hash) && e.key.equals(key)) {  // 关键在这里
            throw new java.io.StreamCorruptedException();  
        }  
    }  
    // Creates the new entry.  
    @SuppressWarnings("unchecked")  
        Entry<K,V> e = (Entry<K,V>)tab[index];  
    tab[index] = new Entry<>(hash, key, value, e);  
    count++;  
}

调了e.key.equals

public boolean equals(Object obj) {  
    return (this == obj);  
}

然后找谁的equals有问题,这里是找到了org.apache.commons.collections.map.AbstractMapDecorator.equals

public boolean equals(Object object) {  
    if (object == this) {  
        return true;  
    }  
    return map.equals(object);
}

其实不是这个有问题,而是它会调用到 java.util.AbstractMap.equals

public boolean equals(Object o) {  
    if (o == this)  
        return true;  
  
    if (!(o instanceof Map))  
        return false;  
    Map<?,?> m = (Map<?,?>) o;  
    if (m.size() != size())  
        return false;  
  
    try {  
        Iterator<Entry<K,V>> i = entrySet().iterator();  
        while (i.hasNext()) {  
            Entry<K,V> e = i.next();  
            K key = e.getKey();  
            V value = e.getValue();  
            if (value == null) {  
                if (!(m.get(key)==null && m.containsKey(key)))  
                    return false;  
            } else {  
                if (!value.equals(m.get(key)))  // 关键在这里
                    return false;  
            }  
        }  
    } catch (ClassCastException unused) {  
        return false;  
    } catch (NullPointerException unused) {  
        return false;  
    }  
  
    return true;  
}

传一个LazyMap进去,LazyMap没有equals,所以会调LazyMap的父类org.apache.commons.collections.map.AbstractMapDecorator,这也没有equals就会调到java.util.AbstractMap.equals,这里面就是会调到get

« 返回首页