Sign in to follow this  
Followers 0
kanishk619

Autoit doesnt always return same results

5 posts in this topic

#1 ·  Posted

The following code contains 2 functions to achieve the same results, although function2 works fine whereas function1 returns different results

;#include <Array.au3>
#include <security.au3>
#include <WinAPI.au3>
#include <ProcessConstants.au3>

Global Const $TOKEN_MAXIMUM_ALLOWED = 0x02000000

Func _GetTokenPSid($hToken)
    Local $aCall = DllStructGetData(_Security__GetTokenInformation($hToken, $TOKENUSER), 1)
    $tempPtr = DllStructCreate("PTR")
    $ptrSize = DllStructGetSize($tempPtr)
    $rawSid = BinaryMid($aCall, $ptrSize * 2 + 1, BinaryLen($aCall))
    $mem = DllStructCreate("byte Attributes[" & BinaryLen($rawSid) & "]")
    DllStructSetData($mem, "Attributes", $rawSid)
    $pSid = DllStructGetPtr($mem)
    Return $pSid
EndFunc   ;==>_GetTokenPSid



Func _GetTokenUser1($hToken)
    $pSid = _GetTokenPSid($hToken)
    Local $aCall = DllCall("advapi32.dll", "bool", "LookupAccountSidW", "ptr", "", "ptr", $pSid, "wstr", "", "dword*", 65536, "wstr", "", "dword*", 65536, "int*", 0)
    If IsArray($aCall) Then
        Return $aCall[5] & "\" & $aCall[3]
    Else
        Return ""
    EndIf
EndFunc   ;==>_GetTokenUser


Func _GetTokenUser2($hToken)
    Local $aCall = DllStructGetData(_Security__GetTokenInformation($hToken, $TOKENUSER), 1)
    $tempPtr = DllStructCreate("PTR")
    $ptrSize = DllStructGetSize($tempPtr)
    $rawSid = BinaryMid($aCall, $ptrSize * 2 + 1, BinaryLen($aCall))
    $mem = DllStructCreate("byte Attributes[" & BinaryLen($rawSid) & "]")
    DllStructSetData($mem, "Attributes", $rawSid)
    $pSid = DllStructGetPtr($mem)
    Local $bCall = DllCall("advapi32.dll", "bool", "LookupAccountSidW", "ptr", "", "ptr", $pSID, "wstr", "", "dword*", 65536, "wstr", "", "dword*", 65536, "int*", 0)
    If IsArray($bCall) Then
        Return $bCall[5] & "\" & $bCall[3]
    Else
        Return ""
    EndIf
EndFunc


Func _ProcessTokenInfo($pid)
    $hToken = _Security__OpenProcessToken(_WinAPI_OpenProcess($TOKEN_MAXIMUM_ALLOWED, 0, $pid), $TOKEN_QUERY)
    If Not $hToken Then
        $hToken = _Security__OpenProcessToken(_WinAPI_OpenProcess($PROCESS_QUERY_LIMITED_INFORMATION, 0, $pid), $TOKEN_QUERY)
    EndIf
    ConsoleWrite("GetTokenUser1 : " & _GetTokenUser1($hToken) & @CRLF)
    ConsoleWrite("GetTokenUser2 : " & _GetTokenUser2($hToken) & @CRLF)
    ConsoleWrite(@CRLF)
    _WinAPI_CloseHandle($hToken)
    Return
EndFunc   ;==>_ProcessTokenInfo


For $i = 1 To 10
    _ProcessTokenInfo(856)
Next

Below are the results

Quote

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : Test-PC\Test
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : \
GetTokenUser2 : Test-PC\Test

GetTokenUser1 : Test-PC\Test
GetTokenUser2 : Test-PC\Test

Am I doing something wrong here ?

Share this post


Link to post
Share on other sites



#2 ·  Posted

Yes you are. First one is wrong because the structure you create inside _GetTokenPSid gets destroyed before used. Then you use pointer to it in _GetTokenUser1. Pointer to lost space.
You're lucky if it don't crash.

2 people like this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for pointing out the mistake, I have made the below changes

Func _GetTokenPSid($hToken)
    Local $aCall = DllStructGetData(_Security__GetTokenInformation($hToken, $TOKENUSER), 1)
    $tempPtr = DllStructCreate("PTR")
    $ptrSize = DllStructGetSize($tempPtr)
    $rawSid = BinaryMid($aCall, $ptrSize * 2 + 1, BinaryLen($aCall))
    $tBuffer = DllStructCreate("byte Attributes[" & BinaryLen($rawSid) & "]")
    DllStructSetData($tBuffer, "Attributes", $rawSid)
    Return $tBuffer
EndFunc   ;==>_GetTokenPSid

Func _GetTokenUser1($hToken)
    $aCall = _GetTokenPSid($hToken)
    $pSid =  DllStructGetPtr($aCall)
    $aCall = _Security__LookupAccountSid($pSID)
    If IsArray($aCall) Then
        Return $aCall[1] & "\" & $aCall[0]
    Else
        Return ""
    EndIf
EndFunc   ;==>_GetTokenUser

The above works,  but I'm not sure why the below code doesn't (I mostly use python hence m trying to figure out why this shouldn't work).

Func _GetTokenUser1($hToken)
    $aCall = _Security__LookupAccountSid(DllStructGetPtr(_GetTokenPSid($hToken))
    ConsoleWrite(DllStructGetPtr(_GetTokenPSid($hToken)) & " , " &  DllStructGetData(_GetTokenPSid($hToken),1) & @CRLF)
    If IsArray($aCall) Then
        Return $aCall[1] & "\" & $aCall[0] & @CRLF
    Else
        Return ""
    EndIf
EndFunc   ;==>_GetTokenUser

Output 
 

Quote

0x0000006B85EB4340 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : Test-PC\Test
GetTokenUser2 : Test-PC\Test

0x0000006B85EB47F0 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB4370 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB4BB0 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB4400 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB4BB0 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB45E0 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB44C0 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB4880 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test

0x0000006B85EB4C40 , 0x010500000000000515000000AE9CF4E5A3A1353347837BC4E9030000
GetTokenUser1 : 
GetTokenUser2 : Test-PC\Test


The buffer is always the same, is it always required to assign function calls to variables?

Edited by kanishk619

Share this post


Link to post
Share on other sites

#4 ·  Posted

1 hour ago, kanishk619 said:

Thanks for pointing out the mistake, I have made the below changes

Func _GetTokenPSid($hToken)
    Local $aCall = DllStructGetData(_Security__GetTokenInformation($hToken, $TOKENUSER), 1)
    $tempPtr = DllStructCreate("PTR")
    $ptrSize = DllStructGetSize($tempPtr)
    $rawSid = BinaryMid($aCall, $ptrSize * 2 + 1, BinaryLen($aCall))
    $tBuffer = DllStructCreate("byte Attributes[" & BinaryLen($rawSid) & "]")
    DllStructSetData($tBuffer, "Attributes", $rawSid)
    Return $tBuffer
EndFunc   ;==>_GetTokenPSid

Func _GetTokenUser1($hToken)
    $aCall = _GetTokenPSid($hToken)
    $pSid =  DllStructGetPtr($aCall)
    $aCall = _Security__LookupAccountSid($pSID)
    If IsArray($aCall) Then
        Return $aCall[1] & "\" & $aCall[0]
    Else
        Return ""
    EndIf
EndFunc   ;==>_GetTokenUser

The above works,  but I'm not sure why the below code doesn't (I mostly use python hence m trying to figure out why this shouldn't work).

Func _GetTokenUser1($hToken)
    $aCall = _Security__LookupAccountSid(DllStructGetPtr(_GetTokenPSid($hToken))
    ConsoleWrite(DllStructGetPtr(_GetTokenPSid($hToken)) & " , " &  DllStructGetData(_GetTokenPSid($hToken),1) & @CRLF)
    If IsArray($aCall) Then
        Return $aCall[1] & "\" & $aCall[0] & @CRLF
    Else
        Return ""
    EndIf
EndFunc   ;==>_GetTokenUser

Output 
 


The buffer is always the same, is it always required to assign function calls to variables?

No, of course not. Preferable way is to use function calls over assigning the result to variable.
However, it really depends what the function returns. In your case the returned value is dllstruct. That struct (if not assigned to variable) exists only while that segment of code is executed.

It's really not complicated, and very much logical.

2 people like this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

2 hours ago, trancexx said:

Preferable way is to use function calls over assigning the result to variable.

I believe I'm guilty of not following this logical advice. Having said that, I did some performance tests a while ago and as expected - you are right. :) Again.

Edited by czardas

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • X_xkijux_x
      By X_xkijux_x
      Ok so im using this program. clarify for school. I would like to have a program that takes every word i type in microsoft word and searches on it in claryfi. is this possible. I would like to have the program not stopping me from typing more after that word. Like when i type a word it auto search it on clarify without making me stop typing and if i want to change i can do that and if im fine that word i can just keep typing and it will search after the next word. 
    • Miliardsto
      By Miliardsto
      Is this possible to make program works like in diagram? There is so much encryption methods and UDFs are they give security? If the $Address will be crypted with some encryption algorithm could be possible to get the value of $Address in easy way? Of course We know its easy too look into autoit code and get value of variable and so We dont want to anyone see $Address value - there are functions Could func1.au3, func2.au3.. be for example func1.php or other type of file? As we know .php files are unnable to see. For example func1 would be read by FileRead() and then Execute() Is this generally possible to do?
      Will it give secure?
      What do you think about? Maybe there is something wrong in this concept or missed.

    • JaredStroeb
      By JaredStroeb
      I need to open an application and send it keys in Session 0.  I have exe's working locally, I can see the application open in session 0 through interactive services, but my send commands do not go to the application.  The following article running-autoit-session-0, directs me to use ControlSend() however my application has no controls that can be identified by AutoIt v3 Window Info.  Get All Windows Controls also returned nothing for the application's handle.  
      What are my options?  
      Can this be done with AutoIt?
      Is there a different "relatively simple" library/tool to use? (I am familiar with C# if there is a viable library there )

       
    • nassausky
      By nassausky
      Hey all,
      Has anyone created any queries for youtube? I was thinking it might be something like this but can't quite figure it out:
      $oIE = _IECreateEmbedded() _IENavigate($oIE,"https://www.youtube.com") Local $oForms = _IETagNameGetCollection($oIE, "form") Local $sTxt = "" For $oForm In $oForms If $oForm.id = "masthead-search" Then $oInputs = _IETagNameGetCollection($oIE, "input") For $oInput In $oInputs If $oInput.id = "masthead-search-term" Then _IEFormElementSetValue($oInput, "cards") _IEFormSubmit($oForm) EndIf Next EndIf Next  
    • Skysnake
      By Skysnake
      Scite comes up occasional, almost incidentally, in some support topics.  I have not found a "Scite" topic in the forums, so this is where I will start.  I have recently been using Scite intensively, specifically with a large project and I have certain issues.  I know that Scite is not an AutoIt product, but it is so closely integrated to the core release that it makes sense to raise issues and request support here.  If this is not the right please refer me to the right forum.
      Thx
       
      Skysnake