|
| 1 | +/** |
| 2 | + * TLS-Server-Scanner - A TLS configuration and analysis tool based on TLS-Attacker |
| 3 | + * |
| 4 | + * Copyright 2017-2022 Ruhr University Bochum, Paderborn University, Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License, Version 2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 8 | + */ |
| 9 | + |
| 10 | +package de.rub.nds.tlsscanner.serverscanner.probe; |
| 11 | + |
| 12 | +import de.rub.nds.scanner.core.constants.TestResults; |
| 13 | +import de.rub.nds.tlsattacker.core.config.Config; |
| 14 | +import de.rub.nds.tlsattacker.core.constants.CipherSuite; |
| 15 | +import de.rub.nds.tlsattacker.core.constants.NamedGroup; |
| 16 | +import de.rub.nds.tlsattacker.core.constants.ProtocolVersion; |
| 17 | +import de.rub.nds.tlsattacker.core.constants.RunningModeType; |
| 18 | +import de.rub.nds.tlsattacker.core.https.HttpsRequestMessage; |
| 19 | +import de.rub.nds.tlsattacker.core.protocol.message.ApplicationMessage; |
| 20 | +import de.rub.nds.tlsattacker.core.state.State; |
| 21 | +import de.rub.nds.tlsattacker.core.workflow.ParallelExecutor; |
| 22 | +import de.rub.nds.tlsattacker.core.workflow.WorkflowTrace; |
| 23 | +import de.rub.nds.tlsattacker.core.workflow.action.SendAction; |
| 24 | +import de.rub.nds.tlsattacker.core.workflow.factory.WorkflowConfigurationFactory; |
| 25 | +import de.rub.nds.tlsattacker.core.workflow.factory.WorkflowTraceType; |
| 26 | +import de.rub.nds.tlsattacker.transport.socket.SocketState; |
| 27 | +import de.rub.nds.tlsattacker.transport.tcp.TcpTransportHandler; |
| 28 | +import de.rub.nds.tlsscanner.core.constants.TlsAnalyzedProperty; |
| 29 | +import de.rub.nds.tlsscanner.core.constants.TlsProbeType; |
| 30 | +import de.rub.nds.tlsscanner.core.probe.TlsProbe; |
| 31 | +import de.rub.nds.tlsscanner.serverscanner.config.ServerScannerConfig; |
| 32 | +import de.rub.nds.tlsscanner.serverscanner.probe.result.ConnectionClosingResult; |
| 33 | +import de.rub.nds.tlsscanner.serverscanner.report.ServerReport; |
| 34 | +import de.rub.nds.tlsscanner.serverscanner.selector.ConfigSelector; |
| 35 | +import java.io.IOException; |
| 36 | + |
| 37 | +/** |
| 38 | + * Determines when the server closes the connection. It's meant for tests in the lab so we limit the probe. Note that |
| 39 | + * NO_RESULT may indicate that we couldn't identify a closing delta, i.e the server didn't close the connection within |
| 40 | + * our limit or the probe could not be executed. |
| 41 | + */ |
| 42 | +public class ConnectionClosingProbe extends TlsServerProbe<ConfigSelector, ServerReport, ConnectionClosingResult> { |
| 43 | + |
| 44 | + public static final long NO_RESULT = -1; |
| 45 | + private static final long LIMIT = 5000; |
| 46 | + |
| 47 | + private boolean useHttpAppData = false; |
| 48 | + |
| 49 | + public ConnectionClosingProbe(ConfigSelector configSelector, ParallelExecutor parallelExecutor) { |
| 50 | + super(parallelExecutor, TlsProbeType.CONNECTION_CLOSING_DELTA, configSelector); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public ConnectionClosingResult executeTest() { |
| 55 | + Config tlsConfig = configSelector.getBaseConfig(); |
| 56 | + configSelector.repairConfig(tlsConfig); |
| 57 | + tlsConfig.setWorkflowTraceType(WorkflowTraceType.HTTPS); |
| 58 | + tlsConfig.setWorkflowExecutorShouldClose(false); |
| 59 | + |
| 60 | + WorkflowTrace handshakeOnly = getWorkflowTrace(tlsConfig); |
| 61 | + WorkflowTrace handshakeWithAppData = getWorkflowTrace(tlsConfig); |
| 62 | + if (useHttpAppData) { |
| 63 | + handshakeWithAppData.addTlsAction(new SendAction(new HttpsRequestMessage(tlsConfig))); |
| 64 | + } else { |
| 65 | + handshakeWithAppData.addTlsAction(new SendAction(new ApplicationMessage(tlsConfig))); |
| 66 | + } |
| 67 | + |
| 68 | + return new ConnectionClosingResult(evaluateClosingDelta(tlsConfig, handshakeOnly), |
| 69 | + evaluateClosingDelta(tlsConfig, handshakeWithAppData)); |
| 70 | + } |
| 71 | + |
| 72 | + public WorkflowTrace getWorkflowTrace(Config tlsConfig) { |
| 73 | + WorkflowConfigurationFactory factory = new WorkflowConfigurationFactory(tlsConfig); |
| 74 | + return factory.createWorkflowTrace(WorkflowTraceType.HANDSHAKE, RunningModeType.CLIENT); |
| 75 | + } |
| 76 | + |
| 77 | + private long evaluateClosingDelta(Config tlsConfig, WorkflowTrace workflowTrace) { |
| 78 | + State state = new State(tlsConfig, workflowTrace); |
| 79 | + executeState(state); |
| 80 | + long delta = 0; |
| 81 | + SocketState socketState = null; |
| 82 | + do { |
| 83 | + try { |
| 84 | + socketState = (((TcpTransportHandler) (state.getTlsContext().getTransportHandler())).getSocketState()); |
| 85 | + switch (socketState) { |
| 86 | + case CLOSED: |
| 87 | + case IO_EXCEPTION: |
| 88 | + case PEER_WRITE_CLOSED: |
| 89 | + case SOCKET_EXCEPTION: |
| 90 | + case TIMEOUT: |
| 91 | + closeSocket(state); |
| 92 | + return delta; |
| 93 | + default: |
| 94 | + } |
| 95 | + Thread.sleep(10); |
| 96 | + delta += 10; |
| 97 | + } catch (InterruptedException ignored) { |
| 98 | + } |
| 99 | + } while (delta < LIMIT); |
| 100 | + closeSocket(state); |
| 101 | + return NO_RESULT; |
| 102 | + } |
| 103 | + |
| 104 | + public void closeSocket(State state) { |
| 105 | + try { |
| 106 | + state.getTlsContext().getTransportHandler().closeConnection(); |
| 107 | + } catch (IOException ignored) { |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean canBeExecuted(ServerReport report) { |
| 113 | + return report.isProbeAlreadyExecuted(TlsProbeType.HTTP_HEADER); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public ConnectionClosingResult getCouldNotExecuteResult() { |
| 118 | + return new ConnectionClosingResult(NO_RESULT, NO_RESULT); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void adjustConfig(ServerReport report) { |
| 123 | + useHttpAppData = report.getResult(TlsAnalyzedProperty.SUPPORTS_HTTPS) == TestResults.TRUE; |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments