Skip to content

Commit

Permalink
added 'done' button and net cat to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrhakr committed Feb 11, 2024
1 parent 1aea568 commit 155f52a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 69 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ This includes:
- Sharing the scans folder via SMB as ***scans*** (by default)
- Setting SMB permissions for the SMB share
- Creating a desktop shortcut to the scans folder
- Setting the Network Category to Private

All you have to do is run scans.exe and follow the prompts, it's as easy as that.
172 changes: 103 additions & 69 deletions scans.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# v1.4.0.1
# Added gui for exe deployment

# Setup variables and defaults
[string]$scanUser = 'scans'
[string]$scanPass = 'scans'
Expand All @@ -11,7 +8,7 @@
# Load the .NET Framework classes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Web
$ProgressPreference = 'SilentlyContinue'

# Download icon
Expand Down Expand Up @@ -45,7 +42,7 @@ $scanningSetupForm.Controls.Add($scanPassLabel)
$scanPassTextBox = New-Object System.Windows.Forms.TextBox
$scanPassTextBox.Location = New-Object System.Drawing.Point (80,35)
$scanPassTextBox.Size = New-Object System.Drawing.Size (190,20)
$scanPassTextBox.Text = $scanPass # [System.Web.Security.Membership]::GeneratePassword(10, 0)
$scanPassTextBox.Text = [System.Web.Security.Membership]::GeneratePassword(10, 0)
$scanningSetupForm.Controls.Add($scanPassTextBox)

# Create a text box for the user to choose a custom path
Expand Down Expand Up @@ -106,85 +103,116 @@ if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
Write-Verbose "Username: $script:username`nPassword: $script:password`nLocal Dir: $script:folderPath`nSMB Share: $script:shareName"
} else {
Write-Error "You canceled scanning setup"
$scanningSetupForm.Close() | Out-Null;
Exit
}
$scanningSetupForm.Close() | Out-Null;

# Create a new form with a title and a size
$loadingForm = New-Object System.Windows.Forms.Form
$loadingForm.Text = 'Scans.exe - Loading...'
$loadingForm.Icon = 'C:\ProgramData\scans.ico'
$loadingFormHeight = 120
$loadingForm.Size = New-Object System.Drawing.Size (300,$loadingFormHeight)
$loadingForm.StartPosition = 'CenterScreen'
$details = New-Object System.Collections.ArrayList;
function createLoadingForm($done){
# Create a new form with a title and a size
$script:loadingForm = New-Object System.Windows.Forms.Form
$script:loadingForm.Text = 'Scans.exe - Loading...'
if($done -eq $true){
$script:loadingForm.Text = 'Scans.exe - ' + $script:details[0]
} else {
$script:loadingForm.Text = 'Scans.exe - Loading...'
}
$script:loadingForm.Icon = 'C:\ProgramData\scans.ico'
$script:loadingForm.Size = New-Object System.Drawing.Size (300,200)
$script:loadingForm.StartPosition = 'CenterScreen'

# Create a text box for the user to choose a custom password
$loadingText = New-Object System.Windows.Forms.Label
$loadingText.Location = New-Object System.Drawing.Point (10,10)
$loadingText.Size = New-Object System.Drawing.Size (280,20)
$loadingText.Text = 'Loading...'
$loadingForm.Controls.Add($loadingText)
$progrssBarObject = New-Object System.Windows.Forms.ProgressBar
$progrssBarObject.Location = New-Object System.Drawing.Point (10,30)
$progrssBarObject.Size = New-Object System.Drawing.Size (265,20)
$progrssBarObject.Minimum = 0
$progrssBarObject.Maximum = 13
$progrssBarObject.Value = 0
$loadingForm.Controls.Add($progrssBarObject)
$detailsBox = New-Object System.Windows.Forms.ListBox
$detailsBox.Location = New-Object System.Drawing.Point (10,60)
$detailsBox.Size = New-Object System.Drawing.Size (265,$($loadingFormHeight - 40))
$loadingForm.Controls.Add($detailsBox)
# Create a text box for the user to choose a custom password
$script:loadingText = New-Object System.Windows.Forms.Label
$script:loadingText.Location = New-Object System.Drawing.Point (10,10)
$script:loadingText.Size = New-Object System.Drawing.Size (280,20)
if($done -eq $true){
$script:loadingText.Text = $script:details[0]
} else {
$script:loadingText.Text = 'Loading...'
}
$script:loadingForm.Controls.Add($script:loadingText)
$script:progrssBarObject = New-Object System.Windows.Forms.ProgressBar
$script:progrssBarObject.Location = New-Object System.Drawing.Point (10,30)
$script:progrssBarObject.Size = New-Object System.Drawing.Size (265,20)
$script:progrssBarObject.Minimum = 0
$script:progrssBarObject.Maximum = 11
if($done -eq $true){
$script:progrssBarObject.Value = $script:progrssBarObject.Maximum
} else {
$script:progrssBarObject.Value = 0
}
$script:loadingForm.Controls.Add($script:progrssBarObject)
$script:detailsBox = New-Object System.Windows.Forms.ListBox
$script:detailsBox.ScrollAlwaysVisible = $true
$script:detailsBox.Location = New-Object System.Drawing.Point (10,60)
$script:detailsBox.Size = New-Object System.Drawing.Size (265,60)
if($done -eq $true){
foreach($item in $script:details){
$script:detailsBox.Items.Add($item) | Out-Null;
}
}
$script:loadingForm.Controls.Add($script:detailsBox)

# Create a done button and add it to the form
$doneButton = New-Object System.Windows.Forms.Button
$doneButton.Location = New-Object System.Drawing.Point (95,125)
$doneButton.Size = New-Object System.Drawing.Size (100,23)
$doneButton.Text = 'Done'
if($done -eq $true){
$doneButton.Enabled = $true
} else {
$doneButton.Enabled = $false
}
$doneButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$loadingForm.AcceptButton = $doneButton
$loadingForm.Controls.Add($doneButton)
}
$null = createLoadingForm $false
$loadingForm.Show()

$percent = 0
function updateProgressBar($text){
function updateProgressBar($text, $sleep){
$script:loadingText.Text = $text
$script:percent += 1
$script:progrssBarObject.Value = $script:percent
$script:loadingFormHeight += 12
$script:detailsBox.Items.Add($text)
$script:loadingForm.Size = New-Object System.Drawing.Size (300,$script:loadingFormHeight)
$script:detailsBox.Size = New-Object System.Drawing.Size (265,$($script:loadingFormHeight - 100))
Start-Sleep -Milliseconds 250
$script:details.Insert(0, $text) | Out-Null;
$script:text = $text
$script:detailsBox.Items.Insert(0, $text) | Out-Null;
Start-Sleep -Milliseconds $sleep
}

# Gather computer details
updateProgressBar "Gathering local computer details"
updateProgressBar "Gathering local computer details" 500
$computerDetails = Get-CimInstance -ClassName Win32_ComputerSystem
$domainJoined = $computerDetails.PartOfDomain

# Creates scans user account if it doesn't exist, otherwise sets password for account
updateProgressBar "Checking User Details"
updateProgressBar "Checking User Details" 500
if(![boolean](Get-LocalUser -Name $scanUser -ErrorAction SilentlyContinue)) {
updateProgressBar "Creating New User"
updateProgressBar "Creating New User" 500
New-LocalUser -Name $scanUser -Password $($scanPass | ConvertTo-SecureString -AsPlainText -Force) -Description "$description`nPassword: $scanPass" -AccountNeverExpires -PasswordNeverExpires -UserMayNotChangePassword -FullName "scans" | Out-Null;
} else {
updateProgressBar "Updating Existing User"
updateProgressBar "Updating Existing User" 500
Set-LocalUser -Name $scanUser -Password $($scanPass | ConvertTo-SecureString -AsPlainText -Force) -Description "$description`nPassword: $scanPass" -AccountNeverExpires -PasswordNeverExpires $true -UserMayChangePassword $false -FullName "scans" | Out-Null;
}

# Hide scans account from login screen on non domain joined computers
$path = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\Userlist'
$hideAccount = Get-ItemProperty -path $path -name $scanUser -ErrorAction SilentlyContinue;
if($? -and $hideAccount.($scanUser) -eq 0){
updateProgressBar "User account is already hidden from login screen"
updateProgressBar "User account is already hidden from login screen" 500
} elseif(!$domainJoined){
updateProgressBar "Hiding scans user from login screen"
updateProgressBar "Hiding scans user from login screen" 500
if(!(Test-Path $path)){
Write-Verbose "Creating Registry Object at $path"
New-Item -Path $path -Force | Out-Null;
}
New-ItemProperty -path $path -name $scanUser -value 0 -PropertyType 'DWord' -Force | Out-Null;
} else {
updateProgressBar "Computer is domain joined, continuing"
updateProgressBar "Computer is domain joined, continuing" 500
}

# Check if scans folder exists, create if missing
updateProgressBar "Checking if scans folder exists"
if(!(Test-Path -Path $folderPath)){
updateProgressBar "Scans folder doesn't exist. Creating Folder at $folderPath"
updateProgressBar "Creating scans folder" 200
New-Item -Path $($folderPath.Split(':')[0] + ':/') -Name $folderPath.Split(':')[1] -ItemType Directory | Out-Null;
#Check if creating folder was successful $? = Was last command successful?(T/F)
if ($?) {
Expand All @@ -193,57 +221,63 @@ if(!(Test-Path -Path $folderPath)){
Write-Error "Folder creation failed!`nManually Create Folder before Continuing!"
}
} else {
updateProgressBar "Scans folder already exists"
updateProgressBar "Scans folder already exists" 500
}

# Grant full recursive permissions on the scan folder to the scan user and current local user
updateProgressBar "Setting folder permissions"
updateProgressBar "Setting folder permissions" 100
$folderAcl = (Get-Acl $folderPath)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($Env:UserName, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$folderAcl.SetAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($scanUser, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$folderAcl.SetAccessRule($rule)
<# $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$folderAcl.SetAccessRule($rule) #>
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$folderAcl.SetAccessRule($rule)
if($domainJoined){
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Domain Users", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$folderAcl.SetAccessRule($rule)
}
Set-Acl $folderPath $folderAcl

# Check if scans share exists, create if missing
updateProgressBar "Checking if SMB share exists"
if(!((Get-SmbShare).Name).toLower().Contains($shareName)){
updateProgressBar "Creating SMB share"
updateProgressBar "Creating SMB share" 500
New-SmbShare -Name $shareName -Path $folderPath -FullAccess $scanUser | Out-Null;
} else {
updateProgressBar "Updating SMB share permissions"
updateProgressBar "Updating SMB share permissions" 500
Grant-SmbShareAccess -Name $shareName -AccountName $scanUser -AccessRight Full -Force | Out-Null;
}

# Create scan folder desktop shortcut
updateProgressBar "Creating Desktop Shortcut"
$shellObject = New-Object -ComObject ("WScript.Shell");
$desktopShortCut = $shellObject.CreateShortcut("C:\Users\Public\Desktop\Scans.lnk");
$desktopShortCut.TargetPath = $folderPath;
$desktopShortCut.IconLocation = 'C:\ProgramData\scans.ico';
$desktopShortCut.Description = $description;
$desktopShortCut.Save() | Out-Null;
$shortcutPath = "C:\Users\Public\Desktop\Scans.lnk"
if ((Test-Path $shortcutPath -PathType Leaf) -and $shellObject.CreateShortcut($shortcutPath).TargetPath -eq $folderPath){
updateProgressBar "Creating Desktop Shortcut" 500
$shellObject = New-Object -ComObject ("WScript.Shell");
$desktopShortCut = $shellObject.CreateShortcut($shortcutPath);
$desktopShortCut.TargetPath = $folderPath;
$desktopShortCut.IconLocation = 'C:\ProgramData\scans.ico';
$desktopShortCut.Description = $description;
$desktopShortCut.Save() | Out-Null;
}


# Set network profile to Private if not domain joined.
$networkCategory = (Get-NetConnectionProfile).NetworkCategory
updateProgressBar "Checking Net Connection Profile"
if(!$domainJoined -and $networkCategory -ne 'Private'){
updateProgressBar "Set Net Connection Profile to Private"
updateProgressBar "Set Network Category to Private" 500
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
} elseif ($domainJoined -and $networkCategory -ne 'DomainAuthenticated'){
updateProgressBar "Set Net Connection Profile to Domain Authenticated"
updateProgressBar "Set Network Category to Domain Authenticated" 500
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory DomainAuthenticated
} else {
updateProgressBar "Net Connection Profile is already set to $networkCategory"
updateProgressBar "Network Category is already $networkCategory" 500
}

updateProgressBar "Finished, Exiting"
Start-Sleep -Seconds 5

$loadingForm.Close() | Out-Null;
updateProgressBar "Finished" 0
$loadingForm.Close() | Out-Null;
createLoadingForm $true;
$loadingForm.ShowDialog() | Out-Null;
if ($done -eq [System.Windows.Forms.DialogResult]::OK) {
$loadingForm.Close() | Out-Null;
Exit;
}

0 comments on commit 155f52a

Please sign in to comment.