Advertisement
Python253

smb_protocol_manager_eternalblue_vulnerability_checker

Apr 11th, 2024 (edited)
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: smb_protocol_manager_eternalblue_vulnerability_checker.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Script to manage SMB (Server Message Block) protocols
  9.  
  10. SMB is a network file sharing protocol that allows applications to read and write to files and request services from server programs in a computer network.
  11. SMB1, SMB2, and SMB3 are different versions of this protocol. SMB1 is known to have security vulnerabilities, such as the EternalBlue exploit.
  12. It is recommended to disable SMB1 due to its vulnerabilities. SMB2 and SMB3 are more secure and should be used instead.
  13.  
  14. This script provides options to:
  15. 1. Verify the status of SMB1 protocol.
  16. 2. Verify the status of SMB2 protocol.
  17. 3. Verify the status of SMB3 protocol.
  18. 4. Enable SMB2 protocol.
  19. 5. Enable SMB3 protocol.
  20. 6. Disable SMB1 protocol.
  21. 7. Delete SMB1 registry key.
  22. 8. Create SMB1 registry key with disabled SMB1.
  23. 9. Enable SMB1 protocol (with a warning message about the potential security risks).
  24. 0. Exit the script.
  25.  
  26. WARNING: Enabling SMB1 is highly insecure and may expose your system to potential attacks.
  27. Only enable SMB1 for testing purposes and if you understand the risks involved.
  28.  
  29. Known exploits that use EternalBlue attack methods:
  30. 1. WannaCry
  31. 2. EternalRocks
  32. 3. Petya
  33. 4. NotPetya
  34. 5. Bad Rabbit
  35. 6. TrickBot
  36. 7. Emotet
  37. 8. Ryuk
  38. 9. GandCrab
  39. 10. SamSam
  40. 11. Smominru
  41. 12. RobbinHood
  42. 13. Dharma
  43.  
  44. The user is prompted to confirm their choice when enabling SMB1.
  45. Numeric options are provided for the user to choose (e.g., 1 for Yes, 2 for No).
  46.  
  47. Note: Changes made to SMB protocols may require system restart to take effect.
  48. """
  49.  
  50. import subprocess
  51.  
  52.  
  53. def verify_smb1_status():
  54.     """
  55.    Verify the status of SMB1 protocol.
  56.    This function checks whether SMB1 protocol is enabled or disabled on the system.
  57.    """
  58.     try:
  59.         # Check the value of the SMB1 registry key
  60.         result = subprocess.run(
  61.             [
  62.                 "reg",
  63.                 "query",
  64.                 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  65.                 "/v",
  66.                 "SMB1",
  67.             ],
  68.             capture_output=True,
  69.             text=True,
  70.         )
  71.         if "SMB1    REG_DWORD    0x1" in result.stdout:
  72.             print(
  73.                 "\nSMB1 is enabled. \n\t\t:: ⚠️ Warning ⚠ ::\n\n\t- Enabling SMB1 exposes your system to potential vulnerabilities such as EternalBlue.\n\n\t- Disable SMB1 with Option 6 or Remove the registry key with Option 7."
  74.             )
  75.         elif "SMB1    REG_DWORD    0x0" in result.stdout:
  76.             print("\nSMB1 is disabled.\n")
  77.         else:
  78.             print(
  79.                 "\nSMB1 status could not be determined or registry key not found on the system.\n"
  80.             )
  81.     except subprocess.CalledProcessError as e:
  82.         print("\nAn error occurred while checking SMB1 status:", e.stderr)
  83.  
  84.  
  85. def verify_smb2_status():
  86.     """
  87.    Verify the status of SMB2 protocol.
  88.    This function checks whether SMB2 protocol is enabled or disabled on the system.
  89.    """
  90.     result = subprocess.run(
  91.         [
  92.             "powershell",
  93.             "-Command",
  94.             '$SMB2Enabled = (Get-SmbServerConfiguration).EnableSMB2Protocol; if ($SMB2Enabled) { Write-Output "SMB2 is enabled" } else { Write-Output "SMB2 is disabled" }',
  95.         ],
  96.         capture_output=True,
  97.         text=True,
  98.     )
  99.     print("\n" + result.stdout.strip() + "\n")
  100.  
  101.  
  102. def verify_smb3_status():
  103.     """
  104.    Verify the status of SMB3 protocol.
  105.    This function checks whether SMB3 protocol is enabled or disabled on the system.
  106.    """
  107.     result = subprocess.run(
  108.         [
  109.             "powershell",
  110.             "-Command",
  111.             '$SMB3Enabled = (Get-SmbServerConfiguration).EnableSMB3Protocol; if ($SMB3Enabled) { Write-Output "SMB3 is enabled" } else { Write-Output "SMB3 is disabled" }',
  112.         ],
  113.         capture_output=True,
  114.         text=True,
  115.     )
  116.     print("\n" + result.stdout.strip() + "\n")
  117.  
  118.  
  119. def enable_smb1():
  120.     """
  121.    Enable the SMB1 protocol.
  122.    This function enables the SMB1 protocol on the system.
  123.    """
  124.     confirm = input(
  125.         "⚠️ WARNING: Enabling SMB1 is highly insecure and may expose your system to potential attacks.\nAre you sure you want to enable SMB1?\n1: Yes\n2: No\nEnter your choice: "
  126.     )
  127.     if confirm == "1":
  128.         try:
  129.             # Check if the registry key exists and is set to 1 (enabled)
  130.             check_registry = subprocess.run(
  131.                 [
  132.                     "reg",
  133.                     "query",
  134.                     "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  135.                     "/v",
  136.                     "SMB1",
  137.                 ],
  138.                 capture_output=True,
  139.                 text=True,
  140.             )
  141.             if "SMB1" in check_registry.stdout:
  142.                 if "0x1" in check_registry.stdout:  # Check if SMB1 is enabled
  143.                     print("\nSMB1 is already enabled.\n")
  144.                 else:
  145.                     # Enable SMB1
  146.                     subprocess.run(
  147.                         [
  148.                             "reg",
  149.                             "add",
  150.                             "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  151.                             "/v",
  152.                             "SMB1",
  153.                             "/t",
  154.                             "REG_DWORD",
  155.                             "/d",
  156.                             "1",
  157.                             "/f",
  158.                         ],
  159.                         capture_output=True,
  160.                         text=True,
  161.                         timeout=10,
  162.                     )
  163.                     print("\nSMB1 has been successfully enabled.\n")
  164.             else:
  165.                 # Create the registry key if it doesn't exist
  166.                 subprocess.run(
  167.                     [
  168.                         "reg",
  169.                         "add",
  170.                         "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  171.                         "/v",
  172.                         "SMB1",
  173.                         "/t",
  174.                         "REG_DWORD",
  175.                         "/d",
  176.                         "1",
  177.                         "/f",
  178.                     ],
  179.                     capture_output=True,
  180.                     text=True,
  181.                     timeout=10,
  182.                 )
  183.                 print("\nSMB1 has been successfully enabled.\n")
  184.         except subprocess.TimeoutExpired:
  185.             print("\nTimeout occurred while trying to enable SMB1.\n")
  186.     elif confirm == "2":
  187.         print("Aborted. SMB1 was not enabled.")
  188.     else:
  189.         print("Invalid choice. Please enter 1 or 2.")
  190.  
  191.  
  192. def enable_smb2():
  193.     """
  194.    Enable the SMB2 protocol.
  195.    This function enables the SMB2 protocol on the system.
  196.    """
  197.     result = subprocess.run(
  198.         ["powershell", "-Command", "(Get-SmbServerConfiguration).EnableSMB2Protocol"],
  199.         capture_output=True,
  200.         text=True,
  201.     )
  202.     if "True" in result.stdout:
  203.         print("\nSMB2 is already enabled.\n")
  204.     else:
  205.         result = subprocess.run(
  206.             [
  207.                 "powershell",
  208.                 "-Command",
  209.                 "Enable-WindowsOptionalFeature -Online -FeatureName SMB2Protocol",
  210.             ],
  211.             capture_output=True,
  212.             text=True,
  213.         )
  214.         if "The operation completed successfully." in result.stdout:
  215.             print("\nSMB2 has been successfully enabled.\n")
  216.         else:
  217.             print("\nFailed to enable SMB2.\n")
  218.  
  219.  
  220. def enable_smb3():
  221.     """
  222.    Enable the SMB3 protocol if supported.
  223.    """
  224.     # Check if SMB3 is supported on the system
  225.     result = subprocess.run(
  226.         [
  227.             "powershell",
  228.             "-Command",
  229.             "Get-WindowsOptionalFeature -Online -FeatureName SMB3Protocol",
  230.         ],
  231.         capture_output=True,
  232.         text=True,
  233.     )
  234.     if "State" in result.stdout and "Enabled" in result.stdout:
  235.         print("\nSMB3 is already enabled.\n")
  236.     elif "State" in result.stdout and "Disabled" in result.stdout:
  237.         # SMB3 is supported but disabled, attempt to enable it
  238.         result = subprocess.run(
  239.             [
  240.                 "powershell",
  241.                 "-Command",
  242.                 "Enable-WindowsOptionalFeature -Online -FeatureName SMB3Protocol",
  243.             ],
  244.             capture_output=True,
  245.             text=True,
  246.         )
  247.         if "The operation completed successfully." in result.stdout:
  248.             print("\nSMB3 has been successfully enabled.\n")
  249.         else:
  250.             print("\nFailed to enable SMB3.\n")
  251.     else:
  252.         # SMB3 is not supported on this system
  253.         print("\nSMB3 is not supported on this system.\n")
  254.  
  255.  
  256. def disable_smb1():
  257.     """
  258.    Disable the SMB1 protocol.
  259.    This function disables the SMB1 protocol on the system.
  260.    """
  261.     try:
  262.         # Check if the registry key exists and is set to 1
  263.         check_registry = subprocess.run(
  264.             [
  265.                 "reg",
  266.                 "query",
  267.                 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  268.                 "/v",
  269.                 "SMB1",
  270.             ],
  271.             capture_output=True,
  272.             text=True,
  273.         )
  274.         if "SMB1" in check_registry.stdout and "0x1" in check_registry.stdout:
  275.             result = subprocess.run(
  276.                 [
  277.                     "reg",
  278.                     "add",
  279.                     "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  280.                     "/v",
  281.                     "SMB1",
  282.                     "/t",
  283.                     "REG_DWORD",
  284.                     "/d",
  285.                     "0",
  286.                     "/f",
  287.                 ],
  288.                 capture_output=True,
  289.                 text=True,
  290.                 timeout=10,
  291.             )
  292.             print("\nSMB1 has been successfully disabled.\n")
  293.         else:
  294.             print(
  295.                 "\nThe SMB1 registry key does not exist or SMB1 is already disabled.\n"
  296.             )
  297.     except subprocess.TimeoutExpired:
  298.         print("\nTimeout occurred while trying to disable SMB1.\n")
  299.  
  300.  
  301. def delete_smb1_registry_key():
  302.     """
  303.    Delete the SMB1 protocol registry key.
  304.    This function removes the SMB1 registry key from the system.
  305.    """
  306.     try:
  307.         subprocess.run(
  308.             [
  309.                 "reg",
  310.                 "delete",
  311.                 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  312.                 "/v",
  313.                 "SMB1",
  314.                 "/f",
  315.             ],
  316.             check=True,
  317.         )
  318.         print("\nThe SMB1 registry key has been successfully deleted.\n")
  319.     except subprocess.CalledProcessError:
  320.         print("\nThe SMB1 registry key does not exist.\n")
  321.  
  322.  
  323. def create_smb1_registry_key():
  324.     """
  325.    Create the SMB1 registry key with value 0 to disable SMB1.
  326.    """
  327.     subprocess.run(
  328.         [
  329.             "reg",
  330.             "add",
  331.             "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
  332.             "/v",
  333.             "SMB1",
  334.             "/t",
  335.             "REG_DWORD",
  336.             "/d",
  337.             "0",
  338.             "/f",
  339.         ]
  340.     )
  341.  
  342.  
  343. def main():
  344.     while True:
  345.         print(":: [SMB PROTOCOL OPTIONS] ::\n")
  346.         print("0: Exit")
  347.         print("1: Verify SMB1 status")
  348.         print("2: Verify SMB2 status")
  349.         print("3: Verify SMB3 status")
  350.         print("4: Enable SMB2")
  351.         print("5: Enable SMB3")
  352.         print("6: Disable SMB1")
  353.         print("7: Delete SMB1 Registry Key")
  354.         print("\n:: ⚠️ WARNING! ⚠️ ::\n")
  355.         print("8: Create SMB1 Registry Key (Disabled SMB1)")
  356.         print("9: Enable SMB1  :: ⚠️ WARNING! ⚠️ ::\n")
  357.  
  358.         choice = input("Enter your choice: ")
  359.  
  360.         if choice == "1":
  361.             verify_smb1_status()
  362.         elif choice == "2":
  363.             verify_smb2_status()
  364.         elif choice == "3":
  365.             verify_smb3_status()
  366.         elif choice == "4":
  367.             enable_smb2()
  368.         elif choice == "5":
  369.             enable_smb3()
  370.         elif choice == "6":
  371.             disable_smb1()
  372.         elif choice == "7":
  373.             delete_smb1_registry_key()
  374.         elif choice == "8":
  375.             create_smb1_registry_key()
  376.         elif choice == "9":
  377.             enable_smb1()
  378.         elif choice == "0":
  379.             print("\nExiting Program...\nGoodBye!\n.")
  380.             break
  381.         else:
  382.             print("\nInvalid choice. Please enter a valid option.\n")
  383.  
  384.         input("\nPress Enter to continue...\n")
  385.  
  386.  
  387. if __name__ == "__main__":
  388.     main()
  389.  
  390.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement