-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClientRequestSniffer.cs
154 lines (125 loc) · 4.2 KB
/
ClientRequestSniffer.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
namespace HttpReverseProxy.Plugin.ClientRequestSniffer
{
using HttpReverseProxyLib.DataTypes.Class;
using HttpReverseProxyLib.DataTypes.Enum;
using HttpReverseProxyLib.DataTypes.Interface;
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading.Tasks;
using ClientSnifferConfig = HttpReverseProxy.Plugin.ClientRequestSniffer.Config;
public partial class ClientRequestSniffer : IPlugin
{
#region MEMBERS
private PluginProperties pluginProperties = new PluginProperties();
private NamedPipeClientStream pipeClient = null;
private StreamWriter pipeWriter = null;
#endregion
#region PUBLIC
public ClientRequestSniffer()
{
// Set plugin properties
this.pluginProperties = new PluginProperties()
{
Name = ClientSnifferConfig.PluginName,
Priority = ClientSnifferConfig.PluginPriority,
Version = ClientSnifferConfig.PluginVersion,
PluginDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins", ClientSnifferConfig.PluginName),
IsActive = true,
SupportedProtocols = ProxyProtocol.Http | ProxyProtocol.Https
};
}
/// <summary>
///
/// </summary>
/// <param name="requestObj"></param>
private void SendClientRequestDataToPipe(RequestObj requestObj)
{
// 2. Send request/response data to pipe/GUI
var pipeData = $"HTTPREQ||{requestObj.SrcMac}||{requestObj.SrcIp}||{requestObj.SrcPort}||" +
$"{requestObj.ClientRequestObj.Host}||80||{requestObj.HttpLogData}\r\n";
Task.Run(() => this.WriteToPipe(requestObj, pipeData));
this.pluginProperties.PluginHost.LoggingInst.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ClientRequestSniffer.SendClientRequestDataToPipe(): Sending data to attack service pipe: {0} ...", pipeData.Trim().Substring(0, 40));
}
/// <summary>
///
/// </summary>
/// <param name="requestObj"></param>
/// <param name="pipeData"></param>
/// <returns></returns>
public bool WriteToPipe(RequestObj requestObj, string pipeData)
{
var retVal = false;
// Create Pipe
try
{
if (this.pipeClient == null)
{
this.pipeClient = new NamedPipeClientStream(ClientSnifferConfig.DATA_OUTPUT_PIPE_NAME);
}
if (!this.pipeClient.IsConnected)
{
this.pipeClient.Connect(500);
}
if (this.pipeClient?.IsConnected == true)
{
if (this.pipeWriter == null)
{
this.pipeWriter = new StreamWriter(this.pipeClient);
this.pipeWriter.AutoFlush = true;
}
if (this.pipeWriter != null &&
this.pipeClient.IsConnected &&
this.pipeClient.CanWrite)
{
if (pipeData.Length > 0)
{
var tmpBuffer = pipeData.Trim();
this.pipeWriter.WriteLine(tmpBuffer);
}
retVal = true;
}
else
{
this.pipeClient = null;
this.pipeWriter = null;
}
}
}
catch (System.TimeoutException tex)
{
this.pluginProperties.PluginHost.LoggingInst.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Warning, "ClientRequestSniffer.WriteToPipe(TimeOutException): {0}", tex.Message);
}
catch (Exception ex)
{
this.pluginProperties.PluginHost.LoggingInst.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Warning, "ClientRequestSniffer.WriteToPipe(Exception): {0}", ex.Message);
}
return retVal;
}
#endregion
#region INTERFACE IMPLEMENTATION: Properties
public PluginProperties Config { get { return this.pluginProperties; } set { this.pluginProperties = value; } }
#endregion
#region INTERFACE IMPLEMENTATION: IComparable
public int CompareTo(IPlugin other)
{
if (other == null)
{
return 1;
}
if (this.Config.Priority > other.Config.Priority)
{
return 1;
}
else if (this.Config.Priority < other.Config.Priority)
{
return -1;
}
else
{
return 0;
}
}
#endregion
}
}