-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
321 lines (252 loc) · 12.8 KB
/
Program.cs
File metadata and controls
321 lines (252 loc) · 12.8 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Note;
using System.IO;
using System.Drawing;
namespace OneNoteAnalyzer
{
class Program
{
public static void ExtractAttachment(string onepath, string exportdirectory)
{
Console.WriteLine("\n -> Extracted OneNote Document Attachments: \n");
string DirectoryName = exportdirectory + "\\OneNoteAttachments";
if (!Directory.Exists(DirectoryName))
{
Directory.CreateDirectory(DirectoryName);
}
Document OneNoteFile = new Document(onepath);
IList<AttachedFile> onenotelist = OneNoteFile.GetChildNodes<AttachedFile>();
var counter = 1;
foreach (AttachedFile file in onenotelist)
{
using (Stream readStream = new MemoryStream(file.Bytes))
{
Console.WriteLine(" -> Extracted Actual Attachment Path: " + Path.GetDirectoryName(file.FilePath) + " | FileName: " + file.FileName + " | Size: " + file.Bytes.Length);
using (Stream outStream = System.IO.File.OpenWrite(DirectoryName + "\\" + counter + "_" + file.FileName))
{
readStream.CopyTo(outStream);
}
}
counter++;
}
Console.WriteLine("\n -> OneNote Document Attachments Extraction Path: " + DirectoryName);
}
public static void ExtractMetaData(string onepath)
{
Document OneNoteFile = new Document(onepath);
int pagecount = OneNoteFile.GetChildNodes<Page>().Count;
Console.WriteLine("\n -> Page Count: " + pagecount);
Console.WriteLine(" -> Page MetaData: \n");
foreach (Page page in OneNoteFile)
{
Console.WriteLine("\n ---------------------------------------------\n");
Console.WriteLine(" -> Title: " + page.CachedTitleString);
Console.WriteLine(" -> Author: " + page.Author);
Console.WriteLine(" -> CreationTime: " + page.CreationTime);
Console.WriteLine(" -> LastModifiedTime: " + page.LastModifiedTime);
}
Console.WriteLine("\n ---------------------------------------------\n");
}
public static void ExtractImages(string onepath, string exportdirectory)
{
Document OneNoteFile = new Document(onepath);
string DirectoryName = exportdirectory + "\\OneNoteImages";
if (!Directory.Exists(DirectoryName))
{
Directory.CreateDirectory(DirectoryName);
}
IList<Aspose.Note.Image> onenodes = OneNoteFile.GetChildNodes<Aspose.Note.Image>();
Console.WriteLine("\n -> Extracted OneNote Document Images: \n");
var counter = 1;
foreach (Aspose.Note.Image image in onenodes)
{
if (image.FileName == null)
{
}
else
{
Console.WriteLine(" -> Extracted Image FileName: " + counter + "_" + image.FileName + " | HyperLinkURL: " + (image.HyperlinkUrl == null ? "Null" : image.HyperlinkUrl) + "");
using (MemoryStream stream = new MemoryStream(image.Bytes))
{
using (var filezstream = File.Create(DirectoryName + "\\" + counter + "_" + image.FileName))
{
stream.CopyTo(filezstream);
}
}
counter++;
}
}
Console.WriteLine("\n -> Image Extraction Path: " + DirectoryName);
}
public static void ExtractText(string onepath, string exportdirectory)
{
Document OneNoteFile = new Document(onepath);
string DirectoryName = exportdirectory + "\\OneNoteText";
if (!Directory.Exists(DirectoryName))
{
Directory.CreateDirectory(DirectoryName);
}
Console.WriteLine("\n -> Extracted OneNote Document Text: \n");
var counter = 1;
foreach (Page page in OneNoteFile)
{
string pagename = page.CachedTitleString;
string parsedfilename = string.Join("_", pagename.Split(Path.GetInvalidFileNameChars()));
string pagepath = DirectoryName + "\\" + counter + "_" + parsedfilename + ".txt";
using (StreamWriter textwritten = new StreamWriter(pagepath))
{
string textonenote = string.Join(Environment.NewLine, page.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
textwritten.WriteLine(textonenote);
Console.WriteLine(" -> Page: " + page.CachedTitleString + " | Extraction Path: " + pagepath);
}
counter++;
}
}
public static void ExtractHyperLink(string onepath, string exportdirectory)
{
Document OneNoteFile = new Document(onepath);
string DirectoryName = exportdirectory + "\\OneNoteHyperLinks";
if (!Directory.Exists(DirectoryName))
{
Directory.CreateDirectory(DirectoryName);
}
string pagepathlink = DirectoryName + "\\onenote_hyperlinks.txt";
Console.WriteLine("\n -> Extracted OneNote Document HyperLinks: (Note: Text might contain hyperlink if no overlay) ");
foreach (Page page in OneNoteFile)
{
IList<RichText> richtextlist = page.GetChildNodes<RichText>();
string line1 = "\n -> Page: " + page.CachedTitleString + "\n";
Console.WriteLine(line1);
using (StreamWriter linktext = new StreamWriter(pagepathlink, append: true))
{
linktext.WriteLine(line1);
}
foreach (RichText Textval in richtextlist)
{
string line2 = " -> Text: " + Textval.Text;
Console.WriteLine(line2);
using (StreamWriter linktext = new StreamWriter(pagepathlink, append: true))
{
linktext.WriteLine(line2);
}
foreach (TextStyle style in Textval.Styles)
{
if (style.HyperlinkAddress == null)
{
}
else
{
string line3 = "\n [*] Contains HyperLink: " + style.HyperlinkAddress + " \n";
Console.WriteLine(line3);
using (StreamWriter linktext = new StreamWriter(pagepathlink, append: true))
{
linktext.WriteLine(line3);
}
}
}
}
}
Console.WriteLine("\n -> HyperLink Extraction Path: " + pagepathlink);
}
public static void ConvertToImage(string onepath, string exportdirectory)
{
Document OneNoteFile = new Document(onepath);
string DirectoryName = exportdirectory;
string FileNameExt = Path.GetFileNameWithoutExtension(onepath);
string finaldirectory = DirectoryName + "\\ConvertImage_" + FileNameExt + ".png";
// Save the document as gif.
OneNoteFile.Save(finaldirectory, SaveFormat.Png);
Console.WriteLine("\n -> Saved Path: " + finaldirectory);
}
public static string CheckFileFormat(string onepath)
{
try
{
Document OneNoteFile = new Document(onepath);
Console.WriteLine("[+] OneNote Document File Format: " + OneNoteFile.FileFormat);
if (OneNoteFile.FileFormat == FileFormat.Unknown)
{
Console.WriteLine("[-] OneNote File Format Not Supported");
System.Environment.Exit(1);
return null;
}
else
{
string filenamewoext = Path.GetFileNameWithoutExtension(onepath);
string ContentDirectoryName = Path.GetDirectoryName(onepath) + "\\" + filenamewoext + "_content";
if (!Directory.Exists(ContentDirectoryName))
{
Console.WriteLine("[+] Export Directory Path: " + ContentDirectoryName);
Directory.CreateDirectory(ContentDirectoryName);
}
return ContentDirectoryName;
}
}
catch (FileCorruptedException ex)
{
Console.WriteLine("\n[-] Corrupted OneNote Document!");
Console.WriteLine("[-] Export/SaveAs the OneNote Document from OneNote 2016 - This will solve this issue!");
System.Environment.Exit(1);
return null;
}
}
static void Main(string[] args)
{
Console.WriteLine(@"
________ _______ __ _____ .__
\_____ \ ____ ____ \ \ _____/ |_ ____ / _ \ ____ _____ | | ___.__.________ ___________
/ | \ / \_/ __ \ / | \ / _ \ __\/ __ \ / /_\ \ / \\__ \ | |< | |\___ // __ \_ __ \
/ | \ | \ ___// | ( <_> ) | \ ___// | \ | \/ __ \| |_\___ | / /\ ___/| | \/
\_______ /___| /\___ >____|__ /\____/|__| \___ >____|__ /___| (____ /____/ ____|/_____ \\___ >__|
\/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/
Author: @knight0x07
");
if (args == null || args.Length == 0)
{
Console.WriteLine("\n[-] Error: No Arguments Passed");
Console.WriteLine("[-] Usage: OneNoteAnalyzer.exe --file \"<path_to_onenote_document>\"");
}
else
{
if (args[0] == "--help")
{
Console.WriteLine("\n[-] Usage: OneNoteAnalyzer.exe --file \"<path_to_onenote_document>\"");
}
else if (args[0] == "--file")
{
string FilePath = args[1];
Console.WriteLine("\n[+] OneNote Document Path: " + FilePath);
if (File.Exists(FilePath))
{
string exportdirectory = CheckFileFormat(FilePath);
Console.WriteLine("[+] Extracting Attachments from OneNote Document");
ExtractAttachment(FilePath, exportdirectory);
Console.WriteLine("\n[+] Extracting Page MetaData from OneNote Document");
ExtractMetaData(FilePath);
Console.WriteLine("\n[+] Extracting Images from OneNote Document");
ExtractImages(FilePath, exportdirectory);
Console.WriteLine("\n[+] Extracting Text from OneNote Document");
ExtractText(FilePath, exportdirectory);
Console.WriteLine("\n[+] Extracting HyperLinks from OneNote Document");
ExtractHyperLink(FilePath, exportdirectory);
Console.WriteLine("\n[+] Converting OneNote Document to Image");
ConvertToImage(FilePath, exportdirectory);
}
else
{
Console.WriteLine("[-] Eror: Invalid OneNote Document Path");
}
}
else
{
Console.WriteLine("\n[-] Error: Invalid Arguments");
Console.WriteLine("[-] Usage: OneNoteAnalyzer.exe --file \"<path_to_onenote_document>\"");
}
}
}
}
}