This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathHookInject.cs
171 lines (148 loc) · 5.41 KB
/
HookInject.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using EasyHook;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Ipc;
namespace PoshInternals
{
public class HookInterface : MarshalByRefObject
{
private static string _channelName;
public Runspace Runspace;
public static IpcServerChannel CreateServer()
{
return RemoteHooking.IpcCreateServer<HookInterface>(
ref _channelName,
WellKnownObjectMode.SingleCall);
}
public static void Inject(int pid, string entryPoint, string dll, string typeName, string scriptBlock, string modulePath, string additionalCode, bool log)
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
RemoteHooking.Inject(
pid,
assembly.Location, // 32-bit version (the same because AnyCPU)
assembly.Location, // 64-bit version (the same because AnyCPU)
_channelName,
entryPoint,
dll,
typeName,
scriptBlock,
modulePath,
additionalCode,
log);
}
public void ReportError(
Int32 InClientPID,
Exception e)
{
throw new Exception(string.Format("Process [{0}] reported error an error!"), e);
}
public void WriteHost(
string e)
{
Console.WriteLine(e);
}
}
public class HookInjection : EasyHook.IEntryPoint
{
public Runspace Runspace;
public HookInterface Interface = null;
public HookInjection(
RemoteHooking.IContext InContext,
String InChannelName,
String entryPoint,
String dll,
String returnType,
String scriptBlock,
String modulePath,
String additionalCode,
bool eventLog)
{
Log("Opening hook interface channel...", eventLog);
Interface = RemoteHooking.IpcConnectClient<HookInterface>(InChannelName);
try
{
Runspace = RunspaceFactory.CreateRunspace();
Runspace.Open();
//Runspace.SessionStateProxy.SetVariable("HookInterface", Interface);
}
catch (Exception ex)
{
Log("Failed to open PowerShell runspace." + ex.Message, eventLog);
Interface.ReportError(RemoteHooking.GetCurrentProcessId(), ex);
}
}
public void Run(
RemoteHooking.IContext InContext,
String channelName,
String entryPoint,
String dll,
String returnType,
String scriptBlock,
String modulePath,
String additionalCode,
bool eventLog)
{
try
{
Log(String.Format("Executing Set-Hook -Local -EntryPoint '{0}' -Dll '{1}' -ReturnType '{2}' -ScriptBlock '{3}' ", entryPoint, dll, returnType, scriptBlock), eventLog);
using (var ps = PowerShell.Create())
{
ps.Runspace = Runspace;
ps.AddCommand("Import-Module");
ps.AddArgument(modulePath);
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("Set-Hook");
ps.AddParameter("EntryPoint", entryPoint);
ps.AddParameter("Dll", dll);
ps.AddParameter("ReturnType", returnType);
ps.AddParameter("AdditionalCode", additionalCode);
var sb = ScriptBlock.Create(scriptBlock);
ps.AddParameter("ScriptBlock", sb);
ps.Invoke();
foreach (var record in ps.Streams.Error)
{
Log("Caught exception " + record.Exception.Message, eventLog);
}
}
RemoteHooking.WakeUpProcess();
new System.Threading.ManualResetEvent(false).WaitOne();
}
catch (Exception e)
{
Log("Caught exception " + e.Message, eventLog);
try
{
Interface.ReportError(RemoteHooking.GetCurrentProcessId(), e);
}
catch
{
}
return;
}
}
private static void Log(string message, bool shouldLog)
{
if (!shouldLog) return;
try
{
if (!System.Diagnostics.EventLog.SourceExists("PoshHook"))
{
System.Diagnostics.EventLog.CreateEventSource("PoshHook", "Application");
}
var log = new System.Diagnostics.EventLog("Application", ".", "PoshHook");
log.WriteEntry(message);
}
catch
{
}
}
}
}