|
| 1 | +/** |
| 2 | + * TLS-Attacker - A Modular Penetration Testing Framework for TLS |
| 3 | + * |
| 4 | + * Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + */ |
| 9 | +package de.rub.nds.tlsattacker.attacks.impl; |
| 10 | + |
| 11 | +import de.rub.nds.modifiablevariable.bytearray.ByteArrayModificationFactory; |
| 12 | +import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray; |
| 13 | +import de.rub.nds.modifiablevariable.integer.IntegerModificationFactory; |
| 14 | +import de.rub.nds.modifiablevariable.integer.ModifiableInteger; |
| 15 | +import de.rub.nds.modifiablevariable.singlebyte.ModifiableByte; |
| 16 | +import de.rub.nds.tlsattacker.attacks.config.DrownCommandConfig; |
| 17 | +import de.rub.nds.tlsattacker.attacks.config.HeartbleedCommandConfig; |
| 18 | +import de.rub.nds.tlsattacker.attacks.constants.DrownVulnerabilityType; |
| 19 | +import static de.rub.nds.tlsattacker.attacks.impl.Attacker.LOGGER; |
| 20 | +import de.rub.nds.tlsattacker.core.config.Config; |
| 21 | +import de.rub.nds.tlsattacker.core.constants.HandshakeMessageType; |
| 22 | +import de.rub.nds.tlsattacker.core.constants.ProtocolMessageType; |
| 23 | +import de.rub.nds.tlsattacker.core.constants.RunningModeType; |
| 24 | +import de.rub.nds.tlsattacker.core.constants.SSL2CipherSuite; |
| 25 | +import de.rub.nds.tlsattacker.core.exceptions.WorkflowExecutionException; |
| 26 | +import de.rub.nds.tlsattacker.core.protocol.message.HeartbeatMessage; |
| 27 | +import de.rub.nds.tlsattacker.core.protocol.message.SSL2ClientMasterKeyMessage; |
| 28 | +import de.rub.nds.tlsattacker.core.protocol.message.SSL2ServerHelloMessage; |
| 29 | +import de.rub.nds.tlsattacker.core.protocol.message.SSL2ServerVerifyMessage; |
| 30 | +import de.rub.nds.tlsattacker.core.protocol.preparator.SSL2ClientMasterKeyPreparator; |
| 31 | +import de.rub.nds.tlsattacker.core.state.State; |
| 32 | +import de.rub.nds.tlsattacker.core.state.TlsContext; |
| 33 | +import de.rub.nds.tlsattacker.core.util.LogLevel; |
| 34 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowExecutor; |
| 35 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowExecutorFactory; |
| 36 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowTrace; |
| 37 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowTraceUtil; |
| 38 | +import de.rub.nds.tlsattacker.core.workflow.action.GenericReceiveAction; |
| 39 | +import de.rub.nds.tlsattacker.core.workflow.action.ReceiveAction; |
| 40 | +import de.rub.nds.tlsattacker.core.workflow.action.SendAction; |
| 41 | +import de.rub.nds.tlsattacker.core.workflow.factory.WorkflowConfigurationFactory; |
| 42 | +import de.rub.nds.tlsattacker.core.workflow.factory.WorkflowTraceType; |
| 43 | +import java.util.Arrays; |
| 44 | +import java.util.List; |
| 45 | + |
| 46 | +import org.bouncycastle.crypto.digests.MD5Digest; |
| 47 | +import org.bouncycastle.crypto.engines.RC4Engine; |
| 48 | +import org.bouncycastle.crypto.params.KeyParameter; |
| 49 | + |
| 50 | +public class DrownAttacker extends Attacker<DrownCommandConfig> { |
| 51 | + |
| 52 | + public DrownAttacker(DrownCommandConfig config) { |
| 53 | + super(config); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void executeAttack() { |
| 58 | + throw new UnsupportedOperationException("Not implemented yet"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Boolean isVulnerable() { |
| 63 | + DrownVulnerabilityType type = getDrownVulnerabilityType(); |
| 64 | + switch (type) { |
| 65 | + case FULL: |
| 66 | + LOGGER.log(LogLevel.CONSOLE_OUTPUT, "Server is vulnerable to the full DROWN attack"); |
| 67 | + return true; |
| 68 | + case NONE: |
| 69 | + return false; |
| 70 | + case SSL2: |
| 71 | + LOGGER.log(LogLevel.CONSOLE_OUTPUT, "Server supports SSL2, but not any weak ciphersuites, " |
| 72 | + + "so is not vulnerable to DROWN"); |
| 73 | + return false; |
| 74 | + case UNKNOWN: |
| 75 | + LOGGER.warn("Could not execute Workflow - something went wrong. Check the Debug output to be certain"); |
| 76 | + return null; |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + public DrownVulnerabilityType getDrownVulnerabilityType() { |
| 82 | + Config tlsConfig = config.createConfig(); |
| 83 | + WorkflowTrace trace = new WorkflowConfigurationFactory(tlsConfig).createWorkflowTrace( |
| 84 | + WorkflowTraceType.SSL2_HELLO, RunningModeType.CLIENT); |
| 85 | + trace.addTlsAction(new SendAction(new SSL2ClientMasterKeyMessage())); |
| 86 | + trace.addTlsAction(new ReceiveAction(new SSL2ServerVerifyMessage())); |
| 87 | + State state = new State(tlsConfig, trace); |
| 88 | + try { |
| 89 | + WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.createWorkflowExecutor( |
| 90 | + tlsConfig.getWorkflowExecutorType(), state); |
| 91 | + workflowExecutor.executeWorkflow(); |
| 92 | + } catch (WorkflowExecutionException ex) { |
| 93 | + LOGGER.info("The TLS protocol flow was not executed completely, follow the debug messages for more information."); |
| 94 | + LOGGER.debug(ex); |
| 95 | + return DrownVulnerabilityType.UNKNOWN; |
| 96 | + } |
| 97 | + |
| 98 | + if (!WorkflowTraceUtil.didReceiveMessage(HandshakeMessageType.SSL2_SERVER_HELLO, trace)) { |
| 99 | + return DrownVulnerabilityType.NONE; |
| 100 | + } |
| 101 | + |
| 102 | + SSL2ServerHelloMessage serverHello = (SSL2ServerHelloMessage) WorkflowTraceUtil.getFirstReceivedMessage( |
| 103 | + HandshakeMessageType.SSL2_SERVER_HELLO, trace); |
| 104 | + List<SSL2CipherSuite> serverCipherSuites = SSL2CipherSuite.getCiphersuites(serverHello.getCipherSuites() |
| 105 | + .getValue()); |
| 106 | + for (SSL2CipherSuite cipherSuite : serverCipherSuites) { |
| 107 | + if (cipherSuite.isWeak()) { |
| 108 | + LOGGER.debug("Declaring host as vulnerable based on weak ciphersuite in ServerHello."); |
| 109 | + return DrownVulnerabilityType.FULL; |
| 110 | + } |
| 111 | + } |
| 112 | + SSL2ServerVerifyMessage message = (SSL2ServerVerifyMessage) WorkflowTraceUtil.getFirstReceivedMessage( |
| 113 | + HandshakeMessageType.SSL2_SERVER_VERIFY, trace); |
| 114 | + if (message != null && checkServerVerifyMessage(message, state.getTlsContext())) { |
| 115 | + LOGGER.debug("Declaring host as vulnerable based on ServerVerify."); |
| 116 | + return DrownVulnerabilityType.FULL; |
| 117 | + } |
| 118 | + return DrownVulnerabilityType.SSL2; |
| 119 | + } |
| 120 | + |
| 121 | + private boolean checkServerVerifyMessage(SSL2ServerVerifyMessage message, TlsContext context) { |
| 122 | + byte[] md5Output = getMD5Output(context); |
| 123 | + |
| 124 | + RC4Engine rc4 = new RC4Engine(); |
| 125 | + rc4.init(false, new KeyParameter(md5Output)); |
| 126 | + byte[] encrypted = message.getEncryptedPart().getValue(); |
| 127 | + int len = encrypted.length; |
| 128 | + if (len < 16) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + byte[] decrypted = new byte[len]; |
| 133 | + rc4.processBytes(encrypted, 0, len, decrypted, 0); |
| 134 | + |
| 135 | + if (Arrays.equals(Arrays.copyOfRange(decrypted, len - 16, len), context.getClientRandom())) { |
| 136 | + return true; |
| 137 | + } else { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private byte[] getMD5Output(TlsContext tlsContext) { |
| 143 | + MD5Digest md5 = new MD5Digest(); |
| 144 | + byte[] clearKey = new byte[SSL2ClientMasterKeyPreparator.EXPORT_RC4_NUM_OF_CLEAR_KEY_BYTES]; |
| 145 | + md5Update(md5, clearKey); |
| 146 | + md5Update(md5, tlsContext.getPreMasterSecret()); |
| 147 | + md5.update((byte) '0'); |
| 148 | + md5Update(md5, tlsContext.getClientRandom()); |
| 149 | + md5Update(md5, tlsContext.getServerRandom()); |
| 150 | + byte[] md5Output = new byte[md5.getDigestSize()]; |
| 151 | + md5.doFinal(md5Output, 0); |
| 152 | + return md5Output; |
| 153 | + } |
| 154 | + |
| 155 | + private static void md5Update(MD5Digest md5, byte[] bytes) { |
| 156 | + md5.update(bytes, 0, bytes.length); |
| 157 | + } |
| 158 | +} |
0 commit comments