-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
308 lines (276 loc) · 14.3 KB
/
Copy pathProgram.cs
File metadata and controls
308 lines (276 loc) · 14.3 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
using SmartFileKit.Domain;
using SmartFileKit.Validation;
using SmartFileKit.Analysis;
using SmartFileKit.Security;
using SmartFileKit.Detection;
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace SmartFileKit.Sample
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("==================================================");
Console.WriteLine(" SmartFileKit Demonstration ");
Console.WriteLine("==================================================");
Console.WriteLine();
DemoFileSize();
DemoSanitizer();
DemoDetection();
DemoValidation();
DemoAnalyzer();
DemoSecurityAndMetadata();
Console.WriteLine();
Console.WriteLine("==================================================");
Console.WriteLine(" Demonstration Complete Successfully ");
Console.WriteLine("==================================================");
}
static void DemoFileSize()
{
Console.WriteLine("--- 1. File Size Formatting ---");
long bytes = 1048576 * 5; // 5 MB
FileSize size = bytes.ToFileSize();
Console.WriteLine($"Conversions for {bytes} bytes:");
Console.WriteLine($"* Megabytes: {size.Megabytes} MB");
Console.WriteLine($"* ToString(): {size.ToString()}");
Console.WriteLine($"* ToString(1): {size.ToString(1)}");
FileSize fluentSize = 250.MB();
Console.WriteLine($"Fluent size 250.MB(): {fluentSize}");
Console.WriteLine($"Addition (5MB + 250MB): {size + fluentSize}");
Console.WriteLine();
}
static void DemoSanitizer()
{
Console.WriteLine("--- 2. File Name Sanitization ---");
string dirtyName1 = "özel dosya (1).png";
string sanitized1 = FileName.Sanitize(dirtyName1);
Console.WriteLine($"Original: \"{dirtyName1}\"");
Console.WriteLine($"Sanitized: \"{sanitized1}\"");
Console.WriteLine();
string traversalName = "../../etc/passwd";
string sanitizedTraversal = FileName.Sanitize(traversalName);
Console.WriteLine($"Original (Path Traversal): \"{traversalName}\"");
Console.WriteLine($"Sanitized: \"{sanitizedTraversal}\"");
Console.WriteLine();
string reservedName = "CON.txt";
string sanitizedReserved = FileName.Sanitize(reservedName);
Console.WriteLine($"Original (Windows Reserved): \"{reservedName}\"");
Console.WriteLine($"Sanitized: \"{sanitizedReserved}\"");
Console.WriteLine();
}
static void DemoDetection()
{
Console.WriteLine("--- 3. File MIME & Category Detection ---");
// PNG Bytes
byte[] pngBytes = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
var pngFormat = FileType.GetFormat(pngBytes);
Console.WriteLine("PNG detection from byte array:");
Console.WriteLine($"* Primary Extension: {pngFormat?.Extension}");
Console.WriteLine($"* MIME Type: {pngFormat?.MimeType}");
Console.WriteLine($"* Category: {pngFormat?.Category}");
Console.WriteLine();
// Word Docx Bytes (ZIP signature + word/)
byte[] docxBytes = new byte[100];
docxBytes[0] = 0x50; docxBytes[1] = 0x4B; docxBytes[2] = 0x03; docxBytes[3] = 0x04;
byte[] wordPattern = Encoding.ASCII.GetBytes("word/");
Buffer.BlockCopy(wordPattern, 0, docxBytes, 20, wordPattern.Length);
var docxFormat = FileType.GetFormat(docxBytes);
Console.WriteLine("Office Word (.docx) detection from byte array:");
Console.WriteLine($"* Extension: {docxFormat?.Extension}");
Console.WriteLine($"* MIME Type: {docxFormat?.MimeType}");
Console.WriteLine($"* Category: {docxFormat?.Category}");
Console.WriteLine();
}
static void DemoValidation()
{
Console.WriteLine("--- 4. Fluent File Validation ---");
// Case A: Valid PNG upload
Console.WriteLine("Case A: Valid PNG upload:");
byte[] validPng = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
var resultA = FileValidator.Validate(validPng, "my_avatar.png")
.MaxSize(2.MB())
.AllowedExtensions(".png", ".jpg")
.AllowedCategories(FileCategory.Image)
.Execute();
Console.WriteLine($"* Is Valid? {resultA.IsValid}");
foreach (var err in resultA.Errors)
{
Console.WriteLine($" Error: {err}");
}
Console.WriteLine();
// Case B: Spoofed File (virus.exe renamed to holiday.jpg)
Console.WriteLine("Case B: Spoofed Upload (virus.exe renamed to holiday.jpg):");
byte[] spoofedBytes = Encoding.ASCII.GetBytes("MZ\x90\x00\x03\x00\x00\x00...");
var resultB = FileValidator.Validate(spoofedBytes, "holiday.jpg")
.MaxSize(5.MB())
.AllowedExtensions(".jpg", ".jpeg")
.AllowedCategories(FileCategory.Image)
.Execute();
Console.WriteLine($"* Is Valid? {resultB.IsValid}");
foreach (var err in resultB.Errors)
{
Console.WriteLine($" Error: {err}");
}
Console.WriteLine();
// Case C: ThrowIfInvalid usage
Console.WriteLine("Case C: Exception throwing with ThrowIfInvalid():");
try
{
FileValidator.Validate(spoofedBytes, "malicious.png")
.AllowedCategories(FileCategory.Image)
.ThrowIfInvalid();
}
catch (FileValidationException ex)
{
Console.WriteLine("Caught Expected Exception:");
Console.WriteLine(ex.Message);
}
Console.WriteLine();
}
static void DemoAnalyzer()
{
Console.WriteLine("--- 5. Advanced File Analysis & Security Engine ---");
Console.WriteLine();
// Case A: Spoofed File (EXE disguised as PDF)
byte[] spoofedBytes = new byte[100];
spoofedBytes[0] = 0x4D; spoofedBytes[1] = 0x5A; // MZ header
using (var stream = new MemoryStream(spoofedBytes))
{
var report = FileAnalyzer.Analyze(stream, "annual_report.pdf");
Console.WriteLine("Case A: Spoofed File (MZ Executable renamed to annual_report.pdf):");
Console.WriteLine($"* Is Safe? {report.IsSafe}");
Console.WriteLine($"* Is Suspicious? {report.IsSuspicious}");
Console.WriteLine($"* Risk Score: {report.RiskScore} / 100 ({report.RiskLevel})");
Console.WriteLine($"* Actual Type: {report.ActualFileType}");
foreach (var issue in report.Issues)
{
Console.WriteLine($" - [{issue.Severity}] {issue.Type}: {issue.Description}");
}
Console.WriteLine();
}
// Case B: ZIP Archive containing a hidden Executable
using (var ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var entry = archive.CreateEntry("dangerous_payload.exe");
using (var writer = new StreamWriter(entry.Open()))
{
writer.Write("MZ..."); // dummy exe
}
}
ms.Position = 0;
var report = FileAnalyzer.Analyze(ms, "documents.zip");
Console.WriteLine("Case B: ZIP Archive containing inner executable:");
Console.WriteLine($"* Is Safe? {report.IsSafe}");
Console.WriteLine($"* Risk Score: {report.RiskScore} / 100 ({report.RiskLevel})");
foreach (var issue in report.Issues)
{
Console.WriteLine($" - [{issue.Severity}] {issue.Type}: {issue.Description}");
}
Console.WriteLine();
}
// Case C: Polyglot File Detection (PDF carrying nested MZ PE executable)
byte[] polyglotBytes = new byte[1024];
polyglotBytes[0] = 0x25; polyglotBytes[1] = 0x50; polyglotBytes[2] = 0x44; polyglotBytes[3] = 0x46; // %PDF
int mzOffset = 100;
polyglotBytes[mzOffset] = 0x4D; polyglotBytes[mzOffset + 1] = 0x5A; // MZ
int peOffset = 16;
polyglotBytes[mzOffset + 0x3C] = (byte)peOffset; // PE Offset
int peStart = mzOffset + peOffset;
polyglotBytes[peStart] = 0x50; polyglotBytes[peStart + 1] = 0x45; // PE\x00\x00
using (var stream = new MemoryStream(polyglotBytes))
{
var report = FileAnalyzer.Analyze(stream, "legit.pdf");
Console.WriteLine("Case C: Polyglot File (PDF embedding MZ/PE Executable):");
Console.WriteLine($"* Is Safe? {report.IsSafe}");
Console.WriteLine($"* Is Suspicious? {report.IsSuspicious}");
Console.WriteLine($"* Risk Score: {report.RiskScore} / 100");
foreach (var issue in report.Issues)
{
Console.WriteLine($" - [{issue.Severity}] {issue.Type}: {issue.Description}");
}
Console.WriteLine();
}
// Case D: Entropy Analysis & Builder API
byte[] plainTextBytes = Encoding.UTF8.GetBytes(new string('A', 2000));
byte[] highEntropyBytes = new byte[2000];
new Random().NextBytes(highEntropyBytes);
using (var plainStream = new MemoryStream(plainTextBytes))
using (var highStream = new MemoryStream(highEntropyBytes))
{
var plainReport = FileAnalyzer.Create().CheckEntropy(true).Analyze(plainStream, "regular.txt");
var highReport = FileAnalyzer.Create().CheckEntropy(true, threshold: 7.0).Analyze(highStream, "hidden.txt");
Console.WriteLine("Case D: Entropy Analysis & Builder API:");
Console.WriteLine($"* Regular text entropy: {plainReport.Entropy:F2} (Is Suspicious: {plainReport.IsSuspicious})");
Console.WriteLine($"* Obfuscated text entropy: {highReport.Entropy:F2} (Is Suspicious: {highReport.IsSuspicious})");
if (highReport.IsSuspicious)
{
foreach (var issue in highReport.Issues)
{
Console.WriteLine($" - [{issue.Severity}] {issue.Type}: {issue.Description}");
}
}
Console.WriteLine();
}
}
static void DemoSecurityAndMetadata()
{
Console.WriteLine("--- 6. Cryptographic Hashing & Duplicate Detection ---");
byte[] fileContent = Encoding.UTF8.GetBytes("SmartFileKit Security Engine Demonstration Content");
using (var s1 = new MemoryStream(fileContent))
using (var s2 = new MemoryStream(fileContent))
{
string sha256 = FileHash.Sha256(s1);
string md5 = FileHash.Calculate(s1, HashAlgorithmType.MD5);
bool matches = DuplicateDetector.AreSame(s1, s2);
Console.WriteLine($"* SHA-256 Hash: {sha256}");
Console.WriteLine($"* MD5 Hash: {md5}");
Console.WriteLine($"* Duplicate check AreSame(): {matches}");
Console.WriteLine();
}
Console.WriteLine("--- 7. File Name Risk & Security Analysis ---");
string filenameA = "invoice.pdf.exe";
var doubleExtResult = FileSecurity.HasDoubleExtension(filenameA);
Console.WriteLine($"Filename: \"{filenameA}\"");
Console.WriteLine($"* Has Double Extension? {doubleExtResult.HasDoubleExtension}");
Console.WriteLine($"* Primary: {doubleExtResult.PrimaryExtension}, Secondary: {doubleExtResult.SecondExtension}");
Console.WriteLine($"* Is Second Extension Dangerous? {doubleExtResult.IsDangerous}");
string filenameB = "../../../CON.txt";
var pathRisk = FileSecurity.AnalyzeFileName(filenameB);
Console.WriteLine($"Filename: \"{filenameB}\"");
Console.WriteLine($"* Risk Score: {pathRisk.RiskScore} / 100");
foreach (var issue in pathRisk.Issues)
{
Console.WriteLine($" - {issue}");
}
Console.WriteLine();
Console.WriteLine("--- 8. Image & Office Metadata Readers ---");
// Mock PNG image
byte[] mockPng = new byte[30];
mockPng[0] = 0x89; mockPng[1] = 0x50; mockPng[2] = 0x4E; mockPng[3] = 0x47;
mockPng[4] = 0x0D; mockPng[5] = 0x0A; mockPng[6] = 0x1A; mockPng[7] = 0x0A;
mockPng[12] = 0x49; mockPng[13] = 0x48; mockPng[14] = 0x44; mockPng[15] = 0x52; // IHDR
// Width 1280 (0x0500), Height 720 (0x02D0)
mockPng[16] = 0x00; mockPng[17] = 0x00; mockPng[18] = 0x05; mockPng[19] = 0x00; // width 1280
mockPng[20] = 0x00; mockPng[21] = 0x00; mockPng[22] = 0x02; mockPng[23] = 0xD0; // height 720
using (var stream = new MemoryStream(mockPng))
{
var meta = ImageMetadata.Read(stream);
if (meta.IsValid)
{
Console.WriteLine($"Image Metadata: Format={meta.Format}, Resolution={meta.Width}x{meta.Height}");
}
}
Console.WriteLine();
Console.WriteLine("--- 9. File Signature Database Registry ---");
var databaseJson = FileSignatureDatabase.ExportJson();
Console.WriteLine($"* JSON Database Registry (Snippet):\n{databaseJson.Substring(0, Math.Min(250, databaseJson.Length))}...");
Console.WriteLine();
}
}
}