AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi CredIsProtected buffer-overflow?
Thema durchsuchen
Ansicht
Themen-Optionen

CredIsProtected buffer-overflow?

Ein Thema von himitsu · begonnen am 1. Mär 2024 · letzter Beitrag vom 4. Mär 2024
Antwort Antwort
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#1

CredIsProtected buffer-overflow?

  Alt 1. Mär 2024, 12:45
Moin, ich/wir spielen grade etwas mit den Windows Credentials.

Nun wollte ich noch CredProtect, CredUnprotect und CredIsProtected hinzufügen,
aber ... nja, CredIsProtected sollte doch eigentlich nur lesen? Aber es schreibt den Speicher nahezu willkührlich um.

vorab:
* Delphi XE bis D12
* mit oder ohne WinMD (ab D11, siehe GetIt)
* nja, vom WinMD das, was sich halbwegs nutzen lässt (was nicht viel ist und das nur unter qualvollen Mühen)

Im Anhang meine kleine Demo/TestApp.
* einmal auf WriteToCrendentialsStore ODER unten den Filter z.B. auf * ändern
* und dann ReadFromCredentialsStore FindInCredentialsStore

* vorher in WinCrypt.FindInCredentialsStore bzw. WinCrypt.ReadFromCredentialsStore dn Block mit CredIsProtected entkommentieren und Haltepunkt auf CredIsProtected
* danach werden während CredIsProtected die "mindestens" Variablen PCred und manchmal auch Creds verändert
* selbst wenn die Funktion schreibt. hätte ich maximal in PCred.CredentialBlob eine Änderung erwartet
* und nachfolgend knallen dann natürlich alle Zugriffe auf PCred.xxxx


Löschen des eventuell Erstellten wieder über die DemoApp
* DeleteInCredentialsStore
oder im Windows
* OpenCredentialManager
* control.exe /name Microsoft.CredentialManager
* [WIN] Anmeldeinformationsverwalrung
* * rechts, unter Windows-Anmeldeinfos
Angehängte Dateien
Dateityp: 7z CryptDemo.7z (105,7 KB, 2x aufgerufen)
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
213 Beiträge
 
#2

AW: CredIsProtected buffer-overflow?

  Alt 2. Mär 2024, 13:40
Hi,

Yesterday i looked and doubted myself, now i tried again to understand the subject at hand , using my ability to decryption of google translation, i am assuming the problem is with overflowing somewhere.
2024-03-02-15_26_02-delphi-credisprotected-buffer-overflow_-delphi-praxis.png

Anyway there is two problems, separated that caused this

1) Without defining enum size the to be compatible with Windows API's it will break
Code:
  {$MINENUMSIZE 4}
  CRED_PROTECTION_TYPE = (CredUnprotected, CredUserProtection, CredTrustedProtection, CredForSystemProtection);
2) The definition of both CredProtect and CredUnProtect is wrong in that demo, and they should looks like this:
Code:
BOOL CredProtectW(
  [in]     BOOL                fAsSelf,
  [in]     LPWSTR              pszCredentials,
  [in]     DWORD               cchCredentials,
  [out]    LPWSTR              pszProtectedCredentials,
  [in, out] DWORD               *pcchMaxChars,
  [out]    CRED_PROTECTION_TYPE *ProtectionType
);
BOOL CredUnprotectW(
  [in]     BOOL  fAsSelf,
  [in]     LPWSTR pszProtectedCredentials,
  [in]     DWORD cchProtectedCredentials,
  [out]    LPWSTR pszCredentials,
  [in, out] DWORD *pcchMaxChars
);

function CredProtect(fAsSelf: BOOL; pszCredentials: LPWSTR; cchCredentials: DWORD; out pszProtectedCredentials: LPWSTR; var pcchMaxChars: DWORD; out ProtectionType: CRED_PROTECTION_TYPE): BOOL; stdcall; external advapi32 name 'CredProtectW';
function CredUnprotect(fAsSelf: BOOL; pszProtectedCredentials: LPWSTR; cchProtectedCredentials: DWORD; out pszCredentials: LPWSTR; var pcchMaxChars: DWORD):  BOOL; stdcall; external advapi32 name 'CredUnprotectW';
and bonus (3), after checking the result of CredIsProtected, in this case or any similar, always set zero to buffer size for the first call, then call once, then check GetLastError for ERROR_INSUFFICIENT_BUFFER, there is no need to check for the result of the function itself in first call, and you have the needed in pcchMaxChars after the second, here again it is always as rule of thumb best to trim the buffer again.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#3

AW: CredIsProtected buffer-overflow?

  Alt 2. Mär 2024, 13:53
Ahhhhhh crap, I forgot to take the {$MINENUMSIZE 4} with me when moving to an external unit.
I've been looking everywhere for days, but I hadn't looked for this.
I thought I had made a mistake in the translation somewhere or was carrying out the call incorrectly.


Bei vielen API-Implementation, von Embarcadero und auch im neuen WinMD, muß man echt aufpassen, da Diese oft einfach nur noch aus Fehlern bestehen.
z.B. Winapi.WinCred, Soap.Win.CertHelper (im Delphi) oder Windows.Foundation, Windows.Security.Credentials und Windows.Security.Cryptography (im WinMD)

Code:
Windows.Security.Credentials : CredReadW
out Credential: PCREDENTIALW
NOT
out Credential: CREDENTIALW
-
[out] PCREDENTIALW *Credential

Windows.Security.Credentials : CredEnumerateW
out Credential: PPCREDENTIAL
NOT
out Credential: CREDENTIALW
.
[out] PCREDENTIALW **Credential

Windows.Security.Credentials : CredUnPackAuthenticationBuffer
pszUserName: LPWSTR
NOT
{out} pszUserName: PPWSTR
-
[out] LPSTR pszUserName
https://www.delphipraxis.net/214473-...ml#post1532976
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
213 Beiträge
 
#4

AW: CredIsProtected buffer-overflow?

  Alt 2. Mär 2024, 14:02
I just want to know one thing

How do you manage to mess up page translate in my Chrome ? HOW ?

%90 of failure in translation or messed up pages here in this forum are from your posts !!!
2024-03-02-15_58_28-delphi-credisprotected-buffer-overflow_-delphi-praxis.png

That is really a talent, and i want it.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#5

AW: CredIsProtected buffer-overflow?

  Alt 2. Mär 2024, 14:23
I don't use the translator in Firefox either,
But at least Google's online translator can handle it quite well when different languages appear together.

https://translate.google.de
and
https://www-delphipraxis-net.transla...pp#post1534086

Many years ago, the Android app was able to find and translate texts in photos and images.

I would have guessed that the translator could do something like that in their own browser.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#6

AW: CredIsProtected buffer-overflow?

  Alt 2. Mär 2024, 17:45
Thanks again for your help.
Sometimes I just have tomatoes on the eyes.

At first I thought that CredProtect was applied to the entire PCREDENTIAL, but at some point I found out that it was only applied to the CredentialBlob from the few examples that could be found on the internet.
The way I see it, you could also apply it to the attributes, but in the main data it's enough for me for now.

Now I will probably introduce a class-var for CREDENTIAL.Persist to be able to control when writing whether it should be saved locally or in the Windows-Domain. At the moment, local is enough for us here.
Code:
CRED_PERSIST_NONE         = 0;
CRED_PERSIST_SESSION      = 1;
CRED_PERSIST_LOCAL_MACHINE = 2;
CRED_PERSIST_ENTERPRISE   = 3;
Miniaturansicht angehängter Grafiken
screenshot-2024-03-02-183450.png  
Angehängte Dateien
Dateityp: 7z WinCrypt.7z (105,0 KB, 2x aufgerufen)
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
213 Beiträge
 
#7

AW: CredIsProtected buffer-overflow?

  Alt 3. Mär 2024, 08:06
You are welcome and thank you for sharing this.

A question though :

Where and how to get the scope of the credential ? (this field in screenshot)
2024-03-03-09_56_45-credential-manager.png

ps: there is exceptions due a conflict in WriteToCredentialStore with "Demo ABC"
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#8

AW: CredIsProtected buffer-overflow?

  Alt 3. Mär 2024, 12:30
When reading, currently unimplemented fields were saved in "Credential.Others".
and filled with hard default values when writing.

Yes, I left some things out of the parameters,
but recently at least moved from hard-coded defaults to class vars.
TCryptCredential.DefaultPersist := ccpEnterprise; I'm currently in the process of moving this to GitHub.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu ( 3. Mär 2024 um 12:33 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#9

AW: CredIsProtected buffer-overflow?

  Alt 4. Mär 2024, 03:31
https://github.com/geheimniswelten/WinCryptCred
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:44 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz