Jump to content

Recommended Posts

Posted

Whenever we try to access a network share it will prompt for credentials based on the shared folder settings.

Like the below prompt

d1.PNG

d2.PNG

Once you save them, they are saved in Windows Credentials of Credentials Manager in Control panel (run --> control keymgr.dll).

d3.PNG

 

But not able to found exact file or registry for this setting.

 

So, can anyone suggest how to do this process of adding network credentials using AutoIT.

 

Posted (edited)

Easy... put a file .bat on C:\\ .... and execute it on windows load..."Autostart". This is an old trick

Edited by rootx
Posted
  On 12/15/2016 at 7:29 AM, rootx said:

Easy... put a file .bat on C:\\ .... and execute it on windows load..."Autostart". This is an old trick

Expand  

But what we need to keep in the batch file is the question then.

Because I don't find any command line options to access the credential manager

Posted

Signature - my forum contributions:

  Reveal hidden contents

 

Posted
  On 12/15/2016 at 9:19 AM, orbs said:
Expand  

Thank you very much.

Don't know why it didn't came in Google search :)

To add credentials..
 

cmdkey /add:sharedmachine /user:domainname\username /pass:password

To remove credentials.

cmdkey /delete:sharedmachine

I will use them using run command to execute in my code.

Posted
  On 12/15/2016 at 9:13 AM, ur said:

But what we need to keep in the batch file is the question then.

Because I don't find any command line options to access the credential manager

Expand  

First learn https://technet.microsoft.com/en-us/library/bb490717.aspx

Second....

local user net use d:\\server\share /user:Dan  password     or net use d:\\server\share /user:Accounts\Dan password

Domain net use d:\\server\share /user:DOMAIN\Dan password

Then to have persistent add /persistent:yes

Posted
  On 12/15/2016 at 9:51 AM, rootx said:

First learn https://technet.microsoft.com/en-us/library/bb490717.aspx

Second....

local user net use d:\\server\share /user:Dan  password     or net use d:\\server\share /user:Accounts\Dan password

Domain net use d:\\server\share /user:DOMAIN\Dan password

Then to have persistent add /persistent:yes

Expand  

I am getting the below error when I try the net use

 

System error 67 has occurred.

The network name cannot be found.

Posted
  On 12/15/2016 at 10:04 AM, rootx said:

net use Z: \\ipserver\ /user:\username password

Expand  

ok..for mapping the network drives right..

But I need to access the network share directly as the number of shares are more than 26 :)

The cmdkey is working.I am able to access the share once I add the credentials to credential manager.

Posted (edited)
  On 12/15/2016 at 10:13 AM, jguinch said:

An AutoIt code answer (because it's a forum about AutoIt) :

DriveMapAdd("", "\\ukreddy-e7470\c$", 0, "username", "password")

 

Expand  
DriveMapAdd("", "\\ukreddy-e7470\c$", 1, "username", "password")

persistent. and require #RequireAdmin

Edited by rootx
Posted

Hi ur,

I prefer not to rely on external programs/tools so let me share an alternative approach to cmdkey.exe. I didn't write this, but I modified it to work for my needs. In my script I actually encrypt the password prior to storing it. Hopefully, this will make it easier for you.

 

#include <Array.au3>

$User = "user1"
$Password = "password1"
$AppName = "My App"
$Comment = "Used For ABC"

_Cred_Write($User, $Password) ; Write to Crediential Manager

$aArray = _Cred_Read() ; Read from Crediential Manager
_ArrayDisplay($aArray)

_Cred_Delete() ; Delete Credientials


Func _Cred_Write($User, $Password, $iPersist = 1, $Target = $AppName, $Comm = $Comment)
    ; iPersist
    ; SESSION = 1
    ; LOCAL_MACHINE = 2
    ; ENTERPRISE = 3

    Local $Comment = DllStructCreate("wchar[100]")
    DllStructSetData($Comment, 1, $Comm)

    Local $targetName = DllStructCreate("wchar[100]")
    DllStructSetData($targetName, 1, $Target)

    Local $userName = DllStructCreate("wchar[100]")
    DllStructSetData($userName, 1, $User)

    Local $credentialBlob = DllStructCreate("wchar[100]")
    DllStructSetData($credentialBlob, 1, $Password)

    Local $structCREDENTIAL = "" & _
            "DWORD Flags;" & _
            "DWORD Type;" & _
            "Ptr TargetName;" & _
            "Ptr Comment;" & _
            "UINT64 LastWritten;" & _
            "DWORD CredintialBlobSize;" & _
            "Ptr CredentialBlob;" & _
            "DWORD Persist;" & _
            "DWORD AttributeCount;" & _
            "ptr Attributes;" & _
            "Ptr TargetAlias;" & _
            "Ptr Username"

    Local $NewCred = DllStructCreate($structCREDENTIAL)
    If @error Then
        MsgBox(0, "NewCred", "Error in DllStructCreate " & @error) ;
        Exit
    EndIf

    DllStructSetData($NewCred, "Flags", 0)
    DllStructSetData($NewCred, "Type", 1)
    DllStructSetData($NewCred, "TargetName", DllStructGetPtr($targetName))
    DllStructSetData($NewCred, "Persist", $iPersist)
    DllStructSetData($NewCred, "AttributeCount", 0)
    DllStructSetData($NewCred, "UserName", DllStructGetPtr($userName))
    DllStructSetData($NewCred, "CredentialBlob", DllStructGetPtr($credentialBlob))
    DllStructSetData($NewCred, "CredintialBlobSize", StringLen($Password) * 2)
    DllStructSetData($NewCred, "Comment", DllStructGetPtr($Comment))

    #comments-start
        MsgBox(0, "DllStruct", "Data:" & @CRLF & _
        "Flags: " & DllStructGetData($NewCred, "Flags") & @CRLF & _
        "Type: " & DllStructGetData($NewCred,"Type") & @CRLF & _
        "TargetName: " &  DllStructGetData($NewCred,"TargetName") & @CRLF & _
        "Persist: " & DllStructGetData($NewCred,"Persist") & @CRLF & _
        "AttributeCount: " &  DllStructGetData($NewCred,"AttributeCount") & @CRLF & _
        "UserName: " &  DllStructGetData($NewCred,"UserName") & @CRLF & _
        "CredentialBlob: " &  DllStructGetData($NewCred,"CredentialBlob") & @CRLF & _
        "CredintialBlobSize: " &  DllStructGetData($NewCred,"CredintialBlobSize") & @CRLF & _
        "Comment: " &  DllStructGetData($NewCred,"Comment"))
    #comments-end

    Local $hAdvapi32 = DllOpen("Advapi32.dll")
    If @error Then
        MsgBox(0, "Error", "Cannot open Advapi32.dll")
        Exit
    EndIf
    Local $aRet = DllCall($hAdvapi32, 'bool', 'CredWriteW', 'ptr', DllStructGetPtr($NewCred), 'dword', 0)

    $NewCred = 0
EndFunc

Func _Cred_Read($Target = $AppName)
    Local $aFuncRet[3]
    Local $iType = 1 ; 1 = Generic | 2 = Domain | 3 = Certificate

    Local $targetName = DllStructCreate("wchar[100]")
    DllStructSetData($targetName, 1, $Target)

    Local $hAdvapi32 = DllOpen("Advapi32.dll")
    Local $aRet = DllCall($hAdvapi32, 'bool', 'CredReadW', 'ptr', DllStructGetPtr($targetName), 'dword', $iType, 'dword', 0, 'ptr*', 0)

    If $aRet[0] = 0 Then Return SetError(1, 0)

    Local $structCREDENTIAL = "" & _
            "DWORD Flags;" & _
            "DWORD Type;" & _
            "Ptr TargetName;" & _
            "Ptr Comment;" & _
            "UINT64 LastWritten;" & _
            "DWORD CredintialBlobSize;" & _
            "Ptr CredentialBlob;" & _
            "DWORD Persist;" & _
            "DWORD AttributeCount;" & _
            "Ptr Attributes;" & _
            "Ptr TargetAlias;" & _
            "Ptr Username"

    Local $tdata = DllStructCreate($structCREDENTIAL, $aRet[4])

    Local $userName = DllStructCreate("wchar[100]", DllStructGetData($tdata, 'Username'))
    Local $sUser = DllStructGetData($userName, 1)

    Local $CredentialBlobSize = DllStructGetData($tdata, 'CredintialBlobSize')
    Local $credentialBlob = DllStructCreate("wchar[100]", DllStructGetData($tdata, 'CredentialBlob'))
    Local $sPassword = StringLeft(DllStructGetData($credentialBlob, 1), $CredentialBlobSize / 2)

    Local $Comment = DllStructCreate("wchar[100]", DllStructGetData($tdata, 'Comment'))
    Local $sComm = DllStructGetData($Comment, 1)

    Dim $aFuncRet[] = [$sUser, $sPassword, $sComm]
    Return $aFuncRet
EndFunc

Func _Cred_Delete($Target = $AppName)
    Local $aRet
    Local $targetName = DllStructCreate("wchar[100]")
    DllStructSetData($targetName, 1, $Target)

    Local $hAdvapi32 = DllOpen("Advapi32.dll")
    $aRet = DllCall($hAdvapi32, 'bool', 'CredDeleteW', 'ptr', DllStructGetPtr($targetName), 'dword', 1, 'dword', 0)
EndFunc

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...