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
| using System.Globalization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using PuppeteerSharp;
var builder = WebApplication.CreateSlimBuilder(args);
var app = builder.Build();
List<ImageInfo> images = [
new("4e668ad2-14bd-4d28-b8f7-6572b8b3e77b", "1.jpg", "Material"),
new("3369e70b-b2d1-41db-8d0a-d78037bb8c52", "2.jpg", "White")
];
IBrowser? browser = null;
IPage? page = null;
app.MapGet("/", async () =>
Results.Content(
await File.ReadAllTextAsync("wwwroot/index.html")
, "text/html")
);
app.MapGet("/static/{*path}",async ([FromRoute] string path) =>
{
if (path.ToLower(CultureInfo.InvariantCulture).Contains("flag"))
return Results.BadRequest();
if (!File.Exists($"wwwroot/static/{path}"))
return Results.NotFound();
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(path, out var contentType))
{
contentType = "application/octet-stream";
}
return Results.Content(await File.ReadAllTextAsync($"wwwroot/static/{path}"),contentType);
});
app.MapGet("/upload", async () =>
Results.Content(
await File.ReadAllTextAsync("wwwroot/upload.html")
, "text/html")
);
app.MapPost("/report/{id:guid}", async (Guid id) =>
{
var image = images.FirstOrDefault(i => i.Id == id.ToString());
if (image == null)
return Results.NotFound("Image not found");
if (browser is null)
{
browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
ExecutablePath = "/usr/bin/chromium",
Headless = true,
Args = [
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--disable-extensions",
"--disable-software-rasterizer",
"--disable-setuid-sandbox",
"--ignore-certificate-errors",
"--media-cache-size=1",
"--disk-cache-size=1"
]
});
}
if (page is null)
{
page = await browser.NewPageAsync();
}
var flag = File.Exists("/flag") ? (await File.ReadAllTextAsync("/flag")).Trim() : "flag{no_flag}";
await page.SetCookieAsync(new CookieParam()
{
Name = "flag",
Value = flag,
Domain = "localhost",
Path = "/"
});
await page.GoToAsync($"http://localhost/#{image.Id}", 5000);
return Results.Ok("Beautiful image reported");
});
app.MapPost("/images", async ([FromForm] IFormFile file, [FromForm] string description) =>
{
if (file.Length > 0)
{
var ext = Path.GetExtension(file.FileName);
var name = Guid.NewGuid().ToString("N") + ext;
var filePath = Path.Combine("wwwroot/uploads", name);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var info = new ImageInfo(Guid.NewGuid().ToString(), name, description);
images.Add(info);
return Results.Created($"/images/{name}", info);
}
return Results.BadRequest();
}).DisableAntiforgery();
app.MapGet("/images", () => Results.Json(images));
app.MapGet("/images/{filename}", ([FromRoute] string filename) =>
{
if (filename.Contains(".."))
return Results.BadRequest();
var filePath = Path.Combine("wwwroot/uploads", filename);
if (!File.Exists(filePath))
return Results.NotFound();
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(filename, out var contentType))
{
contentType = "application/octet-stream";
}
return Results.Bytes(File.ReadAllBytes(filePath), contentType);
});
app.MapDelete("/images/{id}", (string id) =>
{
var image = images.FirstOrDefault(i => i.Id == id);
if (image == null)
return Results.NotFound("Image Not Found");
var filePath = Path.Combine("wwwroot/uploads", image.FileName);
if (File.Exists(filePath))
File.Delete(filePath);
images.Remove(image);
return Results.Ok();
});
app.Run("http://*:8081");
record ImageInfo(string Id, string FileName, string Description);
|