Make deploy_windows.ps1.sh download the configuration, avoiding need for duplicate git clone.

This commit is contained in:
Daniel Goldberg 2020-01-04 23:27:26 +02:00
parent 43adea0728
commit cb8d2eb0ef
1 changed files with 90 additions and 87 deletions

View File

@ -7,26 +7,29 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
# Import the config variables # Import the config variables
$config_filename = "config.ps1"
$config_url = "https://raw.githubusercontent.com/guardicore/monkey/" + $branch + "/deployment_scripts/config.ps1"
$webClient.DownloadFile($config_url, $config_filename)
. ./config.ps1 . ./config.ps1
"Config variables from config.ps1 imported" "Config variables from config.ps1 imported"
# If we want monkey in current dir we need to create an empty folder for source files # If we want monkey in current dir we need to create an empty folder for source files
if ((Join-Path $monkey_home '') -eq (Join-Path (Get-Item -Path ".\").FullName '')) if ((Join-Path $monkey_home '') -eq (Join-Path (Get-Item -Path ".\").FullName ''))
{ {
$monkey_home = Join-Path -Path $monkey_home -ChildPath $MONKEY_FOLDER_NAME $monkey_home = Join-Path -Path $monkey_home -ChildPath $MONKEY_FOLDER_NAME
} }
# We check if git is installed # We check if git is installed
try try
{ {
git | Out-Null -ErrorAction Stop git | Out-Null -ErrorAction Stop
"Git requirement satisfied" "Git requirement satisfied"
} }
catch [System.Management.Automation.CommandNotFoundException] catch [System.Management.Automation.CommandNotFoundException]
{ {
"Please install git before running this script or add it to path and restart cmd" "Please install git before running this script or add it to path and restart cmd"
return return
} }
# Download the monkey # Download the monkey
@ -34,50 +37,50 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
$binDir = (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "\bin") $binDir = (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "\bin")
if ($output -like "*already exists and is not an empty directory.*") if ($output -like "*already exists and is not an empty directory.*")
{ {
"Assuming you already have the source directory. If not, make sure to set an empty directory as monkey's home directory." "Assuming you already have the source directory. If not, make sure to set an empty directory as monkey's home directory."
} }
elseif ($output -like "fatal:*") elseif ($output -like "fatal:*")
{ {
"Error while cloning monkey from the repository:" "Error while cloning monkey from the repository:"
$output $output
return return
} }
else else
{ {
"Monkey cloned from the repository" "Monkey cloned from the repository"
# Create bin directory # Create bin directory
New-Item -ItemType directory -path $binDir New-Item -ItemType directory -path $binDir
"Bin directory added" "Bin directory added"
} }
# We check if python is installed # We check if python is installed
try try
{ {
$version = cmd.exe /c '"python" --version 2>&1' $version = cmd.exe /c '"python" --version 2>&1'
if ($version -like 'Python 3.*') if ($version -like 'Python 3.*')
{ {
"Python 3.* was found, installing dependencies" "Python 3.* was found, installing dependencies"
} }
else else
{ {
throw System.Management.Automation.CommandNotFoundException throw System.Management.Automation.CommandNotFoundException
} }
} }
catch [System.Management.Automation.CommandNotFoundException] catch [System.Management.Automation.CommandNotFoundException]
{ {
"Downloading python 3 ..." "Downloading python 3 ..."
"Select 'add to PATH' when installing" "Select 'add to PATH' when installing"
$webClient.DownloadFile($PYTHON_URL, $TEMP_PYTHON_INSTALLER) $webClient.DownloadFile($PYTHON_URL, $TEMP_PYTHON_INSTALLER)
Start-Process -Wait $TEMP_PYTHON_INSTALLER -ErrorAction Stop Start-Process -Wait $TEMP_PYTHON_INSTALLER -ErrorAction Stop
$env: Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User") $env: Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
Remove-Item $TEMP_PYTHON_INSTALLER Remove-Item $TEMP_PYTHON_INSTALLER
# Check if installed correctly # Check if installed correctly
$version = cmd.exe /c '"python" --version 2>&1' $version = cmd.exe /c '"python" --version 2>&1'
if ($version -like '* is not recognized*') if ($version -like '* is not recognized*')
{ {
"Python is not found in PATH. Add it to PATH and relaunch the script." "Python is not found in PATH. Add it to PATH and relaunch the script."
return return
} }
} }
"Upgrading pip..." "Upgrading pip..."
@ -85,8 +88,8 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
$output $output
if ($output -like '*No module named pip*') if ($output -like '*No module named pip*')
{ {
"Make sure pip module is installed and re-run this script." "Make sure pip module is installed and re-run this script."
return return
} }
"Installing python packages for island" "Installing python packages for island"
@ -100,30 +103,30 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
$user_python_dir = Join-Path (Split-Path $user_python_dir) -ChildPath "\Scripts" $user_python_dir = Join-Path (Split-Path $user_python_dir) -ChildPath "\Scripts"
if (!($ENV: PATH | Select-String -SimpleMatch $user_python_dir)) if (!($ENV: PATH | Select-String -SimpleMatch $user_python_dir))
{ {
"Adding python scripts path to user's env" "Adding python scripts path to user's env"
$env: Path += ";" + $user_python_dir $env: Path += ";" + $user_python_dir
[Environment]::SetEnvironmentVariable("Path", $env:Path, "User") [Environment]::SetEnvironmentVariable("Path", $env:Path, "User")
} }
# Download mongodb # Download mongodb
if (!(Test-Path -Path (Join-Path -Path $binDir -ChildPath "mongodb"))) if (!(Test-Path -Path (Join-Path -Path $binDir -ChildPath "mongodb")))
{ {
"Downloading mongodb ..." "Downloading mongodb ..."
$webClient.DownloadFile($MONGODB_URL, $TEMP_MONGODB_ZIP) $webClient.DownloadFile($MONGODB_URL, $TEMP_MONGODB_ZIP)
"Unzipping mongodb" "Unzipping mongodb"
Expand-Archive $TEMP_MONGODB_ZIP -DestinationPath $binDir Expand-Archive $TEMP_MONGODB_ZIP -DestinationPath $binDir
# Get unzipped folder's name # Get unzipped folder's name
$mongodb_folder = Get-ChildItem -Path $binDir | Where-Object -FilterScript { $mongodb_folder = Get-ChildItem -Path $binDir | Where-Object -FilterScript {
($_.Name -like "mongodb*") ($_.Name -like "mongodb*")
} | Select-Object -ExpandProperty Name } | Select-Object -ExpandProperty Name
# Move all files from extracted folder to mongodb folder # Move all files from extracted folder to mongodb folder
New-Item -ItemType directory -Path (Join-Path -Path $binDir -ChildPath "mongodb") New-Item -ItemType directory -Path (Join-Path -Path $binDir -ChildPath "mongodb")
New-Item -ItemType directory -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "db") New-Item -ItemType directory -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "db")
"Moving extracted files" "Moving extracted files"
Move-Item -Path (Join-Path -Path $binDir -ChildPath $mongodb_folder | Join-Path -ChildPath "\bin\*") -Destination (Join-Path -Path $binDir -ChildPath "mongodb\") Move-Item -Path (Join-Path -Path $binDir -ChildPath $mongodb_folder | Join-Path -ChildPath "\bin\*") -Destination (Join-Path -Path $binDir -ChildPath "mongodb\")
"Removing zip file" "Removing zip file"
Remove-Item $TEMP_MONGODB_ZIP Remove-Item $TEMP_MONGODB_ZIP
Remove-Item (Join-Path -Path $binDir -ChildPath $mongodb_folder) -Recurse Remove-Item (Join-Path -Path $binDir -ChildPath $mongodb_folder) -Recurse
} }
# Download OpenSSL # Download OpenSSL
@ -159,23 +162,23 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
"Installing npm" "Installing npm"
try try
{ {
$version = cmd.exe /c '"npm" --version 2>&1' $version = cmd.exe /c '"npm" --version 2>&1'
if ($version -like "*is not recognized*") if ($version -like "*is not recognized*")
{ {
throw System.Management.Automation.CommandNotFoundException throw System.Management.Automation.CommandNotFoundException
} }
else else
{ {
"Npm already installed" "Npm already installed"
} }
} }
catch [System.Management.Automation.CommandNotFoundException] catch [System.Management.Automation.CommandNotFoundException]
{ {
"Downloading npm ..." "Downloading npm ..."
$webClient.DownloadFile($NPM_URL, $TEMP_NPM_INSTALLER) $webClient.DownloadFile($NPM_URL, $TEMP_NPM_INSTALLER)
Start-Process -Wait $TEMP_NPM_INSTALLER Start-Process -Wait $TEMP_NPM_INSTALLER
$env: Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") $env: Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
Remove-Item $TEMP_NPM_INSTALLER Remove-Item $TEMP_NPM_INSTALLER
} }
"Updating npm" "Updating npm"
@ -197,29 +200,29 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
# Download upx # Download upx
if (!(Test-Path -Path (Join-Path -Path $binDir -ChildPath "upx.exe"))) if (!(Test-Path -Path (Join-Path -Path $binDir -ChildPath "upx.exe")))
{ {
"Downloading upx ..." "Downloading upx ..."
$webClient.DownloadFile($UPX_URL, $TEMP_UPX_ZIP) $webClient.DownloadFile($UPX_URL, $TEMP_UPX_ZIP)
"Unzipping upx" "Unzipping upx"
Expand-Archive $TEMP_UPX_ZIP -DestinationPath $binDir -ErrorAction SilentlyContinue Expand-Archive $TEMP_UPX_ZIP -DestinationPath $binDir -ErrorAction SilentlyContinue
Move-Item -Path (Join-Path -Path $binDir -ChildPath $UPX_FOLDER | Join-Path -ChildPath "upx.exe") -Destination $binDir Move-Item -Path (Join-Path -Path $binDir -ChildPath $UPX_FOLDER | Join-Path -ChildPath "upx.exe") -Destination $binDir
# Remove unnecessary files # Remove unnecessary files
Remove-Item -Recurse -Force (Join-Path -Path $binDir -ChildPath $UPX_FOLDER) Remove-Item -Recurse -Force (Join-Path -Path $binDir -ChildPath $UPX_FOLDER)
"Removing zip file" "Removing zip file"
Remove-Item $TEMP_UPX_ZIP Remove-Item $TEMP_UPX_ZIP
} }
# Download mimikatz binaries # Download mimikatz binaries
$mk32_path = Join-Path -Path $binDir -ChildPath $MK32_DLL $mk32_path = Join-Path -Path $binDir -ChildPath $MK32_DLL
if (!(Test-Path -Path $mk32_path)) if (!(Test-Path -Path $mk32_path))
{ {
"Downloading mimikatz 32 binary" "Downloading mimikatz 32 binary"
$webClient.DownloadFile($MK32_DLL_URL, $mk32_path) $webClient.DownloadFile($MK32_DLL_URL, $mk32_path)
} }
$mk64_path = Join-Path -Path $binDir -ChildPath $MK64_DLL $mk64_path = Join-Path -Path $binDir -ChildPath $MK64_DLL
if (!(Test-Path -Path $mk64_path)) if (!(Test-Path -Path $mk64_path))
{ {
"Downloading mimikatz 64 binary" "Downloading mimikatz 64 binary"
$webClient.DownloadFile($MK64_DLL_URL, $mk64_path) $webClient.DownloadFile($MK64_DLL_URL, $mk64_path)
} }
# Download sambacry binaries # Download sambacry binaries
@ -227,14 +230,14 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
$samba32_path = Join-Path -Path $samba_path -ChildPath $SAMBA_32_BINARY_NAME $samba32_path = Join-Path -Path $samba_path -ChildPath $SAMBA_32_BINARY_NAME
if (!(Test-Path -Path $samba32_path)) if (!(Test-Path -Path $samba32_path))
{ {
"Downloading sambacry 32 binary" "Downloading sambacry 32 binary"
$webClient.DownloadFile($SAMBA_32_BINARY_URL, $samba32_path) $webClient.DownloadFile($SAMBA_32_BINARY_URL, $samba32_path)
} }
$samba64_path = Join-Path -Path $samba_path -ChildPath $SAMBA_64_BINARY_NAME $samba64_path = Join-Path -Path $samba_path -ChildPath $SAMBA_64_BINARY_NAME
if (!(Test-Path -Path $samba64_path)) if (!(Test-Path -Path $samba64_path))
{ {
"Downloading sambacry 64 binary" "Downloading sambacry 64 binary"
$webClient.DownloadFile($SAMBA_64_BINARY_URL, $samba64_path) $webClient.DownloadFile($SAMBA_64_BINARY_URL, $samba64_path)
} }
"Script finished" "Script finished"