如何使用汇编语言在Windows注册表中设置值?

时间:2013-07-29 04:59:55

标签: windows assembly parameters stack masm

确定。所以我有这个程序试图在Windows注册表中创建一个值。不幸的是,没有任何事我一直试图弄清楚是否有任何参数错误。这是代码:

 includelib \Masm64\Lib\Kernel32.lib
 includelib \Masm64\Lib\Advapi32.lib
 extern RegOpenKeyExA : proc
 extern RegSetValueExA : proc
 extern ExitProcess : proc

 dseg       segment     para 'DATA'
 vlnm       db          'Startup', 0
 sbky       db          'Software\Microsoft\Windows\CurrentVersion\Run', 0
 phkr       dd          0
 path       db          'C:\Users\School\AppData\Roaming\Startups.exe', 0
 dseg       ends

 cseg       segment     para 'CODE'
 start      proc
            lea         rdx, [phkr]
            push        rdx
            sub         rsp, 28h
            mov         r9d, 2
            xor         r8d, r8d
            lea         rdx, [sbky]
            mov         ecx, 80000001h
            call        RegOpenKeyExA

            add         rsp, 28h
            push        45
            lea         rbx, [path]
            push        rbx
            sub         rsp, 28h
            mov         r9d, 1
            xor         r8d, r8d
            lea         rdx, [vlnm]
            mov         ecx, phkr
            call        RegSetValueExA

            call        ExitProcess
start       endp
cseg        ends
            end

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

请允许我回答我自己的问题。问题并不是真正涉及不正确的参数,而是我分配堆栈空间的错误。我期望为rcx,rdx,r8和r9分配20h的堆栈空间,并将返回地址对齐在16字节的边界上,我错误地创建了一个模板,如下所示:

*empty*         (rsp-8)
param2          (rsp-16)
param1          (rsp-24)
*empty*         (rsp-32... causes incorrect parameters and convention!)
space for r9    (rsp-40)
space for r8    (rsp-48)
space for rdx   (rsp-56)
space for rcx   (rsp-64)
return address  (rsp-72... not on a 16-byte boundary!)

正确的模板是

*empty*         (rsp-8)
param2          (rsp-16)
param1          (rsp-24)
space for r9    (rsp-32)
space for r8    (rsp-40)
space for rdx   (rsp-48)
space for rcx   (rsp-56)
return address  (rsp-64)

在RegSetValueEx调用之前,我无意中在堆栈参数和寄存器参数之间分配了额外的8个字节,从而提供了不正确的参数。这是正确的代码:

includelib \Masm64\Lib\Kernel32.lib
includelib \Masm64\Lib\Advapi32.lib
extern RegOpenKeyExA : proc
extern RegSetValueExA : proc
extern ExitProcess : proc

dseg        segment     para 'DATA'
vlnm        db          'Startup', 0
sbky        db          'Software\Microsoft\Windows\CurrentVersion\Run', 0
phkr        dd          0
path        db          'C:\Users\Games\AppData\Roaming\Startups.exe', 0
dseg        ends

cseg        segment     para 'CODE'
start       proc
            lea         rdx, [phky]
            push        rdx
            sub         rsp, 20h
            mov         r9d, 2
            xor         r8d, r8d
            lea         rdx, [sbky]
            mov         ecx, 80000001h
            call        RegOpenKeyExA

            add         rsp, 20h
            push        44
            lea         rbx, [path]
            push        rbx
            sub         rsp, 20h
            mov         r9d, 1
            xor         r8, r8
            lea         rdx, [vlnm]
            mov         ecx, phkr
            call        RegSetValueExA

fini:       call        ExitProcess
start       endp
cseg        ends
            end

干杯!

答案 1 :(得分:0)

您只需为密钥分配2个字节(phkr dw 0)。在我看来,它至少应该是4个字节。

除此之外,我建议您添加一些错误检查。如果RegOpenKeyExRegSetValueEx失败,则{{1}}和{{1}}都会返回非零错误代码。