/
MainForm.cs
167 lines (153 loc) · 6.77 KB
/
MainForm.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
using SharpPcap;
using SharpPcap.Packets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace RiftShark
{
public partial class MainForm : Form
{
private bool mClosing = false;
private List<Type> mPluginTypes = new List<Type>();
public MainForm(string[] pArgs)
{
InitializeComponent();
Text = "RiftShark " + Program.AssemblyVersion;
LoadPlugins();
foreach (string fileName in pArgs) OpenFile(fileName);
}
private void LoadPlugins()
{
if (!Directory.Exists(Config.Instance.Plugins)) Directory.CreateDirectory(Config.Instance.Plugins);
string[] plugins = Directory.GetFiles(Config.Instance.Plugins, "*.dll", SearchOption.TopDirectoryOnly);
foreach (string plugin in plugins)
{
Assembly assembly = Assembly.LoadFile(Path.GetFullPath(plugin));
foreach (Type type in assembly.GetTypes())
{
if (!type.IsSubclassOf(typeof(Plugin))) continue;
if (!type.IsClass || type.IsAbstract) continue;
if (type.GetConstructor(Type.EmptyTypes) == null) continue;
mPluginTypes.Add(type);
}
}
}
private void MainForm_FormClosing(object pSender, FormClosingEventArgs pArgs)
{
mClosing = true;
}
private SessionForm NewSession()
{
SessionForm session = new SessionForm();
session.Show(mDockPanel, DockState.Document);
session.CreatePlugins(mPluginTypes);
return session;
}
private void mFileImportMenu_Click(object pSender, EventArgs pArgs)
{
if (mImportKEYSDialog.ShowDialog(this) != DialogResult.OK) return;
byte[] characterKey = null;
byte[] worldKey = null;
using (BinaryReader reader = new BinaryReader(new FileStream(mImportKEYSDialog.FileName, FileMode.Open)))
{
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
bool unknown = false;
switch (reader.ReadByte())
{
case 0x00:
characterKey = reader.ReadBytes(128);
break;
case 0x01:
worldKey = reader.ReadBytes(128);
break;
default: unknown = true; break;
}
if (unknown) break;
}
}
if (characterKey == null && worldKey == null)
{
MessageBox.Show(this, "You have selected an invalid keys file.", "Invalid Keys", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (characterKey != null) Array.Reverse(characterKey);
if (worldKey != null) Array.Reverse(worldKey);
Dictionary<bool, byte[]> keys = new Dictionary<bool, byte[]>();
keys[true] = characterKey;
keys[false] = worldKey;
if (mImportPCAPDialog.ShowDialog(this) != DialogResult.OK) return;
string pcapFileName = mImportPCAPDialog.FileName;
PcapOfflineDevice device = new PcapOfflineDevice(pcapFileName);
device.Open();
Packet packet = null;
SessionForm session = null;
Dictionary<SessionForm, int> totalSessionTCPPackets = new Dictionary<SessionForm, int>();
HashSet<SessionForm> terminatedSessions = new HashSet<SessionForm>();
while ((packet = device.GetNextPacket()) != null)
{
TCPPacket tcpPacket = packet as TCPPacket;
if (tcpPacket == null) continue;
session = Array.Find(MdiChildren, f => (f as SessionForm).MatchTCPPacket(tcpPacket)) as SessionForm;
if (session == null && tcpPacket.Syn && !tcpPacket.Ack)
{
session = NewSession();
session.ImportClientPrivateKeys(keys);
session.BufferTCPPacket(tcpPacket);
totalSessionTCPPackets[session] = 0;
Application.DoEvents();
}
if (session != null && !terminatedSessions.Contains(session)) totalSessionTCPPackets[session] += 1;
if (session != null && (tcpPacket.Fin || tcpPacket.Rst)) terminatedSessions.Add(session);
}
device.Close();
device = new PcapOfflineDevice(pcapFileName);
device.Open();
Dictionary<SessionForm, int> processedSessionTCPPackets = new Dictionary<SessionForm, int>();
Dictionary<SessionForm, int> processedSessionPercents = new Dictionary<SessionForm, int>();
while ((packet = device.GetNextPacket()) != null)
{
if (mClosing) break;
TCPPacket tcpPacket = packet as TCPPacket;
if (tcpPacket == null) continue;
session = Array.Find(MdiChildren, f => (f as SessionForm).MatchTCPPacket(tcpPacket)) as SessionForm;
if (session != null)
{
if (mDockPanel.ActiveDocument != session) session.Activate();
if (!processedSessionTCPPackets.ContainsKey(session)) processedSessionTCPPackets[session] = 0;
if (!processedSessionPercents.ContainsKey(session)) processedSessionPercents[session] = 0;
processedSessionTCPPackets[session] += 1;
session.BufferTCPPacket(tcpPacket);
int newPercent = (processedSessionTCPPackets[session] * 100) / totalSessionTCPPackets[session];
while (newPercent > processedSessionPercents[session])
{
processedSessionPercents[session] += 1;
session.ProgressUpdate();
}
}
}
}
private void mFileOpenMenu_Click(object pSender, EventArgs pArgs)
{
if (mOpenDialog.ShowDialog(this) == DialogResult.OK) foreach (string fileName in mOpenDialog.FileNames) OpenFile(fileName);
}
private void OpenFile(string pFileName)
{
SessionForm session = NewSession();
session.OpenFile(pFileName);
GC.Collect();
}
private void mFileQuit_Click(object pSender, EventArgs pArgs)
{
Close();
}
}
}