def hash_data(buffer, alg, block_size=16*1024): if alg not in _calg_map: raise InvalidHashAlgorithm() block_size = int(block_size) if block_size <= 0: raise ValueError("'block_size' should be a positive integer") with _crypt_context() as hCryptProv: with _crypt_hash(hCryptProv, _calg_map[alg]) as hCryptHash: buffer_len = len(buffer) if block_size > buffer_len: block_size = buffer_len i = 0 while i < buffer_len: count = block_size if (i + block_size <= buffer_len) else (buffer_len - i) if not CryptHashData(hCryptHash, _cast(buffer[i:i+count], PBYTE), DWORD(count), DWORD(0)): raise WinError() i += count dwHashLen = DWORD(sizeof(DWORD)) dwHashSize = DWORD() if not CryptGetHashParam(hCryptHash, HP_HASHSIZE, _cast(pointer(dwHashSize), PBYTE), pointer(dwHashLen), DWORD(0)): raise WinError() pbHashVal = (BYTE * dwHashSize.value)() dwHashLen.value = sizeof(pbHashVal) if not CryptGetHashParam(hCryptHash, HP_HASHVAL, pbHashVal, pointer(dwHashLen), DWORD(0)): raise WinError() return ''.join(map(lambda b: format(b, '02x'), bytes(pbHashVal)))
def msys_control_c_workaround(): """Turn off console Ctrl-c support and implement it ourselves.""" # Used to work around a bug in msys where control-c kills the process # abruptly ~100ms after the process receives SIGINT. This prevents us # from running cleanup handlers, like the one that kills the Unity.exe # subprocess. if not os.getenv('MSYSTEM'): return import ctypes from ctypes.wintypes import HANDLE, DWORD, BOOL kernel32 = ctypes.windll.kernel32 kernel32.GetStdHandle.restype = HANDLE kernel32.GetStdHandle.argtypes = (DWORD, ) #kernel32.GetConsoleMode.restype = BOOL kernel32.GetConsoleMode.argtypes = (HANDLE, ctypes.POINTER(DWORD)) #kernel32.SetConsoleMode.restype = BOOL kernel32.SetConsoleMode.argtypes = (HANDLE, DWORD) STD_INPUT_HANDLE = DWORD(-10) ENABLE_PROCESSED_INPUT = DWORD(1) stdin = kernel32.GetStdHandle(STD_INPUT_HANDLE) mode = DWORD() kernel32.GetConsoleMode(stdin, ctypes.byref(mode)) mode.value = mode.value & ~(ENABLE_PROCESSED_INPUT.value) kernel32.SetConsoleMode(stdin, mode) # interrupt_main won't interrupt WaitForSingleObject, so monkey-patch import subprocess def polling_wait(self): from _subprocess import WaitForSingleObject, WAIT_OBJECT_0 while WaitForSingleObject(self._handle, 3000) != WAIT_OBJECT_0: continue return self.poll() subprocess.Popen.wait = polling_wait import thread import threading def look_for_control_c(): import msvcrt, thread while msvcrt.getch() != '\x03': continue thread.interrupt_main() t = threading.Thread(target=look_for_control_c) t.daemon = True t.start()
def hash_data(buffer, alg, block_size=16 * 1024): if alg not in _calg_map: raise InvalidHashAlgorithm() block_size = int(block_size) if block_size <= 0: raise ValueError("'block_size' should be a positive integer") with _crypt_context() as hCryptProv: with _crypt_hash(hCryptProv, _calg_map[alg]) as hCryptHash: buffer_len = len(buffer) if block_size > buffer_len: block_size = buffer_len i = 0 while i < buffer_len: count = block_size if (i + block_size <= buffer_len) else ( buffer_len - i) if not CryptHashData(hCryptHash, _cast(buffer[i:i + count], PBYTE), DWORD(count), DWORD(0)): raise WinError() i += count dwHashLen = DWORD(sizeof(DWORD)) dwHashSize = DWORD() if not CryptGetHashParam(hCryptHash, HP_HASHSIZE, _cast(pointer(dwHashSize), PBYTE), pointer(dwHashLen), DWORD(0)): raise WinError() pbHashVal = (BYTE * dwHashSize.value)() dwHashLen.value = sizeof(pbHashVal) if not CryptGetHashParam(hCryptHash, HP_HASHVAL, pbHashVal, pointer(dwHashLen), DWORD(0)): raise WinError() return ''.join(map(lambda b: format(b, '02x'), bytes(pbHashVal)))
def doUrlStatus(self, url, host, headers={}, user=0, password=0,port=80): wininet = windll.wininet flags = DWORD() self.lock.acquire() self.hInternet = wininet.InternetOpenA('wapt-get/1.0', 0, 0, 0, 0); self.lock.release() if not self.hInternet: #gErr.log("couldn't open") self.stop() return 0 #print hInternet #print "Doing Connect" #gDbg.log('Connecting...') if self.callback: self.callback('Connecting...', 1) time.sleep(sleepTime) INTERNET_SERVICE_HTTP = 3 self.hConnect = wininet.InternetConnectA(self.hInternet, host, port, user, password, 3, 0, 0) if not self.hConnect: #gErr.log("couldn't connect") self.stop() return 0 #print hConnect #gDbg.log('Opening Request...') if self.callback: self.callback('Sending...', 2) time.sleep(sleepTime) INTERNET_FLAG_NO_CACHE_WRITE = 0x04000000 #hRequest = wininet.InternetOpenUrlA(hInternet, "http://rpc.bloglines.com/listsubs", 0, 0, 0x80000200L, 0) self.hRequest = wininet.HttpOpenRequestA(self.hConnect, "GET", url, 0, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0) if not self.hRequest: #gErr.log("couldn't open request") self.stop() return 0 HTTP_ADDREQ_FLAG_ADD_IF_NEW = 0x10000000 HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA = 0x40000000 HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON = 0x01000000 HTTP_ADDREQ_FLAG_COALESCE = HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA HTTP_ADDREQ_FLAG_ADD = 0x20000000 HTTP_ADDREQ_FLAG_REPLACE = 0x80000000 for k in headers.keys(): res = wininet.HttpAddRequestHeadersA(self.hRequest, "%s: %s\r\n" %(k, headers[k]), -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD) if not res: code = GetLastError() #gErr.log("couldn't add header: %d - %s" %(code, FormatError(code))) self.stop() return retVal gDbg.log('Sending Request...') res = wininet.HttpSendRequestA(self.hRequest, 0,0,0,0) if not res: #gErr.log("couldn't send request") self.stop() return 0 #print "Request Sent: %d", res HTTP_QUERY_FLAG_NUMBER = 0x20000000 HTTP_QUERY_CONTENT_LENGTH = 5 HTTP_QUERY_STATUS_CODE = 19 #gDbg.log('Getting Result...') if self.callback: self.callback('Getting Result...', 3) time.sleep(sleepTime) dwStatus = DWORD() dwBufLen = DWORD(4) res =wininet.HttpQueryInfoA(self.hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, byref(dwStatus), byref(dwBufLen), 0) if res == 0: #gErr.log("Bad HttpQueryInfo") #print "HttpQueryInfo failed" dwStatus.value = 0 status = dwStatus.value #gDbg.log("Status = %d" %status) self.stop() return status
def doUrlPost(self, url, host, postData, headers = {}, user=0, password=0): wininet = windll.wininet flags = DWORD() data = '' retVal = (0, data) # Use the Registry settings for Proxy (nice!) INTERNET_OPEN_TYPE_PRECONFIG = 0 self.lock.acquire() self.hInternet = wininet.InternetOpenA('Blogbot/1.0 (http://blogbot.com/)', INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); self.lock.release() if not self.hInternet: code = GetLastError() gErr.log("couldn't open: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print hInternet #print "Doing Connect" gDbg.log('Connecting...') if self.callback: self.callback('Conecting...', 1) time.sleep(sleepTime) INTERNET_SERVICE_HTTP = 3 INTERNET_INVALID_PORT_NUMBER = 0 self.hConnect = wininet.InternetConnectA(self.hInternet, host, INTERNET_INVALID_PORT_NUMBER, user, password, INTERNET_SERVICE_HTTP, 0, 0) if not self.hConnect: code = GetLastError() gErr.log("couldn't connect: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print hConnect gDbg.log('Opening Request...') if self.callback: self.callback('Sending...', 2) time.sleep(sleepTime) INTERNET_FLAG_NO_CACHE_WRITE = 0x04000000 #hRequest = wininet.InternetOpenUrlA(hInternet, "http://rpc.bloglines.com/listsubs", 0, 0, 0x80000200L, 0) self.hRequest = wininet.HttpOpenRequestA(self.hConnect, "POST", url, 0, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0) if not self.hRequest: code = GetLastError() gErr.log("couldn't open request: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print hRequest HTTP_ADDREQ_FLAG_ADD_IF_NEW = 0x10000000 HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA = 0x40000000 HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON = 0x01000000 HTTP_ADDREQ_FLAG_COALESCE = HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA HTTP_ADDREQ_FLAG_ADD = 0x20000000 HTTP_ADDREQ_FLAG_REPLACE = 0x80000000 for k in headers.keys(): res = wininet.HttpAddRequestHeadersA(self.hRequest, "%s: %s\r\n" %(k, headers[k]), -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD) if not res: code = GetLastError() gErr.log("couldn't add header: %d - %s" %(code, FormatError(code))) self.stop() return retVal gDbg.log('Sending Request...') res = wininet.HttpSendRequestA(self.hRequest, 0,0,postData,len(postData)) if not res: code = GetLastError() gErr.log("couldn't send request: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print "Request Sent: %d", res HTTP_QUERY_FLAG_NUMBER = 0x20000000 HTTP_QUERY_CONTENT_LENGTH = 5 HTTP_QUERY_STATUS_CODE = 19 gDbg.log('Getting Result...') if self.callback: self.callback('Getting Result...', 3) time.sleep(sleepTime) dwStatus = DWORD() dwBufLen = DWORD(4) res =wininet.HttpQueryInfoA(self.hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, byref(dwStatus), byref(dwBufLen), 0) if res == 0: code = GetLastError() gErr.log("couldn't query info: %d - %s" %(code, FormatError(code))) dwStatus.value = 0 status = dwStatus.value gDbg.log("Status = %d" %status) data = '' if (status / 100) == 2: while 1: buff = c_buffer(8192) bytesRead = DWORD() bResult = wininet.InternetReadFile(self.hRequest, buff, 8192, byref(bytesRead)) #print "bResult: ", bResult if bytesRead.value == 0: break data = data + buff.raw[:bytesRead.value] self.stop() return (status, data)
def doUrlGet(self, url, host, headers={}, user=0, password=0, port=80): wininet = windll.wininet flags = DWORD() data = '' retVal = (0, data) # Use the Registry settings for Proxy (nice!) INTERNET_OPEN_TYPE_PRECONFIG = 0 self.lock.acquire() self.hInternet = wininet.InternetOpenA('WAPT)', INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); self.lock.release() if not self.hInternet: code = GetLastError() #gErr.log("couldn't open: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print hInternet #print "Doing Connect" #gDbg.log('Connecting...') if self.callback: self.callback('Connecting...', 1) time.sleep(sleepTime) INTERNET_SERVICE_HTTP = 3 INTERNET_INVALID_PORT_NUMBER = 0 self.hConnect = wininet.InternetConnectA(self.hInternet, host, port, user, password, INTERNET_SERVICE_HTTP, 0, 0) if not self.hConnect: code = GetLastError() #gErr.log("couldn't connect: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print hConnect #gDbg.log('Opening Request...') if self.callback: self.callback('Sending...', 2) time.sleep(sleepTime) INTERNET_FLAG_NO_CACHE_WRITE = 0x04000000 self.hRequest = wininet.HttpOpenRequestA(self.hConnect, "GET", url, 0, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0) if not self.hRequest: code = GetLastError() #gErr.log("couldn't open request: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print hRequest #gDbg.log('Sending Request...') res = wininet.HttpSendRequestA(self.hRequest, 0,0,0,0) if not res: code = GetLastError() gErr.log("couldn't send request: %d - %s" %(code, FormatError(code))) self.stop() return retVal #print "Request Sent: %d", res HTTP_QUERY_FLAG_NUMBER = 0x20000000 HTTP_QUERY_CONTENT_LENGTH = 5 HTTP_QUERY_STATUS_CODE = 19 #gDbg.log('Getting Result...') if self.callback: self.callback('Getting Result...', 3) time.sleep(sleepTime) dwStatus = DWORD() dwBufLen = DWORD(4) res =wininet.HttpQueryInfoA(self.hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, byref(dwStatus), byref(dwBufLen), 0) if res == 0: code = GetLastError() #gErr.log("couldn't query info: %d - %s" %(code, FormatError(code))) dwStatus.value = 0 status = dwStatus.value #gDbg.log("Status = %d" %status) data = '' if status == 200: while 1: buff = c_buffer(8192) bytesRead = DWORD() bResult = wininet.InternetReadFile(self.hRequest, buff, 8192, byref(bytesRead)) #print "bResult: ", bResult if bytesRead.value == 0: break data = data + buff.raw[:bytesRead.value] self.stop() return (status, data)