Initial setup_old 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. subprocess.run(["sudo", "hostnamectl", "set-hostname", "newhostname.peo.local"], check=True)
  8. # Install the necessary packages
  9. subprocess.run(["sudo", "apt-get", "install", "-y", "realmd", "libnss-sss", "libpam-sss", "sssd", "sssd-tools", "adcli", "samba-common-bin", "oddjob", "oddjob-mkhomedir", "packagekit", "python-ldap", "net-tools", "network-manager", "policycoreutils"], check=True)
  10. # Join the domain using realm
  11. subprocess.run(["sudo", "realm", "join", "--user", f"{admin_username}%'{admin_password}'", domain_name], check=True)
  12. # Copy the Falcon_senson.deb file from the remote server to the home directory
  13. subprocess.run(["scp", "anonymous@ptiwa001:falcon-sensor_6.46.0-14306.deb", "~/"], check=True)
  14. # Copy the ninja.deb file from the remote server to the home directory
  15. subprocess.run(["scp", "anonymous@ptiwa001:ninja-agent.deb", "~/"], check=True)
  16. # Verify that the join was successful
  17. result = subprocess.run(["sudo", "net", "ads", "testjoin"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  18. if result.returncode == 0:
  19. print("Successfully joined the domain")
  20. else:
  21. print("Failed to join the domain.")
  22. # Activate creation of homedir
  23. 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)
  24. subprocess.run(["sudo", "pam-auth-update", "--package", "--enable", "mkhomedir"], check=True)
  25. # Set fully qualified domain names to false
  26. subprocess.run(["sudo", "sed", "-i", "s/use_fully_qualified_names = True/use_fully_qualified_names = False/g", "/etc/sssd/sssd.conf"], check=True)
  27. subprocess.run(["sudo", "systemctl", "restart", "sssd.service"], check=True)
  28. # Install Falcon_sensor.deb file
  29. subprocess.run(["sudo", "dpkg", "-i", "~/file.deb"], check=True)
  30. # Run additional Crowdstrike commands
  31. subprocess.run(["sudo", "/opt/CrowdStrike/falconctl", "-s", "--cid=D0511099B3FF494D8B87F48C4AB90201-56"], check=True)
  32. subprocess.run(["sudo", "systemctl", "start", "falcon-sensor"], check=True)
  33. result = subprocess.run(["sudo", "systemctl", "status", "falcon-sensor"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  34. if result.returncode == 0:
  35. print("falcon-sensor service is running.")
  36. else:
  37. print("Failed to join the domain.")
  38. # Install Ninja.deb file
  39. subprocess.run(["sudo", "dpkg", "-i", "~/file.deb"], check=True)
  40. # Run additional Ninja commands
  41. subprocess.run(["sudo", "systemctl", "start", "ninjarmm-agent.service"], check=True)
  42. result = subprocess.run(["sudo", "systemctl", "status", "ninjarmm-agent.service"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  43. if result.returncode == 0:
  44. print("ninjarmm-agent service is running.")
  45. else:
  46. print("ninjarmm-agent service is not running.")
  47. #Deny all permissions
  48. subprocess.run(["sudo", "realm", "deny", "--all"], check=True)
  49. #Permit access for Domain Admins group
  50. subprocess.run(["sudo", "realm", "permit", "-g", "Domain Admins"], check=True)
  51. #Permit access for Access - Admin - All Servers group
  52. subprocess.run(["sudo", "realm", "permit", "-g", "Access - Admin - All Servers"], check=True)
  53. #get hostname
  54. hostname = subprocess.run(["hostname"], stdout=subprocess.PIPE, check=True).stdout.decode().strip()
  55. #Permit access for Access - Admin - hostname group
  56. subprocess.run(["sudo", "realm", "permit", "-g", f"Access - Admin - {hostname}"], check=True)
  57. #get hostname
  58. hostname = subprocess.run(["hostname"], stdout=subprocess.PIPE, check=True).stdout.decode().strip()
  59. #Edit the file using visudo
  60. subprocess.run(["sudo", "sh", "-c", "echo '%Domain\\ Admins ALL=(ALL:ALL) ALL' >> /etc/sudoers.d/LocalAdmins"], check=True)
  61. subprocess.run(["sudo", "sh", "-c", "echo '%Access\\ -\\ Admin\\ -\\ All\\ Servers ALL=(ALL) ALL' >> /etc/sudoers.d/LocalAdmins"], check=True)
  62. subprocess.run(["sudo", "sh", "-c", f"echo '%Access\\ -\\ Admin\\ -\\ {hostname} ALL=(ALL) ALL' >> /etc/sudoers.d/LocalAdmins"], check=True)
  63. print("Join to Domain, installation of Falson Sensor and Ninja Agent are complete")
  64. print(result.stderr.decode())