【问题标题】:VirtualAlloc and Python - Access ViolationVirtualAlloc 和 Python - 访问冲突
【发布时间】:2020-05-28 15:14:50
【问题描述】:

非常简单的 python 脚本给了我访问冲突,我就是不知道为什么。

import ctypes

def Test():
     data = bytearray( "\xDE\xAD\xBE\xEF\x0B\xAD\xC0\xDE", 'utf-16' )
     dataLen = len( data )

     try :
        ptr = ctypes.windll.kernel32.VirtualAlloc( ctypes.c_int( 0 ),
                                                   ctypes.c_int( dataLen ),
                                                   ctypes.c_int( 0x3000 ),
                                                   ctypes.c_int( 0x40 ) )

        buf = ( ctypes.c_char * dataLen ).from_buffer( data )

        ctypes.windll.kernel32.RtlMoveMemory( ctypes.c_int( ptr ),
                                              buf,
                                              ctypes.c_int( dataLen ) )
     except Exception as e :
        print( e )
        exit(-1)

错误:

Traceback (most recent call last):
  File "c:\vs17\...\ptvsd_launcher.py", line 119, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)
  File "c:\vs17\...\ptvsd\debugger.py", line 37, in debug
    run(address, filename, *args, **kwargs)
  File "c:\vs17\...\ptvsd\_local.py", line 64, in run_file
    run(argv, addr, **kwargs)
  File "c:\vs17\...\ptvsd\_local.py", line 125, in _run
    _pydevd.main()
  File "c:\vs17\..\ptvsd\_vendored\pydevd\pydevd.py", line 1752, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "c:\vs17\...\ptvsd\_vendored\pydevd\pydevd.py", line 1099, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\vs17\...\ptvsd\_vendored\pydevd\pydevd.py", line 1106, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\vs17\...\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:\Dev\Python\VirtualAlloc_Testing\VirtualAlloc_Testing.py", line 31, in <module>
    main()
  File "D:\Dev\Python\VirtualAlloc_Testing\VirtualAlloc_Testing.py", line 29, in main
    Test()
  File "D:\Dev\Python\VirtualAlloc_Testing\VirtualAlloc_Testing.py", line 19, in Test
    ctypes.c_int( dataLen ) )
OSError: exception: access violation writing 0x00000000212F0000

【问题讨论】:

  • 为什么要这样使用except Exception?请分享整个错误消息。
  • @AMC - 感谢您的回复:* OSError:异常:访问冲突写入 0x00000000212F0000
  • 我写了上面的 C++ 版本,效果很好。由于某种原因,它在 Python 下不起作用。

标签: python virtualalloc


【解决方案1】:

遇到同样的问题。对我来说原因是错误的VirtualAllocrestype 大小,定义为 32 位值。

确实,ctypes 函数没有原型化并返回 C 默认类型:c_int。在 Windows 上,c_intc_long 的别名,c_long 是带符号的 32 位整数。因此,当系统分配超过 x32 限制的内存块时,VirtualAlloc 返回的地址将被剪切。

例如,在您的情况下,VirtualAlloc 可以返回 0000001A212F0000,但只有较低的部分 0x212F0000 被用作 ptr 的值。

使用前添加下一行:

ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_void_p

同样的东西是关于RtlCopyMemory.argtypes:

ctypes.windll.kernel32.RtlCopyMemory.argtypes = ( ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t ) 

效果不错

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多