Jump to content

Setting RAS IPSec Preshared key


Recommended Posts

I am not an expert but I have written code to change the IPSec preshared key password for L2TP / Automatic Vpn connection. Pls see code below

Func SetRASKey()
    $RASCREDENTIALS=DllStructCreate("dword dwSize;dword dwMask;char szUserName[256];char szPassword[256];char szDomain[16]") 
    If @error Then
        Return(False)
    EndIf
    DllStructSetData($RASCREDENTIALS,"dwMask",16)
    DllStructSetData($RASCREDENTIALS,"szUserName","")
    DllStructSetData($RASCREDENTIALS,"szPassword",<PreShared Key>)
    DllStructSetData($RASCREDENTIALS,"szDomain","")
    DllStructSetData($RASCREDENTIALS,"dwSize",DllStructGetSize($RASCREDENTIALS))
    $ret=DllCall("rasapi32.dll","dword","RasSetCredentialsA","str","","str",<VPN Name>,"ptr",DllStructGetPtr($RASCREDENTIALS),"long",0)

    If Not @error Then
        If $ret[0] <> 0 Then
            Return(False)
        Else
            Return(True)
            EndIf
    Else
        Return(False)
    EndIf
 EndFunc

As per MS Documentation the function returns 0 if successful and any non zero number error if failed. I correctly get this as 0 when parameters are correct and non zero when I change anything, but still the preshared key for the VPN I created in the Network Connections in control panel does not change..any ideas..??

I think we need to have RAS API translated into AutoIT in future versions as its very difficult otherwise

P.S the 16 passed to dwMask to tell the function that Preshared key is to be set otherwise it sets the username password domain for that connection.

You can see Microsoft doc at http://msdn.microsoft.com/en-us/library/aa377811(VS.85).aspx

for more info on this function.Its used to set the Preshared Key/Username credentials for a connection entry(usually VPN) in the Network Connections panel in Windows control panel)

any help to make this function work would be greatly appreciated.

Edited by Vinu
Link to comment
Share on other sites

Ok I progressed to this point. This code seems to work but it does'nt completely. The issue is that the GetDllStructSize does not return correct size so I've passed size as 540 (constant). This does'nt throw up error but still the password is not set correctly ...any one with better ideas ??? ..Please help

Func SetRASKey()
    $RASCREDENTIALS=DllStructCreate("dword dwSize;dword dwMask;char szUserName[257];char szPassword[257];char szDomain[16]")

    If @error Then
        Return(False)
    EndIf
    DllStructSetData($RASCREDENTIALS,"dwMask",16)
    DllStructSetData($RASCREDENTIALS,"szUserName","")
    DllStructSetData($RASCREDENTIALS,"szPassword",<Presharedkey> & Chr(0))
    DllStructSetData($RASCREDENTIALS,"szDomain","")
    DllStructSetData($RASCREDENTIALS,"dwSize",540)
    $ret=DllCall("rasapi32.dll","dword","RasSetCredentialsA","str","","str",<VPN Name>,"ptr",DllStructGetPtr($RASCREDENTIALS),"long",0)
    
    If Not @error Then
        If $ret[0] <> 0 Then
            Return(False)
        Else
            Return(True)    
        EndIf
    Else
        Return(False)
    EndIf
EndFunc
Link to comment
Share on other sites

I am not an expert but I have written code to change the IPSec preshared key password for L2TP / Automatic Vpn connection. Pls see code below

Func SetRASKey()
    $RASCREDENTIALS=DllStructCreate("dword dwSize;dword dwMask;char szUserName[256];char szPassword[256];char szDomain[16]") 
    If @error Then
        Return(False)
    EndIf
    DllStructSetData($RASCREDENTIALS,"dwMask",16)
    DllStructSetData($RASCREDENTIALS,"szUserName","")
    DllStructSetData($RASCREDENTIALS,"szPassword",<PreShared Key>)
    DllStructSetData($RASCREDENTIALS,"szDomain","")
    DllStructSetData($RASCREDENTIALS,"dwSize",DllStructGetSize($RASCREDENTIALS))
    $ret=DllCall("rasapi32.dll","dword","RasSetCredentialsA","str","","str",<VPN Name>,"ptr",DllStructGetPtr($RASCREDENTIALS),"long",0)

    If Not @error Then
        If $ret[0] <> 0 Then
            Return(False)
        Else
            Return(True)
            EndIf
    Else
        Return(False)
    EndIf
 EndFunc

As per MS Documentation the function returns 0 if successful and any non zero number error if failed. I correctly get this as 0 when parameters are correct and non zero when I change anything, but still the preshared key for the VPN I created in the Network Connections in control panel does not change..any ideas..??

I think we need to have RAS API translated into AutoIT in future versions as its very difficult otherwise

P.S the 16 passed to dwMask to tell the function that Preshared key is to be set otherwise it sets the username password domain for that connection.

You can see Microsoft doc at http://msdn.microsoft.com/en-us/library/aa377811(VS.85).aspx

for more info on this function.Its used to set the Preshared Key/Username credentials for a connection entry(usually VPN) in the Network Connections panel in Windows control panel)

any help to make this function work would be greatly appreciated.

Ok...Got it working finally. There is an entry called AuthRestrictions in rasphone.pbk(the file where connection details of Network conenctions are stored.It has the format of an .ini file) we should set this to 608 for L2TP and automatic protocol. Only then preshared key setting will be correctly used. I a mwriting this in case anyone has a similar issue they can apply the same settings

Link to comment
Share on other sites

  • 1 year later...

Hi,

I know this is a rather old post .. but does someone know the solution.. i'm unable to get a positive result

I modified the Function in order to support Unicode (RasSetCredentialsW instead of RasSetCredentialsA)but still i'm unable to change the PreSharedKey

RasApi Error Codes

Func SetRASKey()

    $RASCREDENTIALS = DllStructCreate("dword dwSize;dword dwMask;char szUserName[256];char szPassword[256];char szDomain[16]")
    If @error Then
        Return (False)
    EndIf
    DllStructSetData($RASCREDENTIALS, "dwMask", 16)
    DllStructSetData($RASCREDENTIALS, "szUserName", "")
    DllStructSetData($RASCREDENTIALS, "szPassword", <PreSharedKey>)
    DllStructSetData($RASCREDENTIALS, "szDomain", "")
    DllStructSetData($RASCREDENTIALS, "dwSize", DllStructGetSize($RASCREDENTIALS))
    $ret = DllCall("rasapi32.dll", "dword", "RasSetCredentialsW", "str", "", "str", <VPNEntry>, "ptr", DllStructGetPtr $RASCREDENTIALS), "long", 0)

    If Not @error Then
        If $ret[0] <> 0 Then
            Return (False & " :  " & $ret[0])
        Else
            Return (True)
        EndIf
    Else
        Return (False)
    EndIf
EndFunc   ;==>SetRASKey

$Test = SetRASKey ()
Msgbox (0, "test", $Test)

Best regards,Emiel Wieldraaijer

Link to comment
Share on other sites

  • 1 month later...

I have code

Func SetRASKey()
    $RASCREDENTIALS = DllStructCreate("dword dwSize;dword dwMask;char szUserName[257];char szPassword[257];char szDomain[16]")
    If @error Then 
        Return (False)     
    EndIf     
    DllStructSetData($RASCREDENTIALS, "dwMask", 16)     
    DllStructSetData($RASCREDENTIALS, "szUserName", "")     
    DllStructSetData($RASCREDENTIALS, "szPassword","1234")     
    DllStructSetData($RASCREDENTIALS, "szDomain", "IP number")     
    DllStructSetData($RASCREDENTIALS, "dwSize", DllStructGetSize($RASCREDENTIALS))     
    $ret=DllCall("rasapi32.dll","dword","RasSetCredentialsA","str","","str","VPN Connection","ptr",DllStructGetPtr($RASCREDENTIALS),"long",0)
    If Not @error Then
        If $ret[0] <> 0 Then            
            Return (False & " :  " & $ret[0])         
        Else             
            Return (True)        
        EndIf     
    Else         
        Return (False)     
    EndIf 
EndFunc   ;==>SetRASKey 
$Test = SetRASKey () 
Msgbox (0, "test", $Test)

me when you run codes got error message 621(Cannot open the phone book file.),

help solution, please

thanks

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...