/
PushMe.ps1
176 lines (157 loc) · 6.8 KB
/
PushMe.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Local account push for .exe using powershell and PsExec.exe
# Written by Jeremy Martin - jeremy@informationwarfarecenter.com
#
# Top two lines contais the IP address range to target
# $HostName = IP address converted to system name
# $DomainUser param = -u $HostName/user
# $Pass = -p password
# $Args contains .exe to push
clear
$results = @()
$details = @()
$Subnet = Read-Host -Prompt "Please Enter the Subnet the you want to search in (Example: 192.168.0)"
$IPStart = Read-Host -Prompt "Please Enter the First IP in that range (Example:1)"
$IPEnd = Read-Host -Prompt "Please Enter the Last IP in that range (Example:255)"
$Domain = Read-Host -Prompt "Please Enter the Domain (NA for None)"
$ScriptPath = "c:\"
$Prog = Read-Host -Prompt "Please Enter the program to push"
$Method = Read-Host -Prompt "Please Enter the distribution method. (1. PsExec [Requires UAC Disabled on remote] - 2. PSSession [Remote Powershell is required] - 3. WMI)"
$Report = "$Subnet.$IPStart-$IPEnd-report.csv"
$User = Read-Host -Prompt "Please Enter the Admin User"
$Pass = Read-Host -Prompt "Please Enter the Admin Pass"
$tempFolder=$Env:TEMP
clear
echo "Scanning $Subnet.$IPStart-$IPEnd"
echo "Domain: $Domain"
echo "User: $User"
Echo "Program: $Prog"
if (Test-Path $Report) {
$results += Import-Csv -Path $Report
}
echo "Starting the push now..."
$IPStart..$IPEnd | %{
$IP = "$Subnet.$_"
If (Test-Connection -count 3 -comp $IP -quiet) {
$HostName = [System.Net.Dns]::GetHostByAddress($IP).HostName
$HostName = $HostName.trimend(".domain")
if ($Domain -eq "NA"){
$Domain=$HostName
}
$cmdkeyParams = @('/add:$HostName /user:$Domain\$User /pass:$Pass')
Start-Process -FilePath cmdkey.exe -ArgumentList "$cmdkeyParams" -wait
echo "$IP - $HostName"
$details = @{
Date = get-date
ComputerName = $Hostname
IPAddress = $IP
Method = ""
Install = ""
Copied = ""
}
If ($Method -eq 1){
# Method 1 is a PsExec push
$Target = "\\$HostName"
$PUser = "-u $Domain\$User"
$PPass = "-p $Pass"
$Args = @('-i -f -c', $Prog)
$Exec = "./PsExec.exe"
$Params = "$Target $PUser $PPass $Args"
$details.Method = "PsExec"
echo "$Exec $Params"
$process = Start-Process -FilePath "$Exec" -ArgumentList "$Params" -PassThru
Wait-Process -InputObject $process
if ($process.ExitCode -eq 0) {
$details.Install = "Successful"
$details.Copied = "Yes"
echo "$Prog was pushed to $IP - $HostName"
New-Object -TypeName psobject -Property $props
} elseif ($process.ExitCode -eq 2){
$details.Install = "Copied but cancelled locally"
$details.Copied = "Yes"
echo "$Prog was pushed to $IP - $HostName"
} else {
$details.Install = "Failed"
$details.Copied = "No"
echo "$Prog FAILED to push to $IP - $HostName"
}
} ElseIf ($Method -eq 2){
# Method 2 is a PSSession push
$Target = "\\$HostName\c$"
$details.Method = "PSSession"
echo "Remote copy & run. Attempting to copy data now"
if (Test-Path "$Target\$Prog"){
echo "Found it on the other side"
Remove-Item -Path "$Target\$Prog" -Force -Verbose
}
Try
{
Copy-item $Prog -Destination $Target -PassThru -Force -Verbose
echo "$Prog copied to $IP - $HostName"
$details.Copied = "Yes"
} Catch {
echo "Failed to copy $Prog to $HostName."
$details.Copied = "No"
}
Try
{
$secpasswd = ConvertTo-SecureString $Pass -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
$ScriptBlock = [ScriptBlock]::Create("$ScriptPath\$Prog");
$session = New-PSSession -ComputerName $HostName -credential $mycreds
echo "Attempting Session on $IP - $HostName"
Invoke-Command -Session $session -ScriptBlock $ScriptBlock -Verbose
echo "Successfully connected to remote server."
# remove-pssession -session $session -Verbose
echo "$Prog running on $IP - $HostName"
$details.Install = "Successful"
} Catch {
echo "$Prog Failed to push to $IP - $HostName"
$details.Install = "Failed"
}
echo "Finished with $HostName"
} ElseIf ($Method -eq 3){
# Method 3 is a WMI push
echo "WMI push initiated"
$details.Method = "WMI"
$Target = "\\$HostName\c$"
echo "Remote copy & run. Attempting to copy data now"
if (Test-Path "$Target\$Prog"){
echo "Found it on the other side"
Remove-Item -Path "$Target\$Prog" -Force -Verbose
}
Try {
Copy-item $Prog -Destination $Target -PassThru -Force -Verbose
echo "$Prog copied to $IP - $HostName"
$details.Copied = "Yes"
} Catch {
echo "Failed to copy $Prog to $HostName."
$details.Copied = "No"
}
Try {
$RemoteStartInstall = ([WMICLASS]"\\$HostName\ROOT\CIMV2:win32_process").Create("$ScriptPath\$Prog")
# $Action = { if ($EventArgs.NewEvent.ProcessID -eq $RemoteStartInstall.ProcessId) {
# echo "Remote Install"
# # Remove-Item $ScriptPath
# Unregister-Event -SourceIdentifier Install
# Get-Job -Name Install | Stop-Job -PassThru | Remove-Job -Force
# }
# }
# Register-WmiEvent -SourceIdentifier Install -ComputerName $HostName -Class Win32_ProcessStopTrace -Action $Action
$details.Install = "Successful"
} Catch {
echo "Remote Install failed"
$details.Install = "Failed"
}
}
$cmdkeydParams = @('/delete:$HostName')
if ($Domain -eq $HostName){
$Domain="NA"
} else {
}
$results += New-Object PSObject -Property $details
} else {
Write-Host "the $IP is not reachable"
}
}
$results | export-csv -Path $Report -NoTypeInformation -Force
Invoke-Item $Report