2020-02-03 18:18:02 +08:00
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $false, Position = 0)]
|
|
|
|
[String] $monkey_home = (Get-Item -Path ".\").FullName,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $false, Position = 1)]
|
|
|
|
[System.String]
|
2020-02-10 19:48:12 +08:00
|
|
|
$branch = "develop",
|
|
|
|
[Parameter(Mandatory = $false, Position = 2)]
|
|
|
|
[Bool]
|
|
|
|
$agents = $true
|
2020-02-03 18:18:02 +08:00
|
|
|
)
|
2021-04-06 23:01:22 +08:00
|
|
|
|
2021-04-07 19:39:36 +08:00
|
|
|
function Configure-precommit([String] $git_repo_dir)
|
2021-04-06 23:01:22 +08:00
|
|
|
{
|
|
|
|
Write-Output "Installing pre-commit and setting up pre-commit hook"
|
2021-04-07 19:39:36 +08:00
|
|
|
Push-Location $git_repo_dir
|
2021-04-06 23:01:22 +08:00
|
|
|
python -m pip install pre-commit
|
|
|
|
if ($LastExitCode) {
|
|
|
|
exit
|
|
|
|
}
|
2021-04-21 00:11:46 +08:00
|
|
|
pre-commit install -t pre-commit -t pre-push
|
2021-04-06 23:01:22 +08:00
|
|
|
if ($LastExitCode) {
|
|
|
|
exit
|
|
|
|
}
|
2021-04-07 19:39:36 +08:00
|
|
|
Pop-Location
|
2021-04-27 20:20:38 +08:00
|
|
|
|
|
|
|
# Set env variable to skip Swimm verification during pre-commit, Windows not supported yet
|
2021-04-28 16:02:30 +08:00
|
|
|
$skipValue = [System.Environment]::GetEnvironmentVariable('SKIP', [System.EnvironmentVariableTarget]::User)
|
|
|
|
if ($skipValue) { # if `SKIP` is not empty
|
|
|
|
if (-Not ($skipValue -split ',' -contains 'swimm-verify')) { # if `SKIP` doesn't already have "swimm-verify"
|
|
|
|
[System.Environment]::SetEnvironmentVariable('SKIP', $env:SKIP + ',swimm-verify', [System.EnvironmentVariableTarget]::User)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
[System.Environment]::SetEnvironmentVariable('SKIP', 'swimm-verify', [System.EnvironmentVariableTarget]::User)
|
|
|
|
}
|
2021-04-27 20:20:38 +08:00
|
|
|
|
2021-04-06 23:01:22 +08:00
|
|
|
Write-Output "Pre-commit successfully installed"
|
|
|
|
}
|
|
|
|
|
2020-01-05 05:27:19 +08:00
|
|
|
function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName, [String] $branch = "develop")
|
|
|
|
{
|
2020-02-03 18:18:02 +08:00
|
|
|
Write-Output "Downloading to $monkey_home"
|
|
|
|
Write-Output "Branch $branch"
|
2020-01-05 05:27:19 +08:00
|
|
|
# Set variables for script execution
|
|
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
|
|
$webClient = New-Object System.Net.WebClient
|
|
|
|
|
|
|
|
|
2018-12-10 19:08:59 +08:00
|
|
|
# Import the config variables
|
2020-02-03 18:18:02 +08:00
|
|
|
$config_filename = New-TemporaryFile
|
2021-11-18 18:05:47 +08:00
|
|
|
$config_filename = "$PsScriptRoot\config.ps1"
|
2020-01-05 05:27:26 +08:00
|
|
|
$config_url = "https://raw.githubusercontent.com/guardicore/monkey/" + $branch + "/deployment_scripts/config.ps1"
|
|
|
|
$webClient.DownloadFile($config_url, $config_filename)
|
2018-12-10 19:08:59 +08:00
|
|
|
. ./config.ps1
|
|
|
|
"Config variables from config.ps1 imported"
|
2020-02-09 05:25:10 +08:00
|
|
|
Remove-Item $config_filename
|
2020-02-03 17:16:00 +08:00
|
|
|
|
2018-12-10 19:08:59 +08:00
|
|
|
|
|
|
|
# If we want monkey in current dir we need to create an empty folder for source files
|
2020-01-05 05:27:19 +08:00
|
|
|
if ((Join-Path $monkey_home '') -eq (Join-Path (Get-Item -Path ".\").FullName ''))
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
$monkey_home = Join-Path -Path $monkey_home -ChildPath $MONKEY_FOLDER_NAME
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# We check if git is installed
|
|
|
|
try
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
git | Out-Null -ErrorAction Stop
|
|
|
|
"Git requirement satisfied"
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
catch [System.Management.Automation.CommandNotFoundException]
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Please install git before running this script or add it to path and restart cmd"
|
|
|
|
return
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Download the monkey
|
2020-03-25 19:12:40 +08:00
|
|
|
$command = "git clone --single-branch --recurse-submodules -b $branch $MONKEY_GIT_URL $monkey_home 2>&1"
|
2020-02-03 18:18:02 +08:00
|
|
|
Write-Output $command
|
|
|
|
$output = cmd.exe /c $command
|
2018-12-10 19:08:59 +08:00
|
|
|
$binDir = (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "\bin")
|
2020-01-05 05:27:19 +08:00
|
|
|
if ($output -like "*already exists and is not an empty directory.*")
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Assuming you already have the source directory. If not, make sure to set an empty directory as monkey's home directory."
|
2020-01-05 05:27:19 +08:00
|
|
|
}
|
|
|
|
elseif ($output -like "fatal:*")
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Error while cloning monkey from the repository:"
|
|
|
|
$output
|
|
|
|
return
|
2020-01-05 05:27:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Monkey cloned from the repository"
|
|
|
|
# Create bin directory
|
|
|
|
New-Item -ItemType directory -path $binDir
|
|
|
|
"Bin directory added"
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
2019-02-04 16:35:39 +08:00
|
|
|
|
2018-12-10 19:08:59 +08:00
|
|
|
# We check if python is installed
|
|
|
|
try
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
$version = cmd.exe /c '"python" --version 2>&1'
|
|
|
|
if ($version -like 'Python 3.*')
|
|
|
|
{
|
|
|
|
"Python 3.* was found, installing dependencies"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw System.Management.Automation.CommandNotFoundException
|
|
|
|
}
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
catch [System.Management.Automation.CommandNotFoundException]
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Downloading python 3 ..."
|
|
|
|
"Select 'add to PATH' when installing"
|
|
|
|
$webClient.DownloadFile($PYTHON_URL, $TEMP_PYTHON_INSTALLER)
|
|
|
|
Start-Process -Wait $TEMP_PYTHON_INSTALLER -ErrorAction Stop
|
2020-02-03 15:59:19 +08:00
|
|
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
|
2020-01-05 05:27:26 +08:00
|
|
|
Remove-Item $TEMP_PYTHON_INSTALLER
|
|
|
|
# Check if installed correctly
|
|
|
|
$version = cmd.exe /c '"python" --version 2>&1'
|
|
|
|
if ($version -like '* is not recognized*')
|
|
|
|
{
|
|
|
|
"Python is not found in PATH. Add it to PATH and relaunch the script."
|
|
|
|
return
|
|
|
|
}
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
"Upgrading pip..."
|
|
|
|
$output = cmd.exe /c 'python -m pip install --user --upgrade pip 2>&1'
|
|
|
|
$output
|
2020-01-05 05:27:19 +08:00
|
|
|
if ($output -like '*No module named pip*')
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Make sure pip module is installed and re-run this script."
|
|
|
|
return
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
2019-10-02 15:15:53 +08:00
|
|
|
|
2021-04-15 16:11:11 +08:00
|
|
|
"Installing pipx"
|
|
|
|
pip install --user -U pipx
|
|
|
|
pipx ensurepath
|
|
|
|
pipx install pipenv
|
|
|
|
|
2019-10-02 15:15:53 +08:00
|
|
|
"Installing python packages for island"
|
2021-04-15 16:11:11 +08:00
|
|
|
Push-Location -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR) -ErrorAction Stop
|
|
|
|
pipenv install --dev
|
|
|
|
Pop-Location
|
2019-10-02 15:15:53 +08:00
|
|
|
"Installing python packages for monkey"
|
2021-04-15 16:11:11 +08:00
|
|
|
Push-Location -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_DIR) -ErrorAction Stop
|
|
|
|
pipenv install --dev
|
|
|
|
Pop-Location
|
2018-12-10 19:08:59 +08:00
|
|
|
|
2021-04-07 19:39:36 +08:00
|
|
|
Configure-precommit($monkey_home)
|
2021-04-06 23:01:22 +08:00
|
|
|
|
2019-10-07 21:24:15 +08:00
|
|
|
$user_python_dir = cmd.exe /c 'py -m site --user-site'
|
|
|
|
$user_python_dir = Join-Path (Split-Path $user_python_dir) -ChildPath "\Scripts"
|
2020-02-03 15:59:19 +08:00
|
|
|
if (!($ENV:Path | Select-String -SimpleMatch $user_python_dir))
|
2020-01-05 05:27:19 +08:00
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Adding python scripts path to user's env"
|
2020-02-03 15:59:19 +08:00
|
|
|
$env:Path += ";" + $user_python_dir
|
2020-01-05 05:27:26 +08:00
|
|
|
[Environment]::SetEnvironmentVariable("Path", $env:Path, "User")
|
2019-10-07 21:24:15 +08:00
|
|
|
}
|
|
|
|
|
2022-05-12 19:33:38 +08:00
|
|
|
$install_mongo_script = (Join-Path -Path $monkey_home -ChildPath "$MONKEY_ISLAND_DIR\windows\install_mongo.ps1")
|
|
|
|
Invoke-Expression "$install_mongo_script -binDir $binDir"
|
2018-12-10 19:08:59 +08:00
|
|
|
|
|
|
|
# Download OpenSSL
|
|
|
|
"Downloading OpenSSL ..."
|
|
|
|
$webClient.DownloadFile($OPEN_SSL_URL, $TEMP_OPEN_SSL_ZIP)
|
|
|
|
"Unzipping OpenSSl"
|
|
|
|
Expand-Archive $TEMP_OPEN_SSL_ZIP -DestinationPath (Join-Path -Path $binDir -ChildPath "openssl") -ErrorAction SilentlyContinue
|
|
|
|
"Removing zip file"
|
|
|
|
Remove-Item $TEMP_OPEN_SSL_ZIP
|
|
|
|
|
2019-10-07 14:53:18 +08:00
|
|
|
# Download and install C++ redistributable
|
|
|
|
"Downloading C++ redistributable ..."
|
|
|
|
$webClient.DownloadFile($CPP_URL, $TEMP_CPP_INSTALLER)
|
|
|
|
Start-Process -Wait $TEMP_CPP_INSTALLER -ErrorAction Stop
|
|
|
|
Remove-Item $TEMP_CPP_INSTALLER
|
|
|
|
|
2018-12-10 19:08:59 +08:00
|
|
|
# Generate ssl certificate
|
|
|
|
"Generating ssl certificate"
|
|
|
|
Push-Location -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR)
|
|
|
|
. .\windows\create_certificate.bat
|
|
|
|
Pop-Location
|
|
|
|
|
2020-02-10 19:48:12 +08:00
|
|
|
if ($agents)
|
|
|
|
{
|
|
|
|
# Adding binaries
|
|
|
|
"Adding binaries"
|
|
|
|
$binaries = (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "\cc\binaries")
|
|
|
|
New-Item -ItemType directory -path $binaries -ErrorAction SilentlyContinue
|
|
|
|
$webClient.DownloadFile($LINUX_64_BINARY_URL, (Join-Path -Path $binaries -ChildPath $LINUX_64_BINARY_PATH))
|
|
|
|
$webClient.DownloadFile($WINDOWS_64_BINARY_URL, (Join-Path -Path $binaries -ChildPath $WINDOWS_64_BINARY_PATH))
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:08:59 +08:00
|
|
|
|
|
|
|
# Check if NPM installed
|
|
|
|
"Installing npm"
|
|
|
|
try
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
$version = cmd.exe /c '"npm" --version 2>&1'
|
|
|
|
if ($version -like "*is not recognized*")
|
|
|
|
{
|
|
|
|
throw System.Management.Automation.CommandNotFoundException
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
"Npm already installed"
|
|
|
|
}
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
catch [System.Management.Automation.CommandNotFoundException]
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Downloading npm ..."
|
|
|
|
$webClient.DownloadFile($NPM_URL, $TEMP_NPM_INSTALLER)
|
|
|
|
Start-Process -Wait $TEMP_NPM_INSTALLER
|
2020-02-03 15:59:19 +08:00
|
|
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
|
2020-01-05 05:27:26 +08:00
|
|
|
Remove-Item $TEMP_NPM_INSTALLER
|
2018-12-10 19:08:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
"Updating npm"
|
|
|
|
Push-Location -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "\cc\ui")
|
|
|
|
& npm update
|
2022-05-10 20:15:22 +08:00
|
|
|
& npm run dev
|
2018-12-10 19:08:59 +08:00
|
|
|
Pop-Location
|
|
|
|
|
|
|
|
# Create infection_monkey/bin directory if not already present
|
|
|
|
$binDir = (Join-Path -Path $monkey_home -ChildPath $MONKEY_DIR | Join-Path -ChildPath "\bin")
|
|
|
|
New-Item -ItemType directory -path $binaries -ErrorAction SilentlyContinue
|
|
|
|
|
2019-10-07 14:53:18 +08:00
|
|
|
# Download upx
|
2020-01-05 05:27:19 +08:00
|
|
|
if (!(Test-Path -Path (Join-Path -Path $binDir -ChildPath "upx.exe")))
|
|
|
|
{
|
2020-01-05 05:27:26 +08:00
|
|
|
"Downloading upx ..."
|
|
|
|
$webClient.DownloadFile($UPX_URL, $TEMP_UPX_ZIP)
|
|
|
|
"Unzipping upx"
|
|
|
|
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
|
|
|
|
# Remove unnecessary files
|
|
|
|
Remove-Item -Recurse -Force (Join-Path -Path $binDir -ChildPath $UPX_FOLDER)
|
|
|
|
"Removing zip file"
|
|
|
|
Remove-Item $TEMP_UPX_ZIP
|
2019-10-07 14:53:18 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:43:51 +08:00
|
|
|
# Get Swimm
|
|
|
|
"Downloading Swimm..."
|
|
|
|
$swimm_filename = Join-Path -Path $HOME -ChildPath "swimm.exe"
|
|
|
|
$webClient.DownloadFile($SWIMM_URL, $swimm_filename)
|
|
|
|
Start-Process $swimm_filename
|
|
|
|
|
2021-04-27 20:20:38 +08:00
|
|
|
|
2018-12-10 19:08:59 +08:00
|
|
|
"Script finished"
|
|
|
|
|
|
|
|
}
|
2020-02-09 17:14:30 +08:00
|
|
|
Deploy-Windows -monkey_home $monkey_home -branch $branch
|