Advertisement
Guest User

Setup-hMailServer

a guest
Dec 13th, 2017
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. # Configure and install hMailServer for SMTP and IMAP
  2. # Created for ISTS 16
  3. # Author: Micah Martin (mjm5097@rit.edu)
  4. #
  5. # To install hMailServer run the installer as follows:
  6. #
  7. # .\hMailServer-x.x.x-Bxxx.exe /verysilent
  8. #
  9. # To view the COM API configurations, check out the documentation:
  10. #
  11. # https://www.hmailserver.com/documentation/latest/?page=com_objects
  12. #
  13. # Configure hmail with a new user to use SMTP and IMAP
  14. #
  15.  
  16. param (
  17. [string]$domainName = "whiteteam.ists",
  18. [string]$newUser = "whiteteam",
  19. [string]$defaultPassword = "Changeme-2018"
  20. )
  21.  
  22. ##########################################
  23. ##
  24. ## Install hMailServer and connect
  25. ##
  26. ##########################################
  27.  
  28. function Install-hMail(){
  29. Write-Host "[*] Installing hMailServer"
  30. Try
  31. {
  32. $url = "https://www.hmailserver.com/download_file?downloadid=256"
  33. Invoke-WebRequest -Uri $url -OutFile "hMailServer.exe"
  34. .\hMailServer.exe /verysilent
  35. }
  36. Catch
  37. {
  38. Write-Error "[!] Install failed!"
  39. }
  40. }
  41.  
  42.  
  43. # Get the COM Object for hMailServer, If this fails, install hMail
  44. Try
  45. {
  46. $hMailCom = New-Object -ComObject 'hMailServer.Application'
  47. }
  48. Catch
  49. {
  50. Install-hMail
  51. $hMailCom = New-Object -ComObject 'hMailServer.Application'
  52. }
  53.  
  54. # Print info
  55. Write-Host "[*] Configuring hMail with following settings:"
  56. Write-Host " - Domain Name: $domainName"
  57. Write-Host " - Username: $newUser"
  58. Write-Host " - Default Password: $defaultPassword"
  59. Write-Host ""
  60.  
  61. # Login. After a fresh install, the password will be blank. Otherwise it is defaultPassword
  62. Write-Host "[*] Logging in..."
  63. $hMail = $hMailCom.Authenticate("Administrator","")
  64. if ($hMail -eq $null) {
  65. $hMail = $hMailCom.Authenticate("Administrator",$defaultPassword)
  66. if ($hMail -ne $null) {
  67. Write-Host "[+] Logged in with default password"
  68. } else {
  69. Write-Error "[!] Cannot Log in"
  70. Throw
  71. }
  72. } else {
  73. Write-Host "[+] Logged in with blank password"
  74. }
  75.  
  76. ##########################################
  77. ##
  78. ## Add domains and users
  79. ##
  80. ##########################################
  81.  
  82. $domains = $hMailCom.Domains
  83. Try
  84. {
  85. # Check if the domain already exists
  86. $domain = $domains.ItemByName($domainName)
  87. # Enable the domain
  88. $domain.Active = $true
  89. $domain.Save()
  90. Write-Host "[+] Domain Exists. Moving on"
  91. }
  92. Catch
  93. {
  94. # Create a new domain name
  95. $domain = $domains.Add()
  96. # Set the name
  97. $domain.Name = $domainName
  98. # Enable the domain
  99. $domain.Active = $true
  100. $domain.Save()
  101. Write-Host "[*] Domain does not exists. Creating domain"
  102. }
  103.  
  104. # Get the accounts of the domain
  105. Try
  106. {
  107. $account = $accounts.ItemByAddress("$newUser@$domainName")
  108. # Set the password and activate the account
  109. $account.Password = $defaultPassword
  110. $account.Active = $true
  111. $account.Save()
  112. Write-Host "[+] $newUser@$domainName exists already."
  113. }
  114. Catch
  115. {
  116. $account = $accounts.Add()
  117. # Set the email address, password, and activate the account
  118. $account.Address = "$newUser@$domainName"
  119. $account.Password = $defaultPassword
  120. $account.Active = $true
  121. $account.Save()
  122. Write-Host "[*] $newUser@$domainName created with default password"
  123. }
  124.  
  125. ##########################################
  126. ##
  127. ## Configure key settings
  128. ##
  129. ##########################################
  130.  
  131. $settings = $hMailCom.Settings
  132.  
  133. Write-host "[*] Resetting Admin password"
  134. $settings.SetAdministratorPassword($defaultPassword)
  135.  
  136. Write-Host '[*] Enabling services'
  137. $settings.ServiceIMAP = $true
  138. $settings.ServicePOP3 = $false
  139. $settings.ServiceSMTP = $true
  140.  
  141. Write-Host "[*] Configuring services"
  142. $settings.AllowSMTPAuthPlain = $true
  143. $settings.HostName = $domainName
  144. $settings.DefaultDomain = $domainName
  145. $settings.IMAPIdleEnabled = $true
  146. $settings.AutoBanOnLogonFailure = $false
  147.  
  148. $log = $settings.Logging
  149. Write-Host "[*] Enabling logging"
  150. $log.Enabled = $true
  151. $log.AWStatsEnabled = $true
  152. $log.LogApplication = $true
  153. $log.LogIMAP = $true
  154. $log.LogSMTP = $true
  155. $log.LogTCPIP = $true
  156. $log.MaskPasswordsInLog = $false
  157.  
  158. Write-Host "[+] Complete. hMail installed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement