Files
CookieRetrieval/CookiePython/Program.cs
2023-02-25 10:22:30 +01:00

88 lines
3.2 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System;
using System.Reflection;
namespace CookiePython
{
internal class Program
{
static void Main(string[] args)
{
List<Cookie> cookies = GetChromeCookies();
foreach (Cookie c in cookies)
{
Console.WriteLine(c.HostKey);
Console.WriteLine(c.Name);
Console.WriteLine(c.Value);
}
Console.ReadLine();
}
public class Cookie
{
public string HostKey { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public string Path { get; set; }
public DateTime ExpiresUtc { get; set; }
public bool IsSecure { get; set; }
public bool IsHttpOnly { get; set; }
}
public static List<Cookie> GetChromeCookies()
{
string scriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "recoverCookiesCSV.py");
//string scriptPath = "C:/users/Moi/Desktop/recoverCookiesCSV.py";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "python.exe";
start.Arguments = scriptPath;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
Process process = Process.Start(start);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
List<Cookie> cookies = new List<Cookie>();
using (var reader = new StreamReader("cookies.csv"))
{
// Read the header row and validate column order
string header = reader.ReadLine();
string[] expectedColumns = { "host_key", "name", "value", "path", "expires_utc", "is_secure", "is_httponly" };
string[] actualColumns = header.Split(',');
for (int i = 0; i < expectedColumns.Length; i++)
{
if (expectedColumns[i] != actualColumns[i])
{
throw new InvalidOperationException($"Expected column '{expectedColumns[i]}' at index {i} but found '{actualColumns[i]}'");
}
}
// Read each data row and parse values into a Cookie object
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] fields = line.Split(',');
Cookie cookie = new Cookie();
cookie.HostKey = fields[0];
cookie.Name = fields[1];
cookie.Value = fields[2];
cookie.Path = fields[3];
cookie.ExpiresUtc = new DateTime();
if (long.TryParse(fields[4], out long expiresUtcTicks))
cookie.ExpiresUtc = new DateTime(expiresUtcTicks, DateTimeKind.Utc);
cookies.Add(cookie);
}
}
return cookies;
}
}
}