├── .gitattributes ├── .gitignore ├── README.md ├── LICENSE └── smb_ms17_010.py /.gitattributes: -------------------------------------------------------------------------------- 1 | scanners/* linguist-vendored 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # MS17-010_SUBNET 3 | 4 | This is a simple fork of the Python scanner for MS17-010 at https://github.com/RiskSense-Ops/MS17-010. 5 | 6 | The only modification is that the script now takes one or more IPv4 CIDR networks as input, and checks through every host on each of them sequentially. I just built it to speed some scanning I was doing. 7 | 8 | Usage (for two example networks: 192.168.1.0/24 and 172.16.30.0/23): 9 | 10 | Unix: python2.7 smb_ms17_010.py 192.168.1.0/24 172.16.30.0/23 11 | Windows: py -2 smb_ms17_010.py 192.168.1.0/24 172.16.30.0/2 12 | 13 | It requires the 'ipaddress' library for Python 2, you can add it with pip as follows: 14 | 15 | Unix: python2.7 -m pip install py2-ipaddress 16 | Windows: py -2 -m pip install py2-ipaddress 17 | 18 | 19 | Original description from RiskSense below: 20 | 21 | ******************************************************************** 22 | 23 | # MS17-010 24 | MS17-010 is the Microsoft security bulletin which fixes several remote code execution vulnerabilities in the SMB service on Windows systems. 25 | 26 | There are numerous things about MS17-010 that make it esoteric, such as manipulating the Windows kernel pool heap allocations, running remote Windows ring 0 shellcode, and the intricacies of the different SMB protocol versions. 27 | 28 | We previously improved the ExtraBacon exploit. https://github.com/RiskSense-Ops/CVE-2016-6366 29 | 30 | ## Scanners 31 | The scanner uses uncredentialed information leakage to determine if the MS17-010 patch is installed on a host. If it is not installed, it will also check for a DoublePulsar infections. 32 | 33 | ## Resources 34 | - https://zerosum0x0.blogspot.com/2017/04/doublepulsar-initial-smb-backdoor-ring.html 35 | - https://countercept.com/our-thinking/analyzing-the-doublepulsar-kernel-dll-injection-technique/ 36 | - https://www.rapid7.com/db/modules/auxiliary/scanner/smb/smb_ms17_010 37 | 38 | ## Disclaimer 39 | This code serves an important business purpose for penetration testers and others to show impact to organizations, without having to run NSA malware binaries. We must ask that you use these tools for their intended, lawful purposes, and only with consent of targets. 40 | 41 | ### License 42 | Apache 2.0 and MSF 43 | 44 | ### Credits 45 | - @zerosum0x0 46 | - @jennamagius 47 | - @The_Naterz 48 | - @Aleph___Naught 49 | - @nixawk 50 | - @JukeLennings (Countercept) 51 | 52 | ### Acknowledgements 53 | - Shadow Brokers 54 | - Equation Group 55 | - skape 56 | - Stephen Fewer 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /smb_ms17_010.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python2 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Written by nixawk based on auxiliary/scanner/smb/smb_ms17_010 Metasploit module 5 | # 6 | 7 | # Loop in main() to scan a subnet using ipaddress library added by topranks. 8 | 9 | """ 10 | $ python2.7 smb_exploit.py 192.168.206.152 11 | [+] [192.168.206.152] is likely VULNERABLE to MS17-010! (Windows 7 Ultimate 7600) 12 | 13 | $ python2.7 smb_exploit.py 192.168.206.130 14 | [+] [192.168.206.130] is likely VULNERABLE to MS17-010! (Windows 5.1) 15 | """ 16 | 17 | from ctypes import * 18 | import socket 19 | import struct 20 | import logging 21 | import ipaddress 22 | 23 | logging.basicConfig(level=logging.INFO, format="%(message)s") 24 | log = logging.getLogger(__file__) 25 | 26 | # negotiate_proto_request 27 | # session_setup_andx_request 28 | # tree_connect_andx_request 29 | # peeknamedpipe_request 30 | # trans2 request 31 | 32 | 33 | class SMB_HEADER(Structure): 34 | """SMB Header decoder. 35 | """ 36 | 37 | _pack_ = 1 # Alignment 38 | 39 | _fields_ = [ 40 | ("server_component", c_uint32), 41 | ("smb_command", c_uint8), 42 | ("error_class", c_uint8), 43 | ("reserved1", c_uint8), 44 | ("error_code", c_uint16), 45 | ("flags", c_uint8), 46 | ("flags2", c_uint16), 47 | ("process_id_high", c_uint16), 48 | ("signature", c_uint64), 49 | ("reserved2", c_uint16), 50 | ("tree_id", c_uint16), 51 | ("process_id", c_uint16), 52 | ("user_id", c_uint16), 53 | ("multiplex_id", c_uint16) 54 | ] 55 | 56 | def __new__(self, buffer=None): 57 | return self.from_buffer_copy(buffer) 58 | 59 | def __init__(self, buffer): 60 | log.debug("server_component : %04x" % self.server_component) 61 | log.debug("smb_command : %01x" % self.smb_command) 62 | log.debug("error_class : %01x" % self.error_class) 63 | log.debug("error_code : %02x" % self.error_code) 64 | log.debug("flags : %01x" % self.flags) 65 | log.debug("flags2 : %02x" % self.flags2) 66 | log.debug("process_id_high : %02x" % self.process_id_high) 67 | log.debug("signature : %08x" % self.signature) 68 | log.debug("reserved2 : %02x" % self.reserved2) 69 | log.debug("tree_id : %02x" % self.tree_id) 70 | log.debug("process_id : %02x" % self.process_id) 71 | log.debug("user_id : %02x" % self.user_id) 72 | log.debug("multiplex_id : %02x" % self.multiplex_id) 73 | 74 | 75 | def generate_smb_proto_payload(*protos): 76 | """Generate SMB Protocol. Pakcet protos in order. 77 | """ 78 | hexdata = [] 79 | for proto in protos: 80 | hexdata.extend(proto) 81 | return "".join(hexdata) 82 | 83 | 84 | def calculate_doublepulsar_xor_key(s): 85 | """Calaculate Doublepulsar Xor Key 86 | """ 87 | x = (2 * s ^ (((s & 0xff00 | (s << 16)) << 8) | (((s >> 16) | s & 0xff0000) >> 8))) 88 | x = x & 0xffffffff # this line was added just to truncate to 32 bits 89 | return x 90 | 91 | 92 | def negotiate_proto_request(): 93 | """Generate a negotiate_proto_request packet. 94 | """ 95 | log.debug("generate negotiate request") 96 | netbios = [ 97 | '\x00', # 'Message_Type' 98 | '\x00\x00\x54' # 'Length' 99 | ] 100 | 101 | smb_header = [ 102 | '\xFF\x53\x4D\x42', # 'server_component': .SMB 103 | '\x72', # 'smb_command': Negotiate Protocol 104 | '\x00\x00\x00\x00', # 'nt_status' 105 | '\x18', # 'flags' 106 | '\x01\x28', # 'flags2' 107 | '\x00\x00', # 'process_id_high' 108 | '\x00\x00\x00\x00\x00\x00\x00\x00', # 'signature' 109 | '\x00\x00', # 'reserved' 110 | '\x00\x00', # 'tree_id' 111 | '\x2F\x4B', # 'process_id' 112 | '\x00\x00', # 'user_id' 113 | '\xC5\x5E' # 'multiplex_id' 114 | ] 115 | 116 | negotiate_proto_request = [ 117 | '\x00', # 'word_count' 118 | '\x31\x00', # 'byte_count' 119 | 120 | # Requested Dialects 121 | '\x02', # 'dialet_buffer_format' 122 | '\x4C\x41\x4E\x4D\x41\x4E\x31\x2E\x30\x00', # 'dialet_name': LANMAN1.0 123 | 124 | '\x02', # 'dialet_buffer_format' 125 | '\x4C\x4D\x31\x2E\x32\x58\x30\x30\x32\x00', # 'dialet_name': LM1.2X002 126 | 127 | '\x02', # 'dialet_buffer_format' 128 | '\x4E\x54\x20\x4C\x41\x4E\x4D\x41\x4E\x20\x31\x2E\x30\x00', # 'dialet_name3': NT LANMAN 1.0 129 | 130 | '\x02', # 'dialet_buffer_format' 131 | '\x4E\x54\x20\x4C\x4D\x20\x30\x2E\x31\x32\x00' # 'dialet_name4': NT LM 0.12 132 | ] 133 | 134 | return generate_smb_proto_payload(netbios, smb_header, negotiate_proto_request) 135 | 136 | 137 | def session_setup_andx_request(): 138 | """Generate session setuo andx request. 139 | """ 140 | log.debug("generate session setup andx request") 141 | netbios = [ 142 | '\x00', # 'Message_Type' 143 | '\x00\x00\x63' # 'Length' 144 | ] 145 | 146 | smb_header = [ 147 | '\xFF\x53\x4D\x42', # 'server_component': .SMB 148 | '\x73', # 'smb_command': Session Setup AndX 149 | '\x00\x00\x00\x00', # 'nt_status' 150 | '\x18', # 'flags' 151 | '\x01\x20', # 'flags2' 152 | '\x00\x00', # 'process_id_high' 153 | '\x00\x00\x00\x00\x00\x00\x00\x00', # 'signature' 154 | '\x00\x00', # 'reserved' 155 | '\x00\x00', # 'tree_id' 156 | '\x2F\x4B', # 'process_id' 157 | '\x00\x00', # 'user_id' 158 | '\xC5\x5E' # 'multiplex_id' 159 | ] 160 | 161 | session_setup_andx_request = [ 162 | '\x0D', # Word Count 163 | '\xFF', # AndXCommand: No further command 164 | '\x00', # Reserved 165 | '\x00\x00', # AndXOffset 166 | '\xDF\xFF', # Max Buffer 167 | '\x02\x00', # Max Mpx Count 168 | '\x01\x00', # VC Number 169 | '\x00\x00\x00\x00', # Session Key 170 | '\x00\x00', # ANSI Password Length 171 | '\x00\x00', # Unicode Password Length 172 | '\x00\x00\x00\x00', # Reserved 173 | '\x40\x00\x00\x00', # Capabilities 174 | '\x26\x00', # Byte Count 175 | '\x00', # Account 176 | '\x2e\x00', # Primary Domain 177 | '\x57\x69\x6e\x64\x6f\x77\x73\x20\x32\x30\x30\x30\x20\x32\x31\x39\x35\x00', # Native OS: Windows 2000 2195 178 | '\x57\x69\x6e\x64\x6f\x77\x73\x20\x32\x30\x30\x30\x20\x35\x2e\x30\x00', # Native OS: Windows 2000 5.0 179 | ] 180 | 181 | return generate_smb_proto_payload(netbios, smb_header, session_setup_andx_request) 182 | 183 | 184 | def tree_connect_andx_request(ip, userid): 185 | """Generate tree connect andx request. 186 | """ 187 | log.debug("generate tree connect andx request") 188 | 189 | netbios = [ 190 | '\x00', # 'Message_Type' 191 | '\x00\x00\x47' # 'Length' 192 | ] 193 | 194 | smb_header = [ 195 | '\xFF\x53\x4D\x42', # 'server_component': .SMB 196 | '\x75', # 'smb_command': Tree Connect AndX 197 | '\x00\x00\x00\x00', # 'nt_status' 198 | '\x18', # 'flags' 199 | '\x01\x20', # 'flags2' 200 | '\x00\x00', # 'process_id_high' 201 | '\x00\x00\x00\x00\x00\x00\x00\x00', # 'signature' 202 | '\x00\x00', # 'reserved' 203 | '\x00\x00', # 'tree_id' 204 | '\x2F\x4B', # 'process_id' 205 | userid, # 'user_id' 206 | '\xC5\x5E' # 'multiplex_id' 207 | ] 208 | 209 | ipc = "\\\\{}\IPC$\x00".format(ip) 210 | log.debug("Connecting to {} with UID = {}".format(ipc, userid)) 211 | 212 | tree_connect_andx_request = [ 213 | '\x04', # Word Count 214 | '\xFF', # AndXCommand: No further commands 215 | '\x00', # Reserved 216 | '\x00\x00', # AndXOffset 217 | '\x00\x00', # Flags 218 | '\x01\x00', # Password Length 219 | '\x1A\x00', # Byte Count 220 | '\x00', # Password 221 | ipc.encode(), # \\xxx.xxx.xxx.xxx\IPC$ 222 | '\x3f\x3f\x3f\x3f\x3f\x00' # Service 223 | ] 224 | 225 | length = len("".join(smb_header)) + len("".join(tree_connect_andx_request)) 226 | # netbios[1] = '\x00' + struct.pack('>H', length) 227 | netbios[1] = struct.pack(">L", length)[-3:] 228 | 229 | return generate_smb_proto_payload(netbios, smb_header, tree_connect_andx_request) 230 | 231 | 232 | def peeknamedpipe_request(treeid, processid, userid, multiplex_id): 233 | """Generate tran2 request 234 | """ 235 | log.debug("generate peeknamedpipe request") 236 | netbios = [ 237 | '\x00', # 'Message_Type' 238 | '\x00\x00\x4a' # 'Length' 239 | ] 240 | 241 | smb_header = [ 242 | '\xFF\x53\x4D\x42', # 'server_component': .SMB 243 | '\x25', # 'smb_command': Trans2 244 | '\x00\x00\x00\x00', # 'nt_status' 245 | '\x18', # 'flags' 246 | '\x01\x28', # 'flags2' 247 | '\x00\x00', # 'process_id_high' 248 | '\x00\x00\x00\x00\x00\x00\x00\x00', # 'signature' 249 | '\x00\x00', # 'reserved' 250 | treeid, 251 | processid, 252 | userid, 253 | multiplex_id 254 | ] 255 | 256 | tran_request = [ 257 | '\x10', # Word Count 258 | '\x00\x00', # Total Parameter Count 259 | '\x00\x00', # Total Data Count 260 | '\xff\xff', # Max Parameter Count 261 | '\xff\xff', # Max Data Count 262 | '\x00', # Max Setup Count 263 | '\x00', # Reserved 264 | '\x00\x00', # Flags 265 | '\x00\x00\x00\x00', # Timeout: Return immediately 266 | '\x00\x00', # Reversed 267 | '\x00\x00', # Parameter Count 268 | '\x4a\x00', # Parameter Offset 269 | '\x00\x00', # Data Count 270 | '\x4a\x00', # Data Offset 271 | '\x02', # Setup Count 272 | '\x00', # Reversed 273 | '\x23\x00', # SMB Pipe Protocol: Function: PeekNamedPipe (0x0023) 274 | '\x00\x00', # SMB Pipe Protocol: FID 275 | '\x07\x00', 276 | '\x5c\x50\x49\x50\x45\x5c\x00' # \PIPE\ 277 | ] 278 | 279 | return generate_smb_proto_payload(netbios, smb_header, tran_request) 280 | 281 | 282 | def trans2_request(treeid, processid, userid, multiplex_id): 283 | """Generate trans2 request. 284 | """ 285 | log.debug("generate tran2 request") 286 | netbios = [ 287 | '\x00', # 'Message_Type' 288 | '\x00\x00\x4f' # 'Length' 289 | ] 290 | 291 | smb_header = [ 292 | '\xFF\x53\x4D\x42', # 'server_component': .SMB 293 | '\x32', # 'smb_command': Trans2 294 | '\x00\x00\x00\x00', # 'nt_status' 295 | '\x18', # 'flags' 296 | '\x07\xc0', # 'flags2' 297 | '\x00\x00', # 'process_id_high' 298 | '\x00\x00\x00\x00\x00\x00\x00\x00', # 'signature' 299 | '\x00\x00', # 'reserved' 300 | treeid, 301 | processid, 302 | userid, 303 | multiplex_id 304 | ] 305 | 306 | trans2_request = [ 307 | '\x0f', # Word Count 308 | '\x0c\x00', # Total Parameter Count 309 | '\x00\x00', # Total Data Count 310 | '\x01\x00', # Max Parameter Count 311 | '\x00\x00', # Max Data Count 312 | '\x00', # Max Setup Count 313 | '\x00', # Reserved 314 | '\x00\x00', # Flags 315 | '\xa6\xd9\xa4\x00', # Timeout: 3 hours, 3.622 seconds 316 | '\x00\x00', # Reversed 317 | '\x0c\x00', # Parameter Count 318 | '\x42\x00', # Parameter Offset 319 | '\x00\x00', # Data Count 320 | '\x4e\x00', # Data Offset 321 | '\x01', # Setup Count 322 | '\x00', # Reserved 323 | '\x0e\x00', # subcommand: SESSION_SETUP 324 | '\x00\x00', # Byte Count 325 | '\x0c\x00' + '\x00' * 12 326 | ] 327 | 328 | return generate_smb_proto_payload(netbios, smb_header, trans2_request) 329 | 330 | def check(ip, port=445): 331 | """Check if MS17_010 SMB Vulnerability exists. 332 | """ 333 | try: 334 | buffersize = 1024 335 | timeout = 5.0 336 | 337 | # Send smb request based on socket. 338 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 339 | client.settimeout(timeout) 340 | client.connect((ip, port)) 341 | 342 | # SMB - Negotiate Protocol Request 343 | raw_proto = negotiate_proto_request() 344 | client.send(raw_proto) 345 | tcp_response = client.recv(buffersize) 346 | 347 | # SMB - Session Setup AndX Request 348 | raw_proto = session_setup_andx_request() 349 | client.send(raw_proto) 350 | tcp_response = client.recv(buffersize) 351 | 352 | netbios = tcp_response[:4] 353 | smb_header = tcp_response[4:36] # SMB Header: 32 bytes 354 | smb = SMB_HEADER(smb_header) 355 | 356 | user_id = struct.pack('".format(sys.argv[0])) 424 | sys.exit(1) 425 | else: 426 | i=0 427 | for subnet in sys.argv: 428 | if(i!=0): 429 | try: 430 | subnet=ipaddress.IPv4Network(subnet.decode('utf-8')) 431 | for ip in subnet: 432 | check(str(ip)) 433 | except ipaddress.AddressValueError: 434 | print("[-] [{}] Exception: Not a valid IPv4 subnet".format(str(subnet))) 435 | i=i+1 436 | 437 | 438 | ## References 439 | 440 | # https://blogs.technet.microsoft.com/msrc/2017/04/14/protecting-customers-and-evaluating-risk/ 441 | # https://www.rapid7.com/db/modules/auxiliary/scanner/smb/smb_ms17_010 442 | # https://github.com/rapid7/metasploit-framework/blob/master/modules/auxiliary/scanner/smb/smb_ms17_010.rb 443 | # https://www.symantec.com/security_response/vulnerability.jsp?bid=96707 444 | # https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-SMB2/[MS-SMB2]-151016.pdf 445 | # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365233(v=vs.85).aspx 446 | # https://technet.microsoft.com/en-us/library/security/ms17-010.aspx 447 | # https://community.rapid7.com/community/metasploit/blog/2017/04/03/introducing-rubysmb-the-protocol-library-nobody-else-wanted-to-write 448 | # https://msdn.microsoft.com/en-us/library/ee441741.aspx 449 | # https://github.com/countercept/doublepulsar-detection-script/blob/master/detect_doublepulsar_smb.py 450 | # http://stackoverflow.com/questions/38735421/packing-an-integer-number-to-3-bytes-in-python 451 | # https://zerosum0x0.blogspot.com/2017/04/doublepulsar-initial-smb-backdoor-ring.html 452 | --------------------------------------------------------------------------------