flat assembler
Message board for the users of flat assembler.

Index > Windows > CryptProtectData Problem

Author
Thread Post new topic Reply to topic
jochenvnltn



Joined: 15 Jul 2011
Posts: 96
jochenvnltn 09 Jun 2018, 19:40
Hello Everyone Smile

Im trying out the CryptProtectData API to encrypt a password.
The problem is that nothing is encrypted while the function itself returns ERROR_SUCCESS.

Here's my code:

Code:
include 'win32ax.inc'
entry main

szData db $43,$00,$68,$00,$61,$00,$6e,$00,$67,$00,$65,$00,$00,$00,$00,$00

DataSize = $ - szData



main:

     mov [datain.pbData], szData
     mov [datain.cbData], DataSize

     invoke CryptProtectData, datain, 0, 0, 0, 0, 0, dataout
     ;//invoke CryptUnprotectData , datain, 0, 0, 0, 0, 0, dataout

     call CheckError

     cinvoke wsprintfW,buf,PASS,dataout;[dataout.pbData]
     invoke MessageBoxW,0,buf,cap,MB_OK

exit: invoke ExitProcess,0


proc CheckError
     mov eax, [fs:18h]
     mov eax, [eax+34h] ;GetLastError alternative
     invoke FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM+FORMAT_MESSAGE_ALLOCATE_BUFFER,0,eax,0,error_buffer,0,0
     invoke MessageBox,0,[error_buffer],'error',MB_OK
     ret
endp

 error_buffer dd ?

section '.data' data readable writeable

 struct DATA_BLOB
    cbData   dd ?
    pbData   dd ?
   ends
     
 datain      DATA_BLOB
 dataout     DATA_BLOB

 cap      du 'Result',0
 PASS     du 'Pass:  %s',10,13,0
 buf      du  64  dup (?)

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          Crypt32, 'Crypt32.dll'

  import Crypt32,\
                   CryptUnprotectData, 'CryptUnprotectData',\
                   CryptProtectData,'CryptProtectData'

  include 'api\kernel32.inc'
  include 'api\user32.inc' 
    
Post 09 Jun 2018, 19:40
View user's profile Send private message MSN Messenger Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 09 Jun 2018, 21:42
What do you expect to see and what makes you think nothing is encrypted? Seeing that you try to output the result by wsprintfW with %s placeholder I feel you don’t really understand what you get as the result of the encryption.
Post 09 Jun 2018, 21:42
View user's profile Send private message Visit poster's website Reply with quote
jochenvnltn



Joined: 15 Jul 2011
Posts: 96
jochenvnltn 09 Jun 2018, 22:04
DimonSoft wrote:
What do you expect to see and what makes you think nothing is encrypted? Seeing that you try to output the result by wsprintfW with %s placeholder I feel you don’t really understand what you get as the result of the encryption.


%s outputs it as a string. You can read about it here --> https://msdn.microsoft.com/en-us/library/windows/desktop/ms647550(v=vs.85).aspx
Post 09 Jun 2018, 22:04
View user's profile Send private message MSN Messenger Reply with quote
Walter



Joined: 26 Jan 2013
Posts: 155
Walter 09 Jun 2018, 23:01
This seems to work ok.

Code:
include 'win32ax.inc' 
entry main 

szData db 'paSS94wordGoesHere!',0
DataSize = $ - szData

main: 

     mov [datain.pbData], szData
     mov [datain.cbData], DataSize

     invoke CryptProtectData, datain, 0, 0, 0, 0, 0, dataout 
     cinvoke wsprintf,buf,PASS,[dataout.pbData]
     invoke MessageBox,0,buf,cap,MB_OK

     invoke CryptUnprotectData, dataout, 0, 0, 0, 0, 0, datain
     cinvoke wsprintf,buf,PASS,[datain.pbData]
     invoke MessageBox,0,buf,cap,MB_OK

     invoke ExitProcess,0

section '.data' data readable writeable

 struct DATA_BLOB 
    cbData   dd ? 
    pbData   dd ? 
   ends 
      
 datain      DATA_BLOB 
 dataout     DATA_BLOB 

 cap      db 'Result',0
 PASS     db 'Pass:  %s',10,13,0
 buf      db  64  dup (?)

section '.idata' import data readable writeable 

  library kernel32,'KERNEL32.DLL',\ 
          user32,'USER32.DLL',\ 
          Crypt32, 'Crypt32.dll' 

  import Crypt32,\ 
                   CryptUnprotectData, 'CryptUnprotectData',\ 
                   CryptProtectData,'CryptProtectData' 

  include 'api\kernel32.inc' 
  include 'api\user32.inc'      
Post 09 Jun 2018, 23:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 10 Jun 2018, 02:01
I'm not sure that passwords should ever be encrypted unless you are storing them in a password vault. For most other uses they should be hashed with a salt.
Post 10 Jun 2018, 02:01
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 10 Jun 2018, 07:06
jochenvnltn wrote:
DimonSoft wrote:
What do you expect to see and what makes you think nothing is encrypted? Seeing that you try to output the result by wsprintfW with %s placeholder I feel you don’t really understand what you get as the result of the encryption.


%s outputs it as a string. You can read about it here --> https://msdn.microsoft.com/en-us/library/windows/desktop/ms647550(v=vs.85).aspx

I do know what %s does. Everyone knows. There’s absolutely no sense in using it with arrays of bytes (which is what you get after encryption).

Your code works as expected and encrypts original message. And if you want to talk about reading documentation, you’d better follow this link and then (1) add LocalFree to your code and (2) stop calling GetLastError unless the function returns FALSE. Both are not necessary to get the encrypted string but should be kept in mind if you’re going to transform your code into something larger.
Post 10 Jun 2018, 07:06
View user's profile Send private message Visit poster's website Reply with quote
jochenvnltn



Joined: 15 Jul 2011
Posts: 96
jochenvnltn 10 Jun 2018, 12:05
Walter wrote:
This seems to work ok.

Code:
include 'win32ax.inc' 
entry main 

szData db 'paSS94wordGoesHere!',0
DataSize = $ - szData

main: 

     mov [datain.pbData], szData
     mov [datain.cbData], DataSize

     invoke CryptProtectData, datain, 0, 0, 0, 0, 0, dataout 
     cinvoke wsprintf,buf,PASS,[dataout.pbData]
     invoke MessageBox,0,buf,cap,MB_OK

     invoke CryptUnprotectData, dataout, 0, 0, 0, 0, 0, datain
     cinvoke wsprintf,buf,PASS,[datain.pbData]
     invoke MessageBox,0,buf,cap,MB_OK

     invoke ExitProcess,0

section '.data' data readable writeable

 struct DATA_BLOB 
    cbData   dd ? 
    pbData   dd ? 
   ends 
      
 datain      DATA_BLOB 
 dataout     DATA_BLOB 

 cap      db 'Result',0
 PASS     db 'Pass:  %s',10,13,0
 buf      db  64  dup (?)

section '.idata' import data readable writeable 

  library kernel32,'KERNEL32.DLL',\ 
          user32,'USER32.DLL',\ 
          Crypt32, 'Crypt32.dll' 

  import Crypt32,\ 
                   CryptUnprotectData, 'CryptUnprotectData',\ 
                   CryptProtectData,'CryptProtectData' 

  include 'api\kernel32.inc' 
  include 'api\user32.inc'      


Thank you Walter ! You helped me out allot! thx Wink
Post 10 Jun 2018, 12:05
View user's profile Send private message MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.