From 17fe627d7fef9c8495b1188c577abb7092c6c52a Mon Sep 17 00:00:00 2001 From: snakems Date: Mon, 25 Sep 2017 10:42:36 +0300 Subject: [PATCH 1/2] Add shortcut mod to profiles --- malboxes/malboxes.py | 43 +++++++++++++++++++++++++++++++++++++ malboxes/profile-example.js | 7 ++++++ 2 files changed, 50 insertions(+) diff --git a/malboxes/malboxes.py b/malboxes/malboxes.py index b7f2821..baf8ac2 100644 --- a/malboxes/malboxes.py +++ b/malboxes/malboxes.py @@ -492,6 +492,13 @@ def prepare_profile(template, config): if "provisioners" in packer: config["packer_extra_provisioners"] = packer["provisioners"] + if "shortcut" in profile: + shortcut_function(fd) + for shortcut_mod in profile["shortcut"]: + if not "arguments" in shortcut_mod: + shortcut_mod["arguments"]=None + shortcut(shortcut_mod["dest"], shortcut_mod["target"], shortcut_mod["arguments"], fd) + fd.close() return config @@ -565,6 +572,42 @@ def document(profile_name, modtype, docpath, fd): fd.write(line) +def shortcut_function(fd): + """ Add shortcut function to the profile """ + fd.write("""function Add-Shortcut{ + param([string]$target_path, [string]$dest_path, [string]$arguments=""); + if (-Not (Test-Path $target_path)){ + Write-Output "[Add-Shortcut::Error] Can't add shortcut. Target path '$target_path' not found."; + return; + } + if ((Test-Path $dest_path)){ + Write-Output "[Add-Shortcut::Error] Can't add shortcut. Destination path '$dest_path' exists."; + return; + } + Write-Output "[Add-Shortcut] Destination: '$dest_path'; Target: '$target_path'"; + if((Get-Item $target_path) -is [System.IO.DirectoryInfo]){ + Start-Process "cmd.exe" -ArgumentList "/c mklink /D `"$dest_path`" `"$target_path`"" -Wait -NoNewWindow; + }else{ + $_shell = New-Object -ComObject ("WScript.Shell"); + $_shortcut = $_shell.CreateShortcut($dest_path); + $_shortcut.TargetPath=$target_path; + if(-Not [String]::IsNullOrEmpty($arguments)){ + $_shortcut.Arguments=$arguments; + } + $_shortcut.WorkingDirectory=[System.IO.Path]::GetDirectoryName($target_path); + $_shortcut.Save(); + } + }\r\n""") + +def shortcut(dest, target, arguments, fd): + """ Create shortcut on Desktop """ + if arguments is None: + line = "Add-Shortcut \"{0}\" \"{1}\"\r\n".format(target, dest) + print("Adding shortcut {}: {}".format(dest, target)) + else: + line = "Add-Shortcut \"{0}\" \"{1}\" \"{2}\"\r\n".format(target, dest, arguments) + print("Adding shortcut {}: {} with arguments {}".format(dest, target, arguments)) + fd.write(line) def main(): global DEBUG diff --git a/malboxes/profile-example.js b/malboxes/profile-example.js index cef7296..e135220 100644 --- a/malboxes/profile-example.js +++ b/malboxes/profile-example.js @@ -2,6 +2,13 @@ "package": [{"package": "thunderbird"}], "document": [{"modtype": "add", "docpath": "C:\\Test.doc"}], "directory": [{"modtype": "add", "dirpath": "C:\\mlbxs\\"}], + "shortcut": [ + //Create shortcuts for exe files + {"dest": "$env:USERPROFILE\\Desktop\\Fiddler.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe"}, + {"dest": "$env:USERPROFILE\\Desktop\\Fiddler Port 9999.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe", "arguments": "/port:9999"}, + //Create shortcuts for folders + {"dest": "$env:USERPROFILE\\Desktop\\SysInternals", "target": "$env:ALLUSERSPROFILE\\chocolatey\\lib\\sysinternals\\tools"} + ], "registry": [ {"modtype": "add", "key": "HKLM:\\Hardware\\Description\\System", "value": "04/04/04", "name": "SystemBiosDate", "valuetype": "String"}, {"modtype": "add", "key": "HKLM:\\Hardware\\Description\\System", "value": "Hardware Version 0.0 PARTTBLX", "name": "SystemBiosDate", "valuetype": "String"}, From ac24fd544faea46a13fff7f9dc261dccedff0cc1 Mon Sep 17 00:00:00 2001 From: snakems Date: Tue, 26 Sep 2017 11:37:11 +0300 Subject: [PATCH 2/2] Improve ShortcutMod --- malboxes/malboxes.py | 28 +++------------- malboxes/profile-example.js | 10 ++++-- malboxes/scripts/windows/add-shortcut.ps1 | 41 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 malboxes/scripts/windows/add-shortcut.ps1 diff --git a/malboxes/malboxes.py b/malboxes/malboxes.py index baf8ac2..8673084 100644 --- a/malboxes/malboxes.py +++ b/malboxes/malboxes.py @@ -574,30 +574,10 @@ def document(profile_name, modtype, docpath, fd): def shortcut_function(fd): """ Add shortcut function to the profile """ - fd.write("""function Add-Shortcut{ - param([string]$target_path, [string]$dest_path, [string]$arguments=""); - if (-Not (Test-Path $target_path)){ - Write-Output "[Add-Shortcut::Error] Can't add shortcut. Target path '$target_path' not found."; - return; - } - if ((Test-Path $dest_path)){ - Write-Output "[Add-Shortcut::Error] Can't add shortcut. Destination path '$dest_path' exists."; - return; - } - Write-Output "[Add-Shortcut] Destination: '$dest_path'; Target: '$target_path'"; - if((Get-Item $target_path) -is [System.IO.DirectoryInfo]){ - Start-Process "cmd.exe" -ArgumentList "/c mklink /D `"$dest_path`" `"$target_path`"" -Wait -NoNewWindow; - }else{ - $_shell = New-Object -ComObject ("WScript.Shell"); - $_shortcut = $_shell.CreateShortcut($dest_path); - $_shortcut.TargetPath=$target_path; - if(-Not [String]::IsNullOrEmpty($arguments)){ - $_shortcut.Arguments=$arguments; - } - $_shortcut.WorkingDirectory=[System.IO.Path]::GetDirectoryName($target_path); - $_shortcut.Save(); - } - }\r\n""") + filename = resource_filename(__name__, "scripts/windows/add-shortcut.ps1") + with open(filename, 'r') as add_shortcut_file: + fd.write(add_shortcut_file.read()) + add_shortcut_file.close(); def shortcut(dest, target, arguments, fd): """ Create shortcut on Desktop """ diff --git a/malboxes/profile-example.js b/malboxes/profile-example.js index e135220..5265770 100644 --- a/malboxes/profile-example.js +++ b/malboxes/profile-example.js @@ -3,9 +3,13 @@ "document": [{"modtype": "add", "docpath": "C:\\Test.doc"}], "directory": [{"modtype": "add", "dirpath": "C:\\mlbxs\\"}], "shortcut": [ - //Create shortcuts for exe files - {"dest": "$env:USERPROFILE\\Desktop\\Fiddler.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe"}, - {"dest": "$env:USERPROFILE\\Desktop\\Fiddler Port 9999.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe", "arguments": "/port:9999"}, + //Create shortcuts for exe files on Desktop + {"dest": "Fiddler.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe"}, + {"dest": "$env:DESKTOP\\Fiddler Port 9999.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe", "arguments": "/port:9999"}, + //Create shortcuts for exe files in Start Menu + {"dest": "$env:STARTMENU\\Fiddler\\Fiddler.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe"}, + //Create shortcuts for exe files in Startup folder (Autorun) + {"dest": "$env:STARTUP\\Fiddler.lnk", "target": "$env:LOCALAPPDATA\\Programs\\Fiddler\\Fiddler.exe"}, //Create shortcuts for folders {"dest": "$env:USERPROFILE\\Desktop\\SysInternals", "target": "$env:ALLUSERSPROFILE\\chocolatey\\lib\\sysinternals\\tools"} ], diff --git a/malboxes/scripts/windows/add-shortcut.ps1 b/malboxes/scripts/windows/add-shortcut.ps1 new file mode 100644 index 0000000..44031bc --- /dev/null +++ b/malboxes/scripts/windows/add-shortcut.ps1 @@ -0,0 +1,41 @@ +function Add-Shortcut{ + param([string]$target_path, [string]$dest_path, [string]$arguments=""); + #Create new env varibles for quick use + $env:DESKTOP = [Environment]::GetFolderPath('Desktop'); + $env:STARTMENU = [Environment]::GetFolderPath('StartMenu'); + $env:STARTUP = [Environment]::GetFolderPath('Startup'); + #Check if path to shortcut setted. If not - set path to Desktop + if (-Not [System.IO.Path]::IsPathRooted($dest_path)){ + $dest_path = $env:USERPROFILE + "\\Desktop\\" + $dest_path; + } + #Create subdirectory if doesn't exist + $_path=Split-Path $dest_path; + if (-Not (Test-Path $_path)){ + mkdir -Force $_path; + } + #Check if target exist + if (-Not (Test-Path $target_path)){ + Write-Output "[Add-Shortcut::Error] Can't add shortcut. Target path '$target_path' not found."; + return; + } + #Check if shortcut exist + if ((Test-Path $dest_path)){ + Write-Output "[Add-Shortcut::Error] Can't add shortcut. Destination path '$dest_path' exist."; + return; + } + Write-Output "[Add-Shortcut] Destination: '$dest_path'; Target: '$target_path'"; + if((Get-Item $target_path) -is [System.IO.DirectoryInfo]){ + #Create shortcut (Symbolic link) for folder + Start-Process "cmd.exe" -ArgumentList "/c mklink /D `"$dest_path`" `"$target_path`"" -Wait -NoNewWindow; + }else{ + #Create shortcut for file + $_shell = New-Object -ComObject ("WScript.Shell"); + $_shortcut = $_shell.CreateShortcut($dest_path); + $_shortcut.TargetPath=$target_path; + if(-Not [String]::IsNullOrEmpty($arguments)){ + $_shortcut.Arguments=$arguments; + } + $_shortcut.WorkingDirectory=[System.IO.Path]::GetDirectoryName($target_path); + $_shortcut.Save(); + } +}