又是一个寂寞的周末啊同学们,这几天天气变冷自己却没有赖床,好吧,表扬一次^^
扯点八卦,今天是pycon2011在上海那边开了,我早上和刚才看了网上的直播,做的很不错,形式很像irongeek.com的视频,我觉得这块在国内还是有一点点商业前景的,哈.听了网易林伟和豆瓣洪强宁的演讲,开拓了点视野,豆瓣基本主要都是用python做,网易说一些服务端也是用python写,结合c去做,但是python的代码已经提高到70%了.所以,python都是很有前景的吧.(不过2.x和3.x的版本并行发展让哥蛋疼..)
今天有同学在群里发exploit-db上的MS11-080的连接,打开一看,尼玛的居然是用python写的,做系统提权.我感觉比较新鲜的是对windows系统api的调用居然这么方便(好吧,我孤陋寡闻- - ),顿时来了兴致,ctypes模块的WinDLL.既然python是一个开源的东东(口号:Human knowledge belongs to the world!),让我们可以深入了解底层的具体实现,我们要学习的总是这个渔而不是这个鱼对吧.下面算是我自己做做笔记吧,欢迎大家拍砖:
首先我们来看看WinDLL对windows api调用的方便程度:
上面图演示的是对Kernel32.dll里面的GetModuleHandleA的调用以及结果验证.同学们是不是觉得很方便哩.
看看WinDLL的实现,WinDLL实际上是一个类,下面是代码,我们一层一层往上回溯:
class WinDLL(CDLL):
"""This class represents a dll exporting functions using the
Windows stdcall calling convention.
"""
_func_flags_ = _FUNCFLAG_STDCALL
WinDLL的代码很简单,主要是声明一个函数标志,通过名字可以知道是声明函数的调用约定,这里是STDCALL,一看到这里哥就冒出了一个api需要测试,后面再说,这里卖个关子.WinDLL这个类继承自CDLL,那么我们接下来看的就是CDLL的代码:
class CDLL(object):
"""An instance of this class represents a loaded dll/shared
library, exporting functions using the standard C calling
convention (named 'cdecl' on Windows).
The exported functions can be accessed as attributes, or by
indexing with the function name. Examples:
<obj>.qsort -> callable object
<obj>['qsort'] -> callable object
Calling the functions releases the Python GIL during the call and
reacquires it afterwards.
"""
_func_flags_ = _FUNCFLAG_CDECL
_func_restype_ = c_int
def __init__(self, name, mode=DEFAULT_MODE, handle=None,
use_errno=False,
use_last_error=False):
self._name = name
flags = self._func_flags_
if use_errno:
f