-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathHostCookies.cs
125 lines (115 loc) · 4.31 KB
/
HostCookies.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharpChromium
{
public class HostCookies
{
private Cookie[] _cookies;
private string _hostName;
public Cookie[] Cookies
{
get { return _cookies; }
set { _cookies = value; }
}
public string HostName
{
get { return _hostName; }
set { _hostName = value; }
}
public void Print()
{
string user = Environment.GetEnvironmentVariable("USERNAME");
Console.WriteLine("--- Chromium Cookie (User: {0}) ---", user);
Console.WriteLine("Domain : {0}", HostName);
Console.WriteLine("Cookies (JSON) :\n{0}", ToJSON());
Console.WriteLine();
}
/*
* [X] Exception: Object reference not set to an instance of an object.
at SharpChrome.Cookie.ToJSON()
at SharpChrome.HostCookies.ToJSON()
at SharpChrome.HistoricUrl.Print()
at SharpChrome.Program.Main(String[] args)
[*] Assembly 'SharpChrome' with commands 'history' completed
*/
public string ToJSON()
{
if (this.Cookies != null && this.Cookies.Length > 0)
{
List<string> jsonCookies = new List<string>();
//string[] jsonCookies = new string[this.Cookies.Length];
int j = 0;
//Console.WriteLine("Cookies length: {0}", this.Cookies.Length);
for (int i = 0; i < this.Cookies.Length; i++)
{
//Console.WriteLine("Cookie {0}: {1}", i, this.Cookies[i]);
if (this.Cookies[i] != null)
{
//this.Cookies[i].Id = j + 1;
jsonCookies.Add(this.Cookies[i].ToJSON());
j++;
}
}
return "[\n" + String.Join(",\n", jsonCookies.ToArray()) + "\n]";
}
return "";
}
public static HostCookies FilterHostCookies(HostCookies[] hostCookies, string url)
{
HostCookies results = new HostCookies();
if (hostCookies == null)
return results;
if (url == "" || url == null || url == string.Empty)
return results;
List<String> hostPermutations = new List<String>();
// First retrieve the domain from the url
string domain = url;
// determine if url or raw domain name
if (domain.IndexOf('/') != -1)
{
domain = domain.Split('/')[2];
}
results.HostName = domain;
string[] domainParts = domain.Split('.');
for (int i = 0; i < domainParts.Length; i++)
{
if ((domainParts.Length - i) < 2)
{
// We've reached the TLD. Break!
break;
}
string[] subDomainParts = new string[domainParts.Length - i];
Array.Copy(domainParts, i, subDomainParts, 0, subDomainParts.Length);
string subDomain = String.Join(".", subDomainParts);
hostPermutations.Add(subDomain);
hostPermutations.Add("." + subDomain);
}
List<Cookie> cookies = new List<Cookie>();
foreach (string sub in hostPermutations)
{
// For each permutation
foreach (HostCookies hostInstance in hostCookies)
{
// Determine if the hostname matches the subdomain perm
if (hostInstance.HostName.ToLower() == sub.ToLower())
{
// If it does, cycle through
foreach (Cookie cookieInstance in hostInstance.Cookies)
{
// No dupes
if (!cookies.Contains(cookieInstance))
{
cookies.Add(cookieInstance);
}
}
}
}
}
results.Cookies = cookies.ToArray();
return results;
}
}
}