# # -*- coding: utf-8 -*- # Wireshark tests # By Gerald Combs # # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping # # SPDX-License-Identifier: GPL-2.0-or-later # '''Decryption tests''' import os.path import subprocesstest import unittest import fixtures @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_80211(subprocesstest.SubprocessTestCase): def test_80211_wpa_psk(self, cmd_tshark, capture_file): '''IEEE 802.11 WPA PSK''' # https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=view&target=wpa-Induction.pcap self.runProcess((cmd_tshark, '-o', 'wlan.enable_decryption: TRUE', '-Tfields', '-e', 'http.request.uri', '-r', capture_file('wpa-Induction.pcap.gz'), '-Y', 'http', )) self.assertTrue(self.grepOutput('favicon.ico')) def test_80211_wpa_eap(self, cmd_tshark, capture_file): '''IEEE 802.11 WPA EAP (EAPOL Rekey)''' # Included in git sources test/captures/wpa-eap-tls.pcap.gz self.runProcess((cmd_tshark, '-o', 'wlan.enable_decryption: TRUE', '-r', capture_file('wpa-eap-tls.pcap.gz'), '-Y', 'wlan.analysis.tk==7d9987daf5876249b6c773bf454a0da7', )) self.assertTrue(self.grepOutput('Group Message')) def test_80211_wpa_eapol_incomplete_rekeys(self, cmd_tshark, capture_file): '''WPA decode with message1+2 only and secure bit set on message 2''' # Included in git sources test/captures/wpa-test-decode.pcap.gz self.runProcess((cmd_tshark, '-o', 'wlan.enable_decryption: TRUE', '-r', capture_file('wpa-test-decode.pcap.gz'), '-Y', 'icmp.resp_to == 4263', )) self.assertTrue(self.grepOutput('Echo')) def test_80211_wpa_psk_mfp(self, cmd_tshark, capture_file): '''WPA decode management frames with MFP enabled (802.11w)''' # Included in git sources test/captures/wpa-test-decode-mgmt.pcap.gz self.runProcess((cmd_tshark, '-o', 'wlan.enable_decryption: TRUE', '-r', capture_file('wpa-test-decode-mgmt.pcap.gz'), '-Y', 'wlan.fixed.reason_code == 2 || wlan.fixed.category_code == 3', )) self.assertEqual(self.countOutput('802.11.*SN=.*FN=.*Flags='), 3) def test_80211_wpa_tdls(self, cmd_tshark, capture_file, features): '''WPA decode traffic in a TDLS (Tunneled Direct-Link Setup) session (802.11z)''' if not features.have_libgcrypt16: self.skipTest('Requires GCrypt 1.6 or later.') # Included in git sources test/captures/wpa-test-decode-tdls.pcap.gz self.runProcess((cmd_tshark, #'-ouat:80211_keys:"wpa-pwd","12345678"', '-o', 'wlan.enable_decryption: TRUE', '-r', capture_file('wpa-test-decode-tdls.pcap.gz'), '-Y', 'icmp', )) self.assertEqual(self.countOutput('ICMP.*Echo .ping'), 2) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_dtls(subprocesstest.SubprocessTestCase): def test_dtls(self, cmd_tshark, capture_file): '''DTLS''' # https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=view&target=snakeoil.tgz self.runProcess((cmd_tshark, '-r', capture_file('snakeoil-dtls.pcap'), '-Tfields', '-e', 'data.data', '-Y', 'data', )) self.assertTrue(self.grepOutput('697420776f726b20210a')) def test_dtls_psk_aes128ccm8(self, cmd_tshark, capture_file): '''DTLS 1.2 with PSK, AES-128-CCM-8''' self.runProcess((cmd_tshark, '-r', capture_file('dtls12-aes128ccm8.pcap'), '-o', 'dtls.psk:ca19e028a8a372ad2d325f950fcaceed', '-x' )) dt_count = self.countOutput('Decrypted DTLS') wfm_count = self.countOutput('Works for me!.') self.assertTrue(dt_count == 7 and wfm_count == 2) def test_dtls_udt(self, cmd_tshark, dirs, capture_file): '''UDT over DTLS 1.2 with RSA key''' key_file = os.path.join(dirs.key_dir, 'udt-dtls.key') self.runProcess((cmd_tshark, '-r', capture_file('udt-dtls.pcapng.gz'), '-o', 'dtls.keys_list:0.0.0.0,0,data,{}'.format(key_file), '-Y', 'dtls && udt.type==ack', )) self.assertTrue(self.grepOutput('UDT')) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_tls(subprocesstest.SubprocessTestCase): def test_tls(self, cmd_tshark, capture_file): '''TLS using the server's private key''' # https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=view&target=snakeoil2_070531.tgz self.runProcess((cmd_tshark, '-r', capture_file('rsasnakeoil2.pcap'), '-Tfields', '-e', 'http.request.uri', '-Y', 'http', )) self.assertTrue(self.grepOutput('favicon.ico')) def test_tls_rsa_pq(self, cmd_tshark, dirs, capture_file): '''TLS using the server's private key with p < q (test whether libgcrypt is correctly called)''' key_file = os.path.join(dirs.key_dir, 'rsa-p-lt-q.key') self.runProcess((cmd_tshark, '-r', capture_file('rsa-p-lt-q.pcap'), '-o', 'tls.keys_list:0.0.0.0,443,http,{}'.format(key_file), '-Tfields', '-e', 'http.request.uri', '-Y', 'http', )) self.assertTrue(self.grepOutput('/')) def test_tls_with_password(self, cmd_tshark, capture_file): '''TLS using the server's private key with password''' self.runProcess((cmd_tshark, '-r', capture_file('dmgr.pcapng'), '-Tfields', '-e', 'http.request.uri', '-Y', 'http', )) self.assertTrue(self.grepOutput('unsecureLogon.jsp')) def test_tls_master_secret(self, cmd_tshark, dirs, capture_file): '''TLS using the master secret and ssl.keylog_file preference aliasing''' key_file = os.path.join(dirs.key_dir, 'dhe1_keylog.dat') self.runProcess((cmd_tshark, '-r', capture_file('dhe1.pcapng.gz'), '-o', 'ssl.keylog_file: {}'.format(key_file), '-o', 'tls.desegment_ssl_application_data: FALSE', '-o', 'http.tls.port: 443', '-Tfields', '-e', 'http.request.method', '-e', 'http.request.uri', '-e', 'http.request.version', '-Y', 'http', )) self.assertTrue(self.grepOutput(r'GET\s+/test\s+HTTP/1.0')) def test_tls12_renegotiation(self, cmd_tshark, dirs, capture_file): '''TLS 1.2 with renegotiation''' key_file = os.path.join(dirs.key_dir, 'rsasnakeoil2.key') self.runProcess((cmd_tshark, '-r', capture_file('tls-renegotiation.pcap'), '-o', 'tls.keys_list:0.0.0.0,4433,http,{}'.format(key_file), '-Tfields', '-e', 'http.content_length', '-Y', 'http', )) count_0 = self.countOutput('^0$') count_2151 = self.countOutput('^2151$') self.assertTrue(count_0 == 1 and count_2151 == 1) def test_tls12_psk_aes128ccm(self, cmd_tshark, capture_file): '''TLS 1.2 with PSK, AES-128-CCM''' self.runProcess((cmd_tshark, '-r', capture_file('tls12-aes128ccm.pcap'), '-o', 'tls.psk:ca19e028a8a372ad2d325f950fcaceed', '-q', '-z', 'follow,tls,ascii,0', )) self.assertTrue(self.grepOutput('http://www.gnu.org/software/gnutls')) def test_tls12_psk_aes256gcm(self, cmd_tshark, capture_file): '''TLS 1.2 with PSK, AES-256-GCM''' self.runProcess((cmd_tshark, '-r', capture_file('tls12-aes256gcm.pcap'), '-o', 'tls.psk:ca19e028a8a372ad2d325f950fcaceed', '-q', '-z', 'follow,tls,ascii,0', )) self.assertTrue(self.grepOutput('http://www.gnu.org/software/gnutls')) def test_tls12_chacha20poly1305(self, cmd_tshark, dirs, features, capture_file): '''TLS 1.2 with ChaCha20-Poly1305''' if not features.have_libgcrypt17: self.skipTest('Requires GCrypt 1.7 or later.') key_file = os.path.join(dirs.key_dir, 'tls12-chacha20poly1305.keys') ciphers=[ 'ECDHE-ECDSA-CHACHA20-POLY1305', 'ECDHE-RSA-CHACHA20-POLY1305', 'DHE-RSA-CHACHA20-POLY1305', 'RSA-PSK-CHACHA20-POLY1305', 'DHE-PSK-CHACHA20-POLY1305', 'ECDHE-PSK-CHACHA20-POLY1305', 'PSK-CHACHA20-POLY1305', ] stream = 0 for cipher in ciphers: self.runProcess((cmd_tshark, '-r', capture_file('tls12-chacha20poly1305.pcap'), '-o', 'tls.keylog_file: {}'.format(key_file), '-q', '-z', 'follow,tls,ascii,{}'.format(stream), )) stream += 1 self.assertTrue(self.grepOutput('Cipher is {}'.format(cipher))) def test_tls13_chacha20poly1305(self, cmd_tshark, dirs, features, capture_file): '''TLS 1.3 with ChaCha20-Poly1305''' if not features.have_libgcrypt17: self.skipTest('Requires GCrypt 1.7 or later.') key_file = os.path.join(dirs.key_dir, 'tls13-20-chacha20poly1305.keys') self.runProcess((cmd_tshark, '-r', capture_file('tls13-20-chacha20poly1305.pcap'), '-o', 'tls.keylog_file: {}'.format(key_file), '-q', '-z', 'follow,tls,ascii,0', )) self.assertTrue(self.grepOutput('TLS13-CHACHA20-POLY1305-SHA256')) def test_tls13_rfc8446(self, cmd_tshark, dirs, features, capture_file): '''TLS 1.3 (normal session, then early data followed by normal data).''' if not features.have_libgcrypt16: self.skipTest('Requires GCrypt 1.6 or later.') key_file = os.path.join(dirs.key_dir, 'tls13-rfc8446.keys') proc = self.runProcess((cmd_tshark, '-r', capture_file('tls13-rfc8446.pcap'), '-otls.keylog_file:{}'.format(key_file), '-Y', 'http', '-Tfields', '-e', 'frame.number', '-e', 'http.request.uri', '-e', 'http.file_data', '-E', 'separator=|', )) self.assertEqual([ r'5|/first|', r'6||Request for /first, version TLSv1.3, Early data: no\n', r'8|/early|', r'10||Request for /early, version TLSv1.3, Early data: yes\n', r'12|/second|', r'13||Request for /second, version TLSv1.3, Early data: yes\n', ], proc.stdout_str.splitlines()) def test_tls13_rfc8446_noearly(self, cmd_tshark, dirs, features, capture_file): '''TLS 1.3 (with undecryptable early data).''' if not features.have_libgcrypt16: self.skipTest('Requires GCrypt 1.6 or later.') key_file = os.path.join(dirs.key_dir, 'tls13-rfc8446-noearly.keys') proc = self.runProcess((cmd_tshark, '-r', capture_file('tls13-rfc8446.pcap'), '-otls.keylog_file:{}'.format(key_file), '-Y', 'http', '-Tfields', '-e', 'frame.number', '-e', 'http.request.uri', '-e', 'http.file_data', '-E', 'separator=|', )) self.assertEqual([ r'5|/first|', r'6||Request for /first, version TLSv1.3, Early data: no\n', r'10||Request for /early, version TLSv1.3, Early data: yes\n', r'12|/second|', r'13||Request for /second, version TLSv1.3, Early data: yes\n', ], proc.stdout_str.splitlines()) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_zigbee(subprocesstest.SubprocessTestCase): def test_zigbee(self, cmd_tshark, capture_file): '''ZigBee''' # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=7022 self.runProcess((cmd_tshark, '-r', capture_file('sample_control4_2012-03-24.pcap'), '-Tfields', '-e', 'data.data', '-Y', 'zbee_aps', )) self.assertTrue(self.grepOutput('3067636338652063342e646d2e747620')) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_ansi_c1222(subprocesstest.SubprocessTestCase): def test_ansi_c1222(self, cmd_tshark, capture_file): '''ANSI C12.22''' # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9196 self.runProcess((cmd_tshark, '-r', capture_file('c1222_std_example8.pcap'), '-o', 'c1222.decrypt: TRUE', '-o', 'c1222.baseoid: 2.16.124.113620.1.22.0', '-Tfields', '-e', 'c1222.data', )) self.assertTrue(self.grepOutput('00104d414e55464143545552455220534e2092')) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_dvb_ci(subprocesstest.SubprocessTestCase): def test_dvb_ci(self, cmd_tshark, capture_file): '''DVB-CI''' # simplified version of the sample capture in # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6700 self.runProcess((cmd_tshark, '-r', capture_file('dvb-ci_UV1_0000.pcap'), '-o', 'dvb-ci.sek: 00000000000000000000000000000000', '-o', 'dvb-ci.siv: 00000000000000000000000000000000', '-Tfields', '-e', 'dvb-ci.cc.sac.padding', )) self.assertTrue(self.grepOutput('800000000000000000000000')) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_ipsec(subprocesstest.SubprocessTestCase): def test_ipsec_esp(self, cmd_tshark, capture_file): '''IPsec ESP''' # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=12671 self.runProcess((cmd_tshark, '-r', capture_file('esp-bug-12671.pcapng.gz'), '-o', 'esp.enable_encryption_decode: TRUE', '-Tfields', '-e', 'data.data', )) self.assertTrue(self.grepOutput('08090a0b0c0d0e0f1011121314151617')) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_ike_isakmp(subprocesstest.SubprocessTestCase): def test_ikev1_certs(self, cmd_tshark, capture_file): '''IKEv1 (ISAKMP) with certificates''' # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=7951 self.runProcess((cmd_tshark, '-r', capture_file('ikev1-certs.pcap'), '-Tfields', '-e', 'x509sat.printableString', )) self.assertTrue(self.grepOutput('OpenSwan')) def test_ikev1_simultaneous(self, cmd_tshark, capture_file): '''IKEv1 (ISAKMP) simultaneous exchanges''' # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=12610 self.runProcess((cmd_tshark, '-r', capture_file('ikev1-bug-12610.pcapng.gz'), '-Tfields', '-e', 'isakmp.hash', )) self.assertTrue(self.grepOutput('b52521f774967402c9f6cee95fd17e5b')) def test_ikev1_unencrypted(self, cmd_tshark, capture_file): '''IKEv1 (ISAKMP) unencrypted phase 1''' # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=12620 self.runProcess((cmd_tshark, '-r', capture_file('ikev1-bug-12620.pcapng.gz'), '-Tfields', '-e', 'isakmp.hash', )) self.assertTrue(self.grepOutput('40043b640f4373250d5ac3a1fb63153c')) def test_ikev2_3des_sha160(self, cmd_tshark, capture_file): '''IKEv2 decryption test (3DES-CBC/SHA1_160)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-3des-sha1_160.pcap'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('02f7a0d5f1fdc8ea81039818c65bb9bd09af9b8917319b887ff9ba3046c344c7')) def test_ikev2_aes128_ccm12(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-128-CCM-12) - with CBC-MAC verification''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes128ccm12.pcap'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('c2104394299e1ffe7908ea720ad5d13717a0d454e4fa0a2128ea689411f479c4')) def test_ikev2_aes128_ccm12_2(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-128-CCM-12 using CTR mode, without checksum)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes128ccm12-2.pcap'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('aaa281c87b4a19046c57271d557488ca413b57228cb951f5fa9640992a0285b9')) def test_ikev2_aes192ctr_sha512(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-192-CTR/SHA2-512)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes192ctr.pcap'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('3ec23dcf9348485638407c754547aeb3085290082c49f583fdbae59263a20b4a')) def test_ikev2_aes256cbc_sha256(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-256-CBC/SHA2-256)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes256cbc.pcapng'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('e1a8d550064201a7ec024a85758d0673c61c5c510ac13bcd225d6327f50da3d3')) def test_ikev2_aes256ccm16(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-256-CCM-16)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes256ccm16.pcapng'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('fa2e74bdc01e30fb0b3ddc9723c9449095969da51f69e560209d2c2b7940210a')) def test_ikev2_aes256gcm16(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-256-GCM-16)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes256gcm16.pcap'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('9ab71f14ab553cad873a1aa70b99df155dee77cdcf3694b3b7527acbb9712ded')) def test_ikev2_aes256gcm8(self, cmd_tshark, capture_file): '''IKEv2 decryption test (AES-256-GCM-8)''' self.runProcess((cmd_tshark, '-r', capture_file('ikev2-decrypt-aes256gcm8.pcap'), '-Tfields', '-e', 'isakmp.auth.data', )) self.assertTrue(self.grepOutput('4a66d822d0afbc22ad9a92a2cf4287c920ad8ac3b069a4a7e75fe0a5d499f914')) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_http2(subprocesstest.SubprocessTestCase): def test_http2(self, cmd_tshark, capture_file, features): '''HTTP2 (HPACK)''' if not features.have_nghttp2: self.skipTest('Requires nghttp2.') self.runProcess((cmd_tshark, '-r', capture_file('packet-h2-14_headers.pcapng'), '-Tfields', '-e', 'http2.header.value', '-d', 'tcp.port==3000,http2', )) test_passed = self.grepOutput('nghttp2') if not test_passed: self.log_fd.write('\n\n-- Verbose output --\n\n') self.runProcess((cmd_tshark, '-r', capture_file('packet-h2-14_headers.pcapng'), '-V', '-d', 'tcp.port==3000,http2', )) self.assertTrue(test_passed) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_kerberos(subprocesstest.SubprocessTestCase): def test_kerberos(self, cmd_tshark, dirs, features, capture_file): '''Kerberos''' # Files are from krb-816.zip on the SampleCaptures page. if not features.have_kerberos: self.skipTest('Requires kerberos.') keytab_file = os.path.join(dirs.key_dir, 'krb-816.keytab') self.runProcess((cmd_tshark, '-r', capture_file('krb-816.pcap.gz'), '-o', 'kerberos.decrypt: TRUE', '-o', 'kerberos.file: {}'.format(keytab_file), '-Tfields', '-e', 'kerberos.keyvalue', )) # keyvalue: ccda7d48219f73c3b28311c4ba7242b3 self.assertTrue(self.grepOutput('ccda7d48219f73c3b28311c4ba7242b3')) @fixtures.fixture(scope='session') def run_wireguard_test(cmd_tshark, capture_file, features): if not features.have_libgcrypt17: fixtures.skip('Requires Gcrypt 1.7 or later') def runOne(self, args, keylog=None, pcap_file='wireguard-ping-tcp.pcap'): if keylog: keylog_file = self.filename_from_id('wireguard.keys') args += ['-owg.keylog_file:%s' % keylog_file] with open(keylog_file, 'w') as f: f.write("\n".join(keylog)) proc = self.runProcess([cmd_tshark, '-r', capture_file(pcap_file)] + args) lines = proc.stdout_str.splitlines() return lines return runOne @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_wireguard(subprocesstest.SubprocessTestCase): # The "foo_alt" keys are similar as "foo" except that some bits are changed. # The crypto library should be able to handle this and internally the # dissector uses MSB to recognize whether a private key is set. key_Spriv_i = 'AKeZaHwBxjiKLFnkY2unvEdOTtg4AL+M9dQXfopFVFk=' key_Spriv_i_alt = 'B6eZaHwBxjiKLFnkY2unvEdOTtg4AL+M9dQXfopFVJk=' key_Spub_i = 'Igge9KzRytKNwrgkzDE/8hrLu6Ly0OqVdvOPWhA5KR4=' key_Spriv_r = 'cFIxTUyBs1Qil414hBwEgvasEax8CKJ5IS5ZougplWs=' key_Spub_r = 'YDCttCs9e1J52/g9vEnwJJa+2x6RqaayAYMpSVQfGEY=' key_Epriv_i0 = 'sLGLJSOQfyz7JNJ5ZDzFf3Uz1rkiCMMjbWerNYcPFFU=' key_Epriv_i0_alt = 't7GLJSOQfyz7JNJ5ZDzFf3Uz1rkiCMMjbWerNYcPFJU=' key_Epriv_r0 = 'QC4/FZKhFf0b/eXEcCecmZNt6V6PXmRa4EWG1PIYTU4=' key_Epriv_i1 = 'ULv83D+y3vA0t2mgmTmWz++lpVsrP7i4wNaUEK2oX0E=' key_Epriv_r1 = 'sBv1dhsm63cbvWMv/XML+bvynBp9PTdY9Vvptu3HQlg=' # Ephemeral keys and PSK for wireguard-psk.pcap key_Epriv_i2 = 'iCv2VTi/BC/q0egU931KXrrQ4TSwXaezMgrhh7uCbXs=' key_Epriv_r2 = '8G1N3LnEqYC7+NW/b6mqceVUIGBMAZSm+IpwG1U0j0w=' key_psk2 = '//////////////////////////////////////////8=' key_Epriv_i3 = '+MHo9sfkjPsjCx7lbVhRLDvMxYvTirOQFDSdzAW6kUQ=' key_Epriv_r3 = '0G6t5j1B/We65MXVEBIGuRGYadwB2ITdvJovtAuATmc=' key_psk3 = 'iIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIg=' # dummy key that should not work with anything. key_dummy = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=' def test_mac1_public(self, run_wireguard_test): """Check that MAC1 identification using public keys work.""" lines = run_wireguard_test(self, [ '-ouat:wg_keys:"Public","%s"' % self.key_Spub_i, '-ouat:wg_keys:"Public","%s"' % self.key_Spub_r, '-Y', 'wg.receiver_pubkey', '-Tfields', '-e', 'frame.number', '-e', 'wg.receiver_pubkey', '-e', 'wg.receiver_pubkey.known_privkey', ]) self.assertEqual(4, len(lines)) self.assertIn('1\t%s\t0' % self.key_Spub_r, lines) self.assertIn('2\t%s\t0' % self.key_Spub_i, lines) self.assertIn('13\t%s\t0' % self.key_Spub_r, lines) self.assertIn('14\t%s\t0' % self.key_Spub_i, lines) def test_mac1_private(self, run_wireguard_test): """Check that MAC1 identification using private keys work.""" lines = run_wireguard_test(self, [ '-ouat:wg_keys:"Private","%s"' % self.key_Spriv_i, '-ouat:wg_keys:"Private","%s"' % self.key_Spriv_r, '-Y', 'wg.receiver_pubkey', '-Tfields', '-e', 'frame.number', '-e', 'wg.receiver_pubkey', '-e', 'wg.receiver_pubkey.known_privkey', ]) self.assertEqual(4, len(lines)) self.assertIn('1\t%s\t1' % self.key_Spub_r, lines) self.assertIn('2\t%s\t1' % self.key_Spub_i, lines) self.assertIn('13\t%s\t1' % self.key_Spub_r, lines) self.assertIn('14\t%s\t1' % self.key_Spub_i, lines) def test_decrypt_initiation_sprivr(self, run_wireguard_test): """Check for partial decryption using Spriv_r.""" lines = run_wireguard_test(self, [ '-ouat:wg_keys:"Private","%s"' % self.key_Spriv_r, '-Y', 'wg.type==1', '-Tfields', '-e', 'frame.number', '-e', 'wg.static', '-e', 'wg.static.known_pubkey', '-e', 'wg.static.known_privkey', '-e', 'wg.timestamp.nanoseconds', ]) # static pubkey is unknown because Spub_i is not added to wg_keys. self.assertIn('1\t%s\t0\t0\t%s' % (self.key_Spub_i, '356537872'), lines) self.assertIn('13\t%s\t0\t0\t%s' % (self.key_Spub_i, '490514356'), lines) def test_decrypt_initiation_ephemeral_only(self, run_wireguard_test): """Check for partial decryption using Epriv_i.""" lines = run_wireguard_test(self, [ '-ouat:wg_keys:"Public","%s"' % self.key_Spub_r, '-Y', 'wg.type==1', '-Tfields', '-e', 'frame.number', '-e', 'wg.ephemeral.known_privkey', '-e', 'wg.static', '-e', 'wg.timestamp.nanoseconds', ], keylog=[ 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_i0, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_i1, ]) # The current implementation tries to write as much decrypted data as # possible, even if the full handshake cannot be derived. self.assertIn('1\t1\t%s\t%s' % (self.key_Spub_i, ''), lines) self.assertIn('13\t1\t%s\t%s' % (self.key_Spub_i, ''), lines) def test_decrypt_full_initiator(self, run_wireguard_test): """ Check for full handshake decryption using Spriv_r + Epriv_i. The public key Spub_r is provided via the key log as well. """ lines = run_wireguard_test(self, [ '-Tfields', '-e', 'frame.number', '-e', 'wg.ephemeral.known_privkey', '-e', 'wg.static', '-e', 'wg.timestamp.nanoseconds', '-e', 'wg.handshake_ok', '-e', 'icmp.type', '-e', 'tcp.dstport', ], keylog=[ ' REMOTE_STATIC_PUBLIC_KEY = %s' % self.key_Spub_r, ' LOCAL_STATIC_PRIVATE_KEY = %s' % self.key_Spriv_i_alt, ' LOCAL_EPHEMERAL_PRIVATE_KEY = %s' % self.key_Epriv_i0_alt, ' LOCAL_EPHEMERAL_PRIVATE_KEY = %s' % self.key_Epriv_i1, ]) self.assertIn('1\t1\t%s\t%s\t\t\t' % (self.key_Spub_i, '356537872'), lines) self.assertIn('2\t0\t\t\t1\t\t', lines) self.assertIn('3\t\t\t\t\t8\t', lines) self.assertIn('4\t\t\t\t\t0\t', lines) self.assertIn('13\t1\t%s\t%s\t\t\t' % (self.key_Spub_i, '490514356'), lines) self.assertIn('14\t0\t\t\t1\t\t', lines) self.assertIn('17\t\t\t\t\t\t443', lines) self.assertIn('18\t\t\t\t\t\t49472', lines) def test_decrypt_full_responder(self, run_wireguard_test): """Check for full handshake decryption using responder secrets.""" lines = run_wireguard_test(self, [ '-Tfields', '-e', 'frame.number', '-e', 'wg.ephemeral.known_privkey', '-e', 'wg.static', '-e', 'wg.timestamp.nanoseconds', '-e', 'wg.handshake_ok', '-e', 'icmp.type', '-e', 'tcp.dstport', ], keylog=[ 'REMOTE_STATIC_PUBLIC_KEY=%s' % self.key_Spub_i, 'LOCAL_STATIC_PRIVATE_KEY=%s' % self.key_Spriv_r, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_r0, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_r1, ]) self.assertIn('1\t0\t%s\t%s\t\t\t' % (self.key_Spub_i, '356537872'), lines) self.assertIn('2\t1\t\t\t1\t\t', lines) self.assertIn('3\t\t\t\t\t8\t', lines) self.assertIn('4\t\t\t\t\t0\t', lines) self.assertIn('13\t0\t%s\t%s\t\t\t' % (self.key_Spub_i, '490514356'), lines) self.assertIn('14\t1\t\t\t1\t\t', lines) self.assertIn('17\t\t\t\t\t\t443', lines) self.assertIn('18\t\t\t\t\t\t49472', lines) def test_decrypt_psk_initiator(self, run_wireguard_test): """Check whether PSKs enable decryption for initiation keys.""" lines = run_wireguard_test(self, [ '-Tfields', '-e', 'frame.number', '-e', 'wg.handshake_ok', ], keylog=[ 'REMOTE_STATIC_PUBLIC_KEY = %s' % self.key_Spub_r, 'LOCAL_STATIC_PRIVATE_KEY = %s' % self.key_Spriv_i, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_i2, 'PRESHARED_KEY=%s' % self.key_psk2, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_r3, 'PRESHARED_KEY=%s' % self.key_psk3, ], pcap_file='wireguard-psk.pcap') self.assertIn('2\t1', lines) self.assertIn('4\t1', lines) def test_decrypt_psk_responder(self, run_wireguard_test): """Check whether PSKs enable decryption for responder keys.""" lines = run_wireguard_test(self, [ '-Tfields', '-e', 'frame.number', '-e', 'wg.handshake_ok', ], keylog=[ 'REMOTE_STATIC_PUBLIC_KEY=%s' % self.key_Spub_i, 'LOCAL_STATIC_PRIVATE_KEY=%s' % self.key_Spriv_r, # Epriv_r2 needs psk2. This tests handling of duplicate ephemeral # keys with multiple PSKs. It should not have adverse effects. 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_r2, 'PRESHARED_KEY=%s' % self.key_dummy, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_r2, 'PRESHARED_KEY=%s' % self.key_psk2, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_i3, 'PRESHARED_KEY=%s' % self.key_psk3, # Epriv_i3 needs psk3, this tests that additional keys again have no # bad side-effects. 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_i3, 'PRESHARED_KEY=%s' % self.key_dummy, ], pcap_file='wireguard-psk.pcap') self.assertIn('2\t1', lines) self.assertIn('4\t1', lines) def test_decrypt_psk_wrong_orderl(self, run_wireguard_test): """Check that the wrong order of lines indeed fail decryption.""" lines = run_wireguard_test(self, [ '-Tfields', '-e', 'frame.number', '-e', 'wg.handshake_ok', ], keylog=[ 'REMOTE_STATIC_PUBLIC_KEY=%s' % self.key_Spub_i, 'LOCAL_STATIC_PRIVATE_KEY=%s' % self.key_Spriv_r, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_r2, 'LOCAL_EPHEMERAL_PRIVATE_KEY=%s' % self.key_Epriv_i3, 'PRESHARED_KEY=%s' % self.key_psk2, # note: swapped with previous line 'PRESHARED_KEY=%s' % self.key_psk3, ], pcap_file='wireguard-psk.pcap') self.assertIn('2\t0', lines) self.assertIn('4\t0', lines) @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures class case_decrypt_knxip(subprocesstest.SubprocessTestCase): # Capture files for these tests contain single telegrams. # For realistic (live captured) KNX/IP telegram sequences, see: # https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=14825 def test_knxip_data_security_decryption_ok(self, cmd_tshark, capture_file): '''KNX/IP: Data Security decryption OK''' # capture_file('knxip_DataSec.pcap') contains KNX/IP ConfigReq DataSec PropExtValueWriteCon telegram self.runProcess((cmd_tshark, '-r', capture_file('knxip_DataSec.pcap'), '-o', 'kip.key_1:00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F', )) self.assertTrue(self.grepOutput(' DataSec ')) self.assertTrue(self.grepOutput(' PropExtValueWriteCon ')) def test_knxip_data_security_decryption_fails(self, cmd_tshark, capture_file): '''KNX/IP: Data Security decryption fails''' # capture_file('knxip_DataSec.pcap') contains KNX/IP ConfigReq DataSec PropExtValueWriteCon telegram self.runProcess((cmd_tshark, '-r', capture_file('knxip_DataSec.pcap'), '-o', 'kip.key_1:""', # "" is really necessary, otherwise test fails )) self.assertTrue(self.grepOutput(' DataSec ')) self.assertFalse(self.grepOutput(' PropExtValueWriteCon ')) def test_knxip_secure_wrapper_decryption_ok(self, cmd_tshark, capture_file): '''KNX/IP: SecureWrapper decryption OK''' # capture_file('knxip_SecureWrapper.pcap') contains KNX/IP SecureWrapper RoutingInd telegram self.runProcess((cmd_tshark, '-r', capture_file('knxip_SecureWrapper.pcap'), '-o', 'kip.key_1:00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F', )) self.assertTrue(self.grepOutput(' SecureWrapper ')) self.assertTrue(self.grepOutput(' RoutingInd ')) def test_knxip_secure_wrapper_decryption_fails(self, cmd_tshark, capture_file): '''KNX/IP: SecureWrapper decryption fails''' # capture_file('knxip_SecureWrapper.pcap') contains KNX/IP SecureWrapper RoutingInd telegram self.runProcess((cmd_tshark, '-r', capture_file('knxip_SecureWrapper.pcap'), '-o', 'kip.key_1:""', # "" is really necessary, otherwise test fails )) self.assertTrue(self.grepOutput(' SecureWrapper ')) self.assertFalse(self.grepOutput(' RoutingInd ')) def test_knxip_timer_notify_authentication_ok(self, cmd_tshark, capture_file): '''KNX/IP: TimerNotify authentication OK''' # capture_file('knxip_TimerNotify.pcap') contains KNX/IP TimerNotify telegram self.runProcess((cmd_tshark, '-r', capture_file('knxip_TimerNotify.pcap'), '-o', 'kip.key_1:00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F', )) self.assertTrue(self.grepOutput(' TimerNotify ')) self.assertTrue(self.grepOutput(' OK$')) def test_knxip_timer_notify_authentication_fails(self, cmd_tshark, capture_file): '''KNX/IP: TimerNotify authentication fails''' # capture_file('knxip_TimerNotify.pcap') contains KNX/IP TimerNotify telegram self.runProcess((cmd_tshark, '-r', capture_file('knxip_TimerNotify.pcap'), '-o', 'kip.key_1:""', # "" is really necessary, otherwise test fails )) self.assertTrue(self.grepOutput(' TimerNotify ')) self.assertFalse(self.grepOutput(' OK$')) def test_knxip_keyring_xml_import(self, cmd_tshark, dirs, capture_file): '''KNX/IP: keyring.xml import''' # key_file "keyring.xml" contains KNX decryption keys key_file = os.path.join(dirs.key_dir, 'knx_keyring.xml') # capture_file('empty.pcap') is empty # Write extracted key info to stdout self.runProcess((cmd_tshark, '-o', 'kip.key_file:' + key_file, '-o', 'kip.key_info_file:-', '-r', capture_file('empty.pcap'), )) self.assertTrue(self.grepOutput('^MCA 224[.]0[.]23[.]12 key A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF$')) self.assertTrue(self.grepOutput('^GA 1/7/131 sender 1[.]1[.]1$')) self.assertTrue(self.grepOutput('^GA 1/7/131 sender 1[.]1[.]3$')) self.assertTrue(self.grepOutput('^GA 1/7/131 sender 1[.]1[.]4$')) self.assertTrue(self.grepOutput('^GA 1/7/132 sender 1[.]1[.]2$')) self.assertTrue(self.grepOutput('^GA 1/7/132 sender 1[.]1[.]4$')) self.assertTrue(self.grepOutput('^GA 6/7/191 sender 1[.]1[.]1$')) self.assertTrue(self.grepOutput('^GA 0/1/0 sender 1[.]1[.]1$')) self.assertTrue(self.grepOutput('^GA 0/1/0 sender 1[.]1[.]3$')) self.assertTrue(self.grepOutput('^GA 0/1/0 sender 1[.]1[.]4$')) self.assertTrue(self.grepOutput('^GA 0/1/0 key A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF$')) self.assertTrue(self.grepOutput('^GA 1/7/131 key A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF$')) self.assertTrue(self.grepOutput('^GA 1/7/132 key A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF$')) self.assertTrue(self.grepOutput('^GA 6/7/191 key A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]1 key B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]1 SeqNr 45678$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]2 key B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]2 SeqNr 34567$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]3 key B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]3 SeqNr 23456$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]4 key B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF$')) self.assertTrue(self.grepOutput('^IA 1[.]1[.]4 SeqNr 12345$')) self.assertTrue(self.grepOutput('^IA 2[.]1[.]0 key B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF$')) self.assertTrue(self.grepOutput('^IA 2[.]1[.]0 SeqNr 1234$'))