-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumpFileParser.cs
executable file
·240 lines (192 loc) · 6.78 KB
/
DumpFileParser.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// This file is part of bugreport.
// Copyright (c) 2006-2009 The bugreport Developers.
// See AUTHORS.txt for details.
// Licensed under the GNU General Public License, Version 3 (GPLv3).
// See LICENSE.txt for details.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
namespace bugreport
{
public interface IParsable
{
ReadOnlyCollection<ReportItem> ExpectedReportItems { get; }
UInt32 BaseAddress { get; }
UInt32 EntryPointAddress { get; }
Byte[] GetBytes();
}
public sealed class DumpFileParser : IParsable,
IDisposable
{
private readonly List<ReportItem> expectedReportItems;
private readonly String functionNameToParse = "main";
private readonly List<Byte[]> opcodeList;
private readonly StreamReader reader;
private readonly Stream stream;
private Boolean inTextSection;
public DumpFileParser(Stream stream, String functionNameToParse)
{
this.functionNameToParse = functionNameToParse;
this.stream = stream;
this.stream.Position = 0;
reader = new StreamReader(this.stream);
expectedReportItems = new List<ReportItem>();
opcodeList = Parse();
}
#region IDisposable Members
public void Dispose()
{
if (null != reader)
{
reader.Dispose();
}
}
#endregion
#region IParsable Members
public ReadOnlyCollection<ReportItem> ExpectedReportItems
{
get { return expectedReportItems.AsReadOnly(); }
}
public UInt32 BaseAddress { get; private set; }
public UInt32 EntryPointAddress { get; private set; }
public Byte[] GetBytes()
{
if (opcodeList.Count == 0)
{
return null;
}
var total = 0;
foreach (var bytes in opcodeList)
{
total += bytes.Length;
}
var allByteCount = 0;
var allBytes = new Byte[total];
foreach (var bytes in opcodeList)
{
for (var i = 0; i < bytes.Length; i++)
{
allBytes[i + allByteCount] = bytes[i];
}
allByteCount += bytes.Length;
}
return allBytes;
}
#endregion
public static Byte[] GetByteArrayFor(String hex)
{
var hexStrings = hex.Split(new[] {' '});
var hexBytes = new Byte[hexStrings.Length];
for (var i = 0; i < hexStrings.Length; ++i)
{
hexBytes[i] = Byte.Parse(hexStrings[i], NumberStyles.HexNumber);
}
return hexBytes;
}
private static UInt32 GetAddressFrom(String line)
{
var address = line.Substring(0, 8);
return UInt32.Parse(address, NumberStyles.HexNumber);
}
private static String GetHexWithSpacesFrom(String line)
{
var colonIndex = line.IndexOf(':');
if (colonIndex == -1)
{
return null;
}
if (colonIndex != 8)
{
return null;
}
var afterColonToEnd = line.Substring(colonIndex + 1).Trim();
var doubleSpaceIndex = afterColonToEnd.IndexOf(" ", StringComparison.Ordinal);
var spaceTabIndex = afterColonToEnd.IndexOf(" \t", StringComparison.Ordinal);
Int32 endOfHexIndex;
if (doubleSpaceIndex >= 0 &&
spaceTabIndex >= 0)
{
endOfHexIndex = doubleSpaceIndex < spaceTabIndex ? doubleSpaceIndex : spaceTabIndex;
}
else
{
endOfHexIndex = doubleSpaceIndex > spaceTabIndex ? doubleSpaceIndex : spaceTabIndex;
}
if (endOfHexIndex == -1)
{
return null;
}
var hexString = afterColonToEnd.Substring(0, endOfHexIndex).Trim();
return hexString;
}
private static Byte[] GetHexFrom(String line)
{
if (line.Trim().Length == 0)
{
return null;
}
var hex = GetHexWithSpacesFrom(line);
if (null == hex)
{
return null;
}
var hexBytes = GetByteArrayFor(hex);
return hexBytes;
}
private static Boolean HasAnnotation(String line)
{
return line.Contains("//<OutOfBoundsMemoryAccess ");
}
private static ReportItem GetAnnotationFrom(String line)
{
var locationIndex = line.IndexOf('=') + 1;
var location = UInt32.Parse(line.Substring(locationIndex + "/>".Length, 8), NumberStyles.HexNumber);
var exploitableIndex = line.IndexOf('=', locationIndex + 1) + 1;
var exploitable =
Boolean.Parse(line.Substring(exploitableIndex, (line.Length - exploitableIndex) - "/>".Length));
return new ReportItem(location, exploitable);
}
private void UpdateMainInfoFrom(String line)
{
if (String.IsNullOrEmpty(line) || line[0] < '0' ||
line[0] > '7')
{
return;
}
if (line.Contains("<_start>:"))
{
BaseAddress = GetAddressFrom(line);
inTextSection = true;
}
if (line.Contains("<" + functionNameToParse + ">:"))
{
EntryPointAddress = GetAddressFrom(line);
}
}
private List<Byte[]> Parse()
{
var opcodes = new List<Byte[]>();
while (!reader.EndOfStream)
{
var currentLine = reader.ReadLine();
if (HasAnnotation(currentLine))
{
var item = GetAnnotationFrom(currentLine);
expectedReportItems.Add(item);
}
UpdateMainInfoFrom(currentLine);
if (inTextSection)
{
var opcode = GetHexFrom(currentLine);
if (opcode != null)
{
opcodes.Add(GetHexFrom(currentLine));
}
}
}
return opcodes;
}
}
}