def get_name(self, nametype=gdef.CERT_NAME_SIMPLE_DISPLAY_TYPE, flags=0):
"""Retrieve the subject or issuer name of the certificate. See ``CertGetNameStringA``
:returns: :class:`str`
"""
size = winproxy.CertGetNameStringA(self, nametype, flags, None, None, 0)
namebuff = ctypes.c_buffer(size)
size = winproxy.CertGetNameStringA(self, nametype, flags, None, namebuff, size)
return namebuff[:-1]
python类c_buffer()的实例源码
def get_param(self, param_type, index=0):
data_size = gdef.DWORD()
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa380227(v=vs.85).aspx
winproxy.CryptMsgGetParam(self, param_type, index, None, data_size)
buffer = ctypes.c_buffer(data_size.value)
winproxy.CryptMsgGetParam(self, param_type, index, buffer, data_size)
if param_type in self.MSG_PARAM_KNOW_TYPES:
buffer = self.MSG_PARAM_KNOW_TYPES[param_type].from_buffer(buffer)
if isinstance(buffer, gdef.DWORD): # DWORD -> return the Python int
return buffer.value
return buffer
# Certificate accessors
def get_long_path(path):
"""Return the long path form for ``path``
:returns: :class:`str`
"""
size = 0x1000
buffer = ctypes.c_buffer(size)
rsize = winproxy.GetLongPathNameA(path, buffer, size)
return buffer[:rsize]
def get_short_path(path):
"""Return the short path form for ``path``
:returns: :class:`str`
"""
size = 0x1000
buffer = ctypes.c_buffer(size)
rsize = winproxy.GetShortPathNameA(path, buffer, size)
return buffer[:rsize]
def decompress_buffer(comptype, buffer, uncompress_size=None):
if uncompress_size is None:
uncompress_size = len(buffer) * 10
result_size = DWORD()
uncompressed = ctypes.c_buffer(uncompress_size)
windows.winproxy.RtlDecompressBuffer(comptype, uncompressed, uncompress_size, buffer, len(buffer), result_size)
return uncompressed[:result_size.value]
# sid.py + real SID type ?
def get_known_sid(sid_type):
size = DWORD()
try:
windows.winproxy.CreateWellKnownSid(sid_type, None, None, size)
except WindowsError:
pass
buffer = ctypes.c_buffer(size.value)
windows.winproxy.CreateWellKnownSid(sid_type, None, buffer, size)
return ctypes.cast(buffer, PSID)
def getDeviceInfoDetail(devnum=0):
"""Get an entry from the internal device info list. """
f = _ft.DWORD()
t = _ft.DWORD()
i = _ft.DWORD()
l = _ft.DWORD()
h = _ft.FT_HANDLE()
n = c.c_buffer(MAX_DESCRIPTION_SIZE)
d = c.c_buffer(MAX_DESCRIPTION_SIZE)
createDeviceInfoList()
call_ft(_ft.FT_GetDeviceInfoDetail, _ft.DWORD(devnum),
c.byref(f), c.byref(t), c.byref(i), c.byref(l), n, d, c.byref(h))
return {'index': devnum, 'flags': f.value, 'type': t.value,
'id': i.value, 'location': l.value, 'serial': n.value,
'description': d.value, 'handle': h}
def read(self, nchars, raw=True):
"""Read up to nchars bytes of data from the device. Can return fewer if
timedout. Use getQueueStatus to find how many bytes are available"""
b_read = _ft.DWORD()
b = c.c_buffer(nchars)
call_ft(_ft.FT_Read, self.handle, b, nchars, c.byref(b_read))
return b.raw[:b_read.value] if raw else b.value[:b_read.value]
def getDeviceInfo(self):
"""Returns a dictionary describing the device. """
deviceType = _ft.DWORD()
deviceId = _ft.DWORD()
desc = c.c_buffer(MAX_DESCRIPTION_SIZE)
serial = c.c_buffer(MAX_DESCRIPTION_SIZE)
call_ft(_ft.FT_GetDeviceInfo, self.handle, c.byref(deviceType),
c.byref(deviceId), serial, desc, None)
return {'type': deviceType.value, 'id': deviceId.value,
'description': desc.value, 'serial': serial.value}
def __init__(self, ArcName=None, ArcNameW=u'', OpenMode=RAR_OM_LIST):
self.CmtBuf = ctypes.c_buffer(64*1024)
ctypes.Structure.__init__(self, ArcName=ArcName, ArcNameW=ArcNameW, OpenMode=OpenMode, _CmtBuf=ctypes.addressof(self.CmtBuf), CmtBufSize=ctypes.sizeof(self.CmtBuf))
def __init__(self):
self.CmtBuf = ctypes.c_buffer(64*1024)
ctypes.Structure.__init__(self, _CmtBuf=ctypes.addressof(self.CmtBuf), CmtBufSize=ctypes.sizeof(self.CmtBuf))
def struct(cls, ea, **sid):
"""Return the structure_t at address ``ea`` as a dict of ctypes.
If the structure ``sid`` is specified, then use that specific structure type.
"""
ea = interface.address.within(ea)
if any(n in sid for n in ('sid','struc','structure','id')):
res = sid['sid'] if 'sid' in sid else sid['struc'] if 'struc' in sid else sid['structure'] if 'structure' in sid else sid['id'] if 'id' in sid else None
sid = res.id if isinstance(res, structure.structure_t) else res
else:
sid = type.structure.id(ea)
st = structure.instance(sid, offset=ea)
typelookup = {
(int,-1) : ctypes.c_int8, (int,1) : ctypes.c_uint8,
(int,-2) : ctypes.c_int16, (int,2) : ctypes.c_uint16,
(int,-4) : ctypes.c_int32, (int,4) : ctypes.c_uint32,
(int,-8) : ctypes.c_int64, (int,8) : ctypes.c_uint64,
(float,4) : ctypes.c_float, (float,8) : ctypes.c_double,
}
res = {}
for m in st.members:
t, val = m.type, read(m.offset, m.size) or ''
try:
ct = typelookup[t]
except KeyError:
ty, sz = t if isinstance(t, __builtin__.tuple) else (m.type, 0)
if isinstance(t, __builtin__.list):
t = typelookup[tuple(ty)]
ct = t*sz
elif ty in (chr,str):
ct = ctypes.c_char*sz
else:
ct = None
finally:
res[m.name] = val if any(_ is None for _ in (ct,val)) else ctypes.cast(ctypes.pointer(ctypes.c_buffer(val)),ctypes.POINTER(ct)).contents
return res
def _task_list():
psapi = ctypes.windll.psapi
kernel = ctypes.windll.kernel32
hModule = ctypes.c_ulong()
count = ctypes.c_ulong()
modname = ctypes.c_buffer(30)
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_READ = 0x0010
pid_list = win32process.EnumProcesses()
info_list = []
for pid in pid_list:
hProcess = kernel.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid)
if hProcess:
psapi.EnumProcessModules(hProcess, ctypes.byref(hModule), ctypes.sizeof(hModule), ctypes.byref(count))
psapi.GetModuleBaseNameA(hProcess, hModule.value, modname, ctypes.sizeof(modname))
pname = ctypes.string_at(modname)
procmeminfo = win32process.GetProcessMemoryInfo(hProcess)
procmemusage = (procmeminfo["WorkingSetSize"]/1024)
info_list.append((pid, pname, procmemusage))
kernel.CloseHandle(hProcess)
return info_list
def cfstring_to_string(value_string):
value_length = carbon.CFStringGetLength(value_string)
buffer_length = carbon.CFStringGetMaximumSizeForEncoding(
value_length, kCFStringEncodingUTF8)
buffer = ctypes.c_buffer(buffer_length + 1)
result = carbon.CFStringGetCString(value_string,
buffer,
len(buffer),
kCFStringEncodingUTF8)
if not result:
return
return buffer.value
def cfstring_to_string(value_string):
value_length = carbon.CFStringGetLength(value_string)
buffer_length = carbon.CFStringGetMaximumSizeForEncoding(
value_length, kCFStringEncodingUTF8)
buffer = ctypes.c_buffer(buffer_length + 1)
result = carbon.CFStringGetCString(value_string,
buffer,
len(buffer),
kCFStringEncodingUTF8)
if not result:
return
return buffer.value
def getHardwareInformation(self):
''' Get information from the hardware'''
model = c_buffer(255)
softwareVersion = c_buffer(255)
hardwareNotes = c_buffer(255)
self.aptdll.GetHWInfo(self.SerialNum, model, 255, softwareVersion, 255, hardwareNotes, 255)
hwinfo = [model.value, softwareVersion.value, hardwareNotes.value]
return hwinfo
def test_memory_read():
data = 'A'*0x40
buf = ctypes.c_buffer(data)
ea = ctypes.addressof(buf)
z = provider.memory()
z.seek(ea)
if z.consume(len(data)) == data:
raise Success
raise Failure
def test_memory_write():
data = 'A'*0x40
buf = ctypes.c_buffer(data)
ea = ctypes.addressof(buf)
z = provider.memory()
z.seek(ea)
z.store('B'*len(data))
if buf.value == 'B'*len(data):
raise Success
raise Failure
def test_memory_readwrite():
data = 'A'*0x40
buf = ctypes.c_buffer(data)
ea = ctypes.addressof(buf)
z = provider.memory()
z.seek(ea)
z.store('B'*len(data))
z.seek(ea)
if z.consume(len(data)) == 'B'*len(data):
raise Success
def decrypt(cls, data, key):
clen = cls.align_size(len(data))
ckey = c_buffer(key)
cin = c_buffer(data)
cout = c_buffer(clen)
cls.LIB.decrypt(cin, clen, ckey, cout)
return cout.raw