Windows CredEnumerate in JNA

不羁的心 提交于 2021-01-28 03:51:46

问题


I am trying to use JNA to read credentials stored in Windows Credential Manager (Control Panel > Credential Manager). Found the c function for it (CredEnumerateW) which seems to run ok as it returns "true" and I am able to get the number of credentials stored in the Credential Manager (pCount.getValue() in my code).

The problem is I do not know how to read the credentials data. The structure which holds the credentials(_CREDENTIAL) contains other 2 structures inside (FILETIME and CREDENTIAL_ATTRIBUTE). I tried to search for similar examples but could not find one matching my scenario. Please see my code below.

Can anybody help me to get this working?

public class CredEnumerate {

        /*
        typedef struct _CREDENTIAL {
              DWORD                 Flags;
              DWORD                 Type;
              LPTSTR                TargetName;
              LPTSTR                Comment;
              FILETIME              LastWritten;
              DWORD                 CredentialBlobSize;
              LPBYTE                CredentialBlob;
              DWORD                 Persist;
              DWORD                 AttributeCount;
              PCREDENTIAL_ATTRIBUTE Attributes;
              LPTSTR                TargetAlias;
              LPTSTR                UserName;
            } CREDENTIAL, *PCREDENTIAL; */
        public static class _CREDENTIAL extends Structure {
            public int                      Flags;
            public int                      Type;
            public String                   TargetName;
            public String                   Comment;
            public _FILETIME                LastWritten = new _FILETIME();
            public int                      CredentialBlobSize;
            public byte                     CredentialBlob;
            public  int                     Persist;
            public int                      AttributeCount;
            public _CREDENTIAL_ATTRIBUTE    Attributes = new _CREDENTIAL_ATTRIBUTE();
            public String                   TargetAlias;
            public String                   UserName;
        }

        /*
        typedef struct _CREDENTIAL_ATTRIBUTE {
              LPTSTR Keyword;
              DWORD  Flags;
              DWORD  ValueSize;
              LPBYTE Value;
                } CREDENTIAL_ATTRIBUTE, *PCREDENTIAL_ATTRIBUTE; */
        public static class _CREDENTIAL_ATTRIBUTE extends Structure {
            public String Keyword;
            public int  Flags;
            public int  ValueSize;
            public byte Value;
        }

        /*
        typedef struct _FILETIME {
              DWORD dwLowDateTime;
              DWORD dwHighDateTime;
            } FILETIME, *PFILETIME; */
        public static class _FILETIME extends Structure {
            public int dwLowDateTime;
            public int dwHighDateTime;
        }

        /*
        BOOL CredEnumerate( 
            _In_  LPCTSTR     Filter,
            _In_  DWORD       Flags,
            _Out_ DWORD       *Count,
            _Out_ PCREDENTIAL **Credentials) */ 
        public interface Advapi32 extends StdCallLibrary {
            Advapi32 INSTANCE = (Advapi32) Native.loadLibrary("advapi32", Advapi32.class);      
            boolean CredEnumerateW(String filter, int flags, IntByReference count, PointerByReference pref);
        }

        public static void main(String[] args) {

            IntByReference pCount = new IntByReference();
            PointerByReference pCredentials = new PointerByReference();     
            boolean result = Advapi32.INSTANCE.CredEnumerateW(null, 0, pCount, pCredentials);

            System.out.println("result: " + result);
            System.out.println("number of credentials: " +  pCount.getValue()); 

            //how to read the _CREDENTIAL structure data from pCredentials?

        }

    }

回答1:


It turned out for me that the array-of-pointers, so a pointer-to-pointers, was the only tricky thing in using CredEnumerate. This was solved with p.getPointerArray.

With the declaration of Win32 function CredEnumerateW

boolean CredEnumerateW(WString Filter, int Flags, IntByReference Count, // int ref
            PointerByReference pptr);

my solution looks like:

try {
    IntByReference count = new IntByReference();
    PointerByReference pptr = new PointerByReference();
    boolean ok = WinCrypt.INSTANCE.CredEnumerateW(null, WinCrypt.Enumerate.CRED_ENUMERATE_ALL_CREDENTIALS,
    count, pptr);
    System.out.println("count: " + count.getValue());

    CREDENTIAL[] credentials = new CREDENTIAL[count.getValue()];

    Pointer p = pptr.getValue();
    Pointer[] credPointers = p.getPointerArray(0, count.getValue());

    for (int i = 0; i < count.getValue(); i++) {
        CREDENTIAL cred = new CREDENTIAL(credPointers[i]);
        credentials[i] = cred;

        String tnameEncoded = cred.targetName.toString();
        String namespace = tnameEncoded.substring(0, tnameEncoded.indexOf(":"));
        String attributeAndTargetName = tnameEncoded.substring(tnameEncoded.indexOf(":") + 1);
        String targetName = attributeAndTargetName.substring(attributeAndTargetName.indexOf("=") + 1);

        System.out.println(namespace + " / " + targetName);

        if (namespace.indexOf("MicrosoftAccount") < 0 && namespace.indexOf("WindowsLive") < 0) {
    CREDENTIAL fullcred = readCredentials(targetName, getType(namespace), 0);
    System.out.println("  " + cred.userName);
    System.out.println("  " + fullcred.getPassword());
        }
    }

} catch (LastErrorException error) {
    int rc = error.getErrorCode();
    String errMsg = error.getMessage();
    System.out.println(rc + ": " + errMsg);
    System.exit(1);

}

and reading the credentials from a pointer address is implemented as

private static CREDENTIAL readCredentials(String targetName, int type, int flags)
throws UnsupportedEncodingException {
    try {
        PointerByReference pptr = new PointerByReference();
        boolean ok = WinCrypt.INSTANCE.CredReadW(new WString(targetName), type, flags, pptr);
        CREDENTIAL cred = new CREDENTIAL(pptr.getValue());

        return (cred);
    } catch (LastErrorException error) {
        int rc = error.getErrorCode();
        String errMsg = error.getMessage();
        System.out.println(rc + ": " + errMsg);
        throw new IllegalAccessError("cannot access windows vault for " + targetName);
    }
}

See also How to map Windows API CredWrite/CredRead in JNA? for basics about reading/writing credentials with Win32 credentials API.



来源:https://stackoverflow.com/questions/33754249/windows-credenumerate-in-jna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!