Initial setup 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import subprocess
  2. # Set the domain name and administrator credentials
  3. domain_name = "example.com"
  4. admin_username = "administrator"
  5. admin_password = "password"
  6. # Set Hostname
  7. def set_hostname():
  8. hostname = subprocess.run(["hostname"], stdout=subprocess.PIPE, check=True).stdout.decode().strip()
  9. hostname = hostname.split(".")[0]
  10. subprocess.run(["sudo", "hostnamectl", "set-hostname", hostname], check=True)
  11. # Install the necessary packages
  12. def install_packages():
  13. packages = ["realmd", "libnss-sss", "libpam-sss", "sssd", "sssd-tools", "adcli", "samba-common-bin", "oddjob", "oddjob-mkhomedir", "packagekit", "python-ldap", "net-tools", "network-manager", "policycoreutils"]
  14. subprocess.run(["sudo", "apt-get", "install", "-y", *packages], check=True)
  15. # Copy the files from the remote server
  16. def copy_files(file_name):
  17. subprocess.run(["scp", "anonymous@ptiwa001:"+file_name, "~/"], check=True)
  18. # Join the domain using realm
  19. def join_domain():
  20. subprocess.run(["sudo", "realm", "join", "--user", f"{admin_username}%'{admin_password}'", domain_name], check=True)
  21. # Verify that the join was successful
  22. result = subprocess.run(["sudo", "net", "ads", "testjoin"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  23. if result.returncode == 0:
  24. print("Successfully joined the domain")
  25. else:
  26. print("Failed to join the domain. Error: ",result.stderr.decode())
  27. # Activate creation of homedir
  28. def activate_mkhomedir():
  29. subprocess.run(["sudo", "bash", "-c", "cat > /usr/share/pam-configs/mkhomedir <<EOF\nName: activate mkhomedir\nDefault: yes\nPriority: 900\nSession-Type: Additional\nSession:\n\tRequired\tpam_mkhomedir.so umask=0022 skel=/etc/skel\nEOF"], check=True)
  30. subprocess.run(["sudo", "pam-auth-update", "--package", "--enable", "mkhomedir"], check=True)
  31. # Set fully qualified domain names to false
  32. subprocess.run(["sudo", "sed", "-i", "s/use_fully_qualified_names = True/use_fully_qualified_names = False/g", "/etc/sssd/sssd.conf"], check=True)
  33. subprocess.run(["sudo", "systemctl", "restart", "sssd.service"], check=True)
  34. # Install Falcon_sensor.deb file
  35. subprocess.run(["sudo", "dpkg", "-i", "~/falcon-sensor_6.46.0-14306.deb"], check=True)
  36. # Run additional Crowdstrike commands
  37. subprocess.run(["sudo", "/opt/CrowdStrike/falconctl", "-s", "--cid=D0511099B3FF494D8B87F48C4AB90201-56"], check=True)
  38. subprocess.run(["sudo", "systemctl", "start", "falcon-sensor"], check=True)
  39. result = subprocess.run(["sudo", "systemctl", "status", "falcon-sensor"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  40. if result.returncode == 0:
  41. print("falcon-sensor service is running.")
  42. else:
  43. print("falcon-sensor service is not running. Error: ", result.stderr.decode())
  44. # Install Ninja.deb file
  45. subprocess.run(["sudo", "dpkg", "-i", "~/ninja-agent.deb"], check=True)
  46. # Run additional Ninja commands
  47. subprocess.run(["sudo", "systemctl", "start", "ninjarmm-agent.service"], check=True)
  48. result = subprocess.run(["sudo", "systemctl", "status", "ninjarmm-agent.service"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  49. if result.returncode == 0:
  50. print("ninjarmm-agent service is running.")
  51. else:
  52. print("ninjarmm-agent service is not running. Error: ", result.stderr.decode())
  53. # Permit domain groups
  54. hostname = subprocess.run(["hostname"], stdout=subprocess.PIPE, check=True).stdout.decode().strip()
  55. group_name = f"Access - Admin - {hostname}"
  56. subprocess.run(["sudo", "realm", "deny", "--all"], check=True)
  57. subprocess.run(["sudo", "realm", "permit", "-g", "Domain Admins"], check=True)
  58. subprocess.run(["sudo", "realm", "permit", "-g", "Access - Admin - All Servers"], check=True)
  59. subprocess.run(["sudo", "realm", "permit", "-g", group_name], check=True)
  60. # Edit the sudoers file
  61. subprocess.run(["sudo", "visudo", "-f", "/etc/sudoers.d/LocalAdmins"], check=True)
  62. subprocess.run(["sudo", "bash", "-c", f"echo '%Domain\ Admins ALL=(ALL:ALL) ALL' >> /etc/sudoers.d/LocalAdmins"], check=True)
  63. subprocess.run(["sudo", "bash", "-c", f"echo '%Access\ -\ Admin\ -\ All\ Servers ALL=(ALL) ALL' >> /etc/sudoers.d/LocalAdmins"], check=True)
  64. subprocess.run(["sudo", "bash", "-c", f"echo '%{group_name} ALL=(ALL) ALL' >> /etc/sudoers.d/LocalAdmins"], check=True)