-
-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathChallenge40.java
More file actions
69 lines (58 loc) · 2.51 KB
/
Challenge40.java
File metadata and controls
69 lines (58 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.owasp.wrongsecrets.challenges.docker;
import static org.owasp.wrongsecrets.Challenges.ErrorResponses.DECRYPTION_ERROR;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import lombok.extern.slf4j.Slf4j;
import org.owasp.wrongsecrets.challenges.FixedAnswerChallenge;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
/**
* This is a challenge based on leaking secrets due to keeping the encryption key and secret
* together.
*/
@Slf4j
@Component
public class Challenge40 extends FixedAnswerChallenge {
private final Resource resource;
public Challenge40(@Value("classpath:executables/secrchallenge.json") Resource resource) {
this.resource = resource;
}
@Override
public String getAnswer() {
return getSolution();
}
@SuppressFBWarnings(
value = {"CIPHER_INTEGRITY", "ECB_MODE"},
justification = "This is to allow for easy ECB online decryptors")
private String getSolution() {
try {
String jsonContent = resource.getContentAsString(Charset.defaultCharset());
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonContent);
String encryptedText = jsonNode.get("secret").asText();
byte[] decodedEncryptedText = Base64.getDecoder().decode(encryptedText.trim());
byte[] plainDecryptionKey = jsonNode.get("key").asText().getBytes(StandardCharsets.UTF_8);
// Create a SecretKey from the plaintext key
SecretKey secretKey = new SecretKeySpec(plainDecryptionKey, "AES");
// Initialize the Cipher for decryption
// codeql[java/weak-cryptographic-algorithm] Intentionally weak ECB mode for educational challenge about co-located key and secret
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// Decrypt the data
byte[] decryptedData = cipher.doFinal(decodedEncryptedText);
// Convert the decrypted bytes to a String
return new String(decryptedData, StandardCharsets.UTF_8);
} catch (Exception e) {
log.warn("there was an exception with decrypting content in challenge40", e);
return DECRYPTION_ERROR;
}
}
}