Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
zha0cai committed Jun 18, 2024
1 parent 5f04a19 commit cdfb77b
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

package org.owasp.webgoat.lessons.cryptography;

import com.thoughtworks.xstream.core.util.Base64Encoder;
import jakarta.servlet.http.HttpServletRequest;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import javax.xml.bind.DatatypeConverter;
import lombok.extern.slf4j.Slf4j;
import org.owasp.webgoat.container.assignments.AssignmentEndpoint;
Expand Down Expand Up @@ -73,6 +75,7 @@ public AttackResult completed(
modulus; /* used to validate the modulus of the public key but might need to be corrected */
KeyPair keyPair = (KeyPair) request.getSession().getAttribute("keyPair");
RSAPublicKey rsaPubKey = (RSAPublicKey) keyPair.getPublic();

if (tempModulus.length() == 512) {
tempModulus = "00".concat(tempModulus);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.owasp.webgoat.lessons.cryptography.mytest;

import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashBruteforce {
public static final String[] SECRETS = {"secret", "admin", "password", "123456", "passw0rd"};

public static void main(String[] args) {
String md5HashStr = "21232F297A57A5A743894A0E4A801FC3";
String sha256HashStr = "34de66e5caf2cb69ff2bebdc1f3091ecf6296852446c718e38ebfa60e4aa75d2";

String md5SecretStr = crackHash(md5HashStr, "MD5");
System.out.println(md5SecretStr);

String sha256SecretStr = crackHash(sha256HashStr, "SHA-256");
System.out.println(sha256SecretStr);
}

private static String crackHash(String targetMd5Hash, String algorithm) {
try {
// 选择算法
MessageDigest md = switch (algorithm) {
case "MD5" -> MessageDigest.getInstance("MD5");
case "SHA-256" -> MessageDigest.getInstance("SHA-256");
default -> throw new NoSuchAlgorithmException("不支持的哈希算法: " + algorithm);
};

// 爆破 hash
for (String secret: SECRETS) {
md.update(secret.getBytes());
byte[] digest = md.digest();

String md5Hash = DatatypeConverter.printHexBinary(digest).toUpperCase();
if (md5Hash.equals(targetMd5Hash)) {
return secret;
}
}

} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.owasp.webgoat.lessons.cryptography.mytest;

import java.util.Base64;

public class XOREncode {
public static void main(String[] args) {
// Base64 编码的字符串
// {xor}Oz4rPj0+LDovPiwsKDAtOw==
String base64EncodedStr = "Oz4rPj0+LDovPiwsKDAtOw==";
// Base64 解码
byte[] base64DecodedBytes = Base64.getDecoder().decode(base64EncodedStr);

// 进行 xor 猜解
for (int key=0; key<=0xFF; key++) {
byte[] xorDecodedBytes = xorDecode(base64DecodedBytes, (byte) key);
String decodedStr = new String(xorDecodedBytes);

System.out.printf("Key: 0x%02X -> Decoded String: %s%n", key, decodedStr);
// if (decodedStr.equals("databasepassword")) {
// System.out.printf("Key: 0x%02X -> Decoded String: %s%n", key, decodedStr);
// }
}
}

private static byte[] xorDecode(byte[] dataBytes, byte key) {
byte[] xorDecodedBytes = new byte[dataBytes.length];
for (int i=0; i<dataBytes.length; i++) {
xorDecodedBytes[i] = (byte)(dataBytes[i] ^ key);
}

return xorDecodedBytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.owasp.webgoat.lessons.spoofcookie.mytest;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.crypto.codec.Hex;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class GenerationCookie {

// 生成一个包含指定长度的随机字母字符串
private static final String SALT = RandomStringUtils.randomAlphabetic(10);

public static String encode(final String value) {
if (value == null) {
return null;
}

// cookie 处理流程
String encoded = value.toLowerCase() + SALT;
encoded = revert(encoded);
encoded = hexEncode(encoded);
return base64Encode(encoded);
}

public static String decode(final String encodedValue) {
if (encodedValue == null) {
return null;
}

String decoded = base64Decode(encodedValue);
decoded = hexDecode(decoded);
decoded = revert(decoded);

return decoded = decoded.substring(0, decoded.length()-SALT.length());
}

private static String revert(final String value) {
return new StringBuilder(value).reverse().toString();
}

private static String hexEncode(final String value) {
char[] encoded = Hex.encode(value.getBytes(StandardCharsets.UTF_8));
return new String(encoded);
}

private static String hexDecode(final String value) {
byte[] decoded = Hex.decode(value);
return new String(decoded);
}

private static String base64Encode(final String value) {
return Base64.getEncoder().encodeToString(value.getBytes());
}

private static String base64Decode(final String value) {
byte[] decoded = Base64.getDecoder().decode(value.getBytes());
return new String(decoded);
}

public static void main(String[] args) {
String username = "Tom";
System.out.println(encode(username));

String lowerCasedUsername = decode("NDQ2NTZmNWE2ODZmNGE1MjRmNDI2ZDZmNzQ=");
System.out.println(lowerCasedUsername);
}

}

0 comments on commit cdfb77b

Please sign in to comment.