-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/owasp/webgoat/lessons/cryptography/mytest/HashBruteforce.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/org/owasp/webgoat/lessons/cryptography/mytest/XOREncode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/org/owasp/webgoat/lessons/spoofcookie/mytest/GenerationCookie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |