Esempio n. 1
1
_TEB* GetThreadTeb(DWORD tid)
{
	NTSTATUS ntStatus;
	if (NtQueryInformationThread == NULL)
		NtQueryInformationThread = (pNtQIT)GetProcAddress(GetModuleHandle("ntdll.dll"),
		"NtQueryInformationThread");

	if (NtQueryInformationThread == NULL) {
		MyTrace("%s(): cannot found NtQueryInformationThread()", __FUNCTION__);
		return 0;
	}

	HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, tid);
	UINT32 ThreadBasicInformation = 0;
	THREAD_BASIC_INFORMATION bi;
	ntStatus = NtQueryInformationThread(hThread, ThreadBasicInformation, &bi, sizeof(bi), NULL);
	XDbgCloseHandle(hThread);
	if (ntStatus != 0)
		return 0;

	return (_TEB*)bi.TebBaseAddress;
}
Esempio n. 2
0
void XDbgProxy::sendProcessInfo(DWORD firstThread)
{
	MyTrace("%s()", __FUNCTION__);
	DebugEventPacket event;
	memset(&event, 0, sizeof(event));
	DEBUG_EVENT& msg = event.event;
	DebugAckPacket ack;
	msg.dwProcessId = XDbgGetCurrentProcessId();
	msg.dwThreadId = firstThread;

	char modName[MAX_PATH + 1] = {0};

	memset(&msg.u.CreateProcessInfo, 0, sizeof(msg.u.CreateProcessInfo));
	msg.dwDebugEventCode = CREATE_PROCESS_DEBUG_EVENT;
	msg.u.CreateProcessInfo.dwDebugInfoFileOffset = 0;
	msg.u.CreateProcessInfo.fUnicode = 0;
	msg.u.CreateProcessInfo.hFile = NULL;
	msg.u.CreateProcessInfo.hProcess = NULL;
	msg.u.CreateProcessInfo.hThread = NULL;
	msg.u.CreateProcessInfo.lpBaseOfImage = (PVOID )GetModuleHandle(NULL);
	GetModuleFileName(GetModuleHandle(NULL), modName, MAX_PATH);
	msg.u.CreateProcessInfo.lpImageName = modName;
	msg.u.CreateProcessInfo.lpStartAddress = (LPTHREAD_START_ROUTINE)GetThreadStartAddress(firstThread);
	MyTrace("%s(): mod: %s, main thread start at: %p", __FUNCTION__, modName, 
		msg.u.CreateProcessInfo.lpStartAddress);
	msg.u.CreateProcessInfo.lpThreadLocalBase = GetThreadTeb(firstThread);
	msg.u.CreateProcessInfo.nDebugInfoSize = 0;
	sendDbgEvent(event, ack, false);
}
Esempio n. 3
0
PVOID WINAPI GetThreadStartAddress(HANDLE hThread)
{
	NTSTATUS ntStatus;
	HANDLE hDupHandle;
	PVOID dwStartAddress;

	if (NtQueryInformationThread == NULL)
		NtQueryInformationThread = (pNtQIT)GetProcAddress(GetModuleHandle("ntdll.dll"),
		"NtQueryInformationThread");

	if (NtQueryInformationThread == NULL) {
		MyTrace("%s(): cannot found NtQueryInformationThread()", __FUNCTION__);
		return 0;
	}

	HANDLE hCurrentProcess = GetCurrentProcess();
	if (!DuplicateHandle(hCurrentProcess, hThread, hCurrentProcess, &hDupHandle, THREAD_QUERY_INFORMATION, FALSE, 0)){
		SetLastError(ERROR_ACCESS_DENIED);
		MyTrace("%s(): cannot found open thread", __FUNCTION__);
		return 0;
	}

	UINT32 ThreadQuerySetWin32StartAddress = 9;
	ntStatus = NtQueryInformationThread(hDupHandle, ThreadQuerySetWin32StartAddress, &dwStartAddress, 
		sizeof(PVOID), NULL);
	CloseHandle(hDupHandle);
	if (ntStatus != 0) {
		MyTrace("%s(): NtQueryInformationThread() failed. status: %x, threadHandle: %x, threadId: %d", 
			__FUNCTION__, ntStatus, hThread, GetThreadId(hThread));
		return 0;
	}

	return dwStartAddress;
}
Esempio n. 4
0
void XDbgProxy::GetThreadContext(ApiCallPacket& inPkt)
{
	// MyTrace("%s()", __FUNCTION__);

	ApiReturnPakcet outPkt;
	HANDLE hThread;

	hThread = threadIdToHandle(inPkt.SuspendThread.threadId);

	if (hThread == NULL || hThread == (HANDLE )-1) {
		outPkt.GetThreadContext.result = FALSE;
		SetLastError(ERROR_INVALID_PARAMETER);
		MyTrace("%s(): cannot found the threadId: %d", __FUNCTION__, inPkt.GetThreadContext.threadId);
	} else {
		outPkt.GetThreadContext.ctx.ContextFlags = inPkt.GetThreadContext.contextFlags;
		outPkt.GetThreadContext.result = ::GetThreadContext(hThread, &outPkt.GetThreadContext.ctx);
	}

	outPkt.lastError = GetLastError();

#ifdef _API_TRACE
	MyTrace("%s(): th: %x, tid: %d, result: %d, errno: %d", __FUNCTION__, hThread,
		inPkt.GetThreadContext.threadId, outPkt.GetThreadContext.result, outPkt.lastError);
#endif

	sendApiReturn(outPkt);
}
Esempio n. 5
0
void XDbgProxy::WriteProcessMemory(ApiCallPacket& inPkt)
{
	// MyTrace("%s()", __FUNCTION__);
	assert(inPkt.WriteProcessMemory.size <= MAX_MEMORY_BLOCK);
	ApiReturnPakcet outPkt;

	PVOID addr = inPkt.WriteProcessMemory.addr;
	PUCHAR buffer = inPkt.WriteProcessMemory.buffer;
	SIZE_T size = inPkt.WriteProcessMemory.size;
	
	__try {
		outPkt.WriteProcessMemory.result = ::WriteProcessMemory(GetCurrentProcess(), addr,
			buffer, size, &outPkt.WriteProcessMemory.writtenSize);
	}
	__except (EXCEPTION_EXECUTE_HANDLER) {
		outPkt.WriteProcessMemory.result = FALSE;
	}

	outPkt.lastError = GetLastError();
#ifdef _API_TRACE
	MyTrace("%s(): addr: %p, size: %d, result: %d, errno: %d", __FUNCTION__, addr, size, 
		outPkt.WriteProcessMemory.result, outPkt.lastError);
#endif
	sendApiReturn(outPkt);
}
Esempio n. 6
0
DWORD WINAPI GetProcessIdFromHandle(HANDLE hProcess)
{
	NTSTATUS ntStatus;
	if (NtQueryInformationProcess == NULL)
		NtQueryInformationProcess = (pNtQIT)GetProcAddress(GetModuleHandle("ntdll.dll"),
		"NtQueryInformationProcess");

	if (NtQueryInformationProcess == NULL) {
		MyTrace("%s(): cannot found NtQueryInformationThread()", __FUNCTION__);
		return 0;
	}

	HANDLE hProcess2;
	if (!DuplicateHandle(GetCurrentProcess(), hProcess, GetCurrentProcess(), &hProcess2,
		PROCESS_ALL_ACCESS, FALSE, 0))
		return 0;

	UINT32 ProcessBasicInformation = 0;
	PROCESS_BASIC_INFORMATION bi;
	ntStatus = NtQueryInformationProcess(hProcess2, ProcessBasicInformation, &bi, sizeof(bi), NULL);
	CloseHandle(hProcess2);
	if (ntStatus != 0)
		return 0;

	return (DWORD)bi.UniqueProcessId;
}
Esempio n. 7
0
DWORD WINAPI GetThreadIdFromHandle(HANDLE hThread, LPDWORD processId)
{
	NTSTATUS ntStatus;
	if (NtQueryInformationThread == NULL)
		NtQueryInformationThread = (pNtQIT)GetProcAddress(GetModuleHandle("ntdll.dll"),
		"NtQueryInformationThread");

	if (NtQueryInformationThread == NULL) {
		MyTrace("%s(): cannot found NtQueryInformationThread()", __FUNCTION__);
		return 0;
	}

	HANDLE hThread2;
	if (!DuplicateHandle(GetCurrentProcess(), hThread, GetCurrentProcess(), &hThread2, 
		THREAD_ALL_ACCESS, FALSE, 0))
		return 0;

	UINT32 ThreadBasicInformation = 0;
	THREAD_BASIC_INFORMATION bi;
	ntStatus = NtQueryInformationThread(hThread2, ThreadBasicInformation, &bi, sizeof(bi), NULL);
	CloseHandle(hThread2);
	if (ntStatus != 0)
		return 0;

	if (processId)
		*processId = (DWORD )bi.ClientId.UniqueProcess;

	return (DWORD )bi.ClientId.UniqueThread;
}
Esempio n. 8
0
void XDbgProxy::SuspendThread(ApiCallPacket& inPkt)
{
	// MyTrace("%s()", __FUNCTION__);

	ApiReturnPakcet outPkt;
	HANDLE hThread;

#ifdef _DEBUG
	if (inPkt.SuspendThread.threadId == getId() || inPkt.SuspendThread.threadId == _apiThread.getId()) {
		// log error
		assert(false);
		outPkt.SuspendThread.result = -1;
		SetLastError(ERROR_INVALID_PARAMETER);
		sendApiReturn(outPkt);
	}
#endif
	
	hThread = threadIdToHandle(inPkt.SuspendThread.threadId);

	if (hThread == NULL) {
		outPkt.SuspendThread.result = -1;
		SetLastError(ERROR_INVALID_PARAMETER);
	} else {
		outPkt.SuspendThread.result = ::SuspendThread(hThread);
	}

	outPkt.lastError = GetLastError();

#ifdef _API_TRACE
	MyTrace("%s(): threadId: %d, result: %d, errno: %d", __FUNCTION__, 
		inPkt.SuspendThread.threadId, outPkt.SuspendThread.result, outPkt.lastError);
#endif

	sendApiReturn(outPkt);
}
Esempio n. 9
0
void XDbgProxy::sendThreadInfo()
{
	MyTrace("%s()", __FUNCTION__);

	DebugEventPacket event;
	memset(&event, 0, sizeof(event));
	DEBUG_EVENT& msg = event.event;
	DebugAckPacket ack;

	msg.dwDebugEventCode = CREATE_THREAD_DEBUG_EVENT;
	msg.dwProcessId = XDbgGetCurrentProcessId();
	
	DWORD threadId = getFirstThread();
	while (threadId) {

		msg.dwThreadId = threadId;
		msg.u.CreateThread.hThread = NULL;
		msg.u.CreateThread.lpStartAddress = (LPTHREAD_START_ROUTINE)
			GetThreadStartAddress(threadId);

		msg.u.CreateThread.lpThreadLocalBase = GetThreadTeb(threadId);
		sendDbgEvent(event, ack, false);
		threadId = getNextThread();
	}
}
Esempio n. 10
0
void XDbgProxy::sendModuleInfo(DWORD firstThread)
{
	MyTrace("%s()", __FUNCTION__);

	DebugEventPacket event;
	memset(&event, 0, sizeof(event));
	DEBUG_EVENT& msg = event.event;
	DebugAckPacket ack;

	msg.dwDebugEventCode = LOAD_DLL_DEBUG_EVENT;
	msg.dwProcessId = XDbgGetCurrentProcessId();
	msg.dwThreadId = firstThread;

	char modName[MAX_PATH + 1] = {0};

	HMODULE hMainModule = GetModuleHandle(NULL);

	HMODULE hModules[512];
	DWORD len;
	if (!EnumProcessModules(XDbgGetCurrentProcess(), hModules, sizeof(hModules), &len)) {
		// log error
		assert(false);
		return;
	}

	len /= sizeof(HMODULE);
	MyTrace("%s(): module count: %d", __FUNCTION__, len);

	for (DWORD i = 0; i < len; i++) {
		if (hMainModule == hModules[i])
			continue;

		msg.u.LoadDll.dwDebugInfoFileOffset = 0;
		msg.u.LoadDll.fUnicode = 0;
		msg.u.LoadDll.hFile = NULL;
		msg.u.LoadDll.lpBaseOfDll = hModules[i];
		memset(modName, 0, sizeof(modName));
		if (GetModuleFileName(hModules[i], modName, MAX_PATH) == 0 || modName[0] == '\0')
			continue;
		msg.u.LoadDll.lpImageName = modName;
		sendDbgEvent(event, ack, false);
		MyTrace("%s(): module: %s, %p", __FUNCTION__, modName, msg.u.LoadDll.lpBaseOfDll);
	}
}
Esempio n. 11
0
int WINAPI iso_patch_list(int iso_type, char *iso_file, int need_ecc_edc, char *patch_list_file)
{

	int nRet;
	int nErrLine = 0;

	//先模拟写入,测试合法性
	nRet = iso_patch_list_core(iso_type, iso_file, need_ecc_edc, patch_list_file, 1, &nErrLine);
	if (nRet < 0)
	{
		MyTrace("%4d | error(%d) %s\n", nErrLine, nRet, error_string[abs(nRet)] );
		return nRet;
	}
	
	MyTrace("-----------------------------------------------------------------------------\n");
	MyTrace("Press any key to start patching, press CTRL+BREAK to abort.");
	getch();
	MyTrace("\n");

	//全部合法了以后真实写入
	nRet = iso_patch_list_core(iso_type, iso_file, need_ecc_edc, patch_list_file, 0, &nErrLine);
	if (nRet < 0)
	{
		MyTrace("%4d | error(%d) %s\n", nErrLine, nRet, error_string[abs(nRet)] );
		return nRet;
	}

	MyTrace("-----------------------------------------------------------------------------\n");
	return nRet;
}
Esempio n. 12
0
void XDbgProxy::VirtualProtectEx(ApiCallPacket& inPkt)
{
	ApiReturnPakcet outPkt;
	outPkt.VirtualProtectEx.result = ::VirtualProtect(inPkt.VirtualProtectEx.addr,
		inPkt.VirtualProtectEx.size, inPkt.VirtualProtectEx.prot,
		&outPkt.VirtualProtectEx.oldProt);
	outPkt.lastError = GetLastError();

#ifdef _API_TRACE
	MyTrace("%s(): addr: %p, result: %d, errno: %d", __FUNCTION__, inPkt.VirtualProtectEx.addr,
		outPkt.VirtualProtectEx.result, outPkt.lastError);
#endif

	sendApiReturn(outPkt);
}
Esempio n. 13
0
void XDbgProxy::_GetModuleFileNameExW(ApiCallPacket& inPkt)
{
	ApiReturnPakcet outPkt;
	outPkt._GetModuleFileNameExW.result = ::GetModuleFileNameExW(GetCurrentProcess(), 
		inPkt._GetModuleFileNameExW.hMod, outPkt._GetModuleFileNameExW.fileName, 
		sizeof(outPkt._GetModuleFileNameExW.fileName));

	outPkt.lastError = GetLastError();

#ifdef _API_TRACE
	MyTrace("%s(): hMod: %p, result: %d, errno: %d", __FUNCTION__, inPkt._GetModuleFileNameExW.hMod,
		outPkt._GetModuleFileNameExW.result, outPkt.lastError);
#endif

	sendApiReturn(outPkt);
}
Esempio n. 14
0
BOOL injectDllByRemoteThread(DWORD pid, HMODULE hInst)
{
	char dllPath[MAX_PATH];
	if (!GetModuleFileName((HMODULE)hInst, dllPath, sizeof(dllPath) - 1)) {
		assert(false);
		return FALSE;
	}

	if (!LoadRemoteDll(pid, dllPath)) {
		// assert(false);
		MyTrace("injectDll(%u) failed", pid);
		return FALSE;
	}

	return TRUE;
}
Esempio n. 15
0
void XDbgProxy::VirtualQueryEx(ApiCallPacket& inPkt)
{
	// MyTrace("%s()", __FUNCTION__);

	ApiReturnPakcet outPkt;
	outPkt.VirtualQueryEx.result = ::VirtualQuery(inPkt.VirtualQueryEx.addr, &outPkt.VirtualQueryEx.memInfo,
		sizeof(outPkt.VirtualQueryEx.memInfo));
	
	outPkt.lastError = GetLastError();

#ifdef _API_TRACE
	MyTrace("%s(): addr: %p, result: %d, errno: %d", __FUNCTION__, inPkt.VirtualQueryEx.addr, 
		outPkt.VirtualQueryEx.result, outPkt.lastError);
#endif

	sendApiReturn(outPkt);
}
Esempio n. 16
0
bool XDbgProxy::initialize()
{
	_evtQueueEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
	if (!(/* _threadHandled && _dllHandled && */ _evtQueueEvent)) {
		assert(false);
		return false;
	}

	if (!InitWin32ApiWrapper()) {
		assert(false);
		return false;
	}

	/* if (!createPipe())
		return false; */

	_vehCookie = AddVectoredExceptionHandler(1, &XDbgProxy::_VectoredHandler);
	if (_vehCookie == NULL)
		return false;

	typedef VOID (CALLBACK *PLDR_DLL_NOTIFICATION_FUNCTION)(ULONG, PCLDR_DLL_NOTIFICATION_DATA, PVOID);
	typedef NTSTATUS (NTAPI *LdrRegDllNotifFunc)(ULONG, PLDR_DLL_NOTIFICATION_FUNCTION, PVOID, PVOID *);
	LdrRegDllNotifFunc LdrRegisterDllNotification = (LdrRegDllNotifFunc )GetProcAddress(
		GetModuleHandle("ntdll.dll"), "LdrRegisterDllNotification");
	if (LdrRegisterDllNotification) {
		
		if (LdrRegisterDllNotification(0, &XDbgProxy::_LdrDllNotification, NULL, &_dllNotifCooike) != 0) {
			// log error
			assert(false);			
		}
	}

	MyTrace("%s(): starting thread.", __FUNCTION__);
	_stopFlag = false;
	if (!start()) {
		assert(false);
		return false;
	}
	
	if (!_apiThread.start()) {
		assert(false);
		return false;
	}

	return true;
}
Esempio n. 17
0
void XDbgProxy::ResumeThread(ApiCallPacket& inPkt)
{
	// MyTrace("%s()", __FUNCTION__);

	ApiReturnPakcet outPkt;
	HANDLE hThread;

#ifdef _DEBUG
	if (inPkt.SuspendThread.threadId == getId() || inPkt.SuspendThread.threadId == _apiThread.getId()) {
		// log error
		assert(false);
		outPkt.ResumeThread.result = -1;
		SetLastError(ERROR_INVALID_PARAMETER);
		sendApiReturn(outPkt);
	}
#endif

	hThread = threadIdToHandle(inPkt.SuspendThread.threadId);

	if (hThread == NULL) {

		// when the thread create by suspend flag, it's not in the thread list
		hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, inPkt.SuspendThread.threadId);
		if (hThread == NULL) {
			outPkt.ResumeThread.result = -1;
			SetLastError(ERROR_INVALID_PARAMETER);
		} else 	{
			outPkt.ResumeThread.result = ::ResumeThread(hThread);
			CloseHandle(hThread);
		}
	} else {
		outPkt.ResumeThread.result = ::ResumeThread(hThread);
	}

	outPkt.lastError = GetLastError();

#ifdef _API_TRACE
	MyTrace("%s(): threadId: %d, result: %d, errno: %d", __FUNCTION__, 
		inPkt.ResumeThread.threadId, outPkt.ResumeThread.result, outPkt.lastError);
#endif

	sendApiReturn(outPkt);
}
Esempio n. 18
0
static void
default_mjpeg_log_handler(log_level_t level, const char message[])
{
	if (level != LOG_DEBUG)
		MyTrace(message);
/*
  const char *ids;

  if( (*_filter)( level ) )
    return;
  if (default_handler_id_is_set) {
    ids = default_handler_id;
  } else {
#ifdef HAVE___PROGNAME
    ids = __progname;
#else
    ids = DEFAULT_DEFAULT_ID;
#endif
  }
  switch(level) {
  case LOG_ERROR:
    fprintf(stderr, "**ERROR: [%s] %s\n", ids, message);
    break;
  case LOG_DEBUG:
    fprintf(stderr, "--DEBUG: [%s] %s\n", ids, message);
    break;
  case LOG_WARN:
    fprintf(stderr, "++ WARN: [%s] %s\n", ids, message);
    break;
  case LOG_INFO:
    fprintf(stderr, "   INFO: [%s] %s\n", ids, message);
    break;
  default:
    assert(0);
  }
  */
}
Esempio n. 19
0
void XDbgProxy::onDbgDisconnect()
{
	MyTrace("%s()", __FUNCTION__);
	// MutexGuard guard(this);
}
Esempio n. 20
0
long XDbgProxy::runApiLoop()
{	
	ApiCallPacket inPkt;
	bool attached = false;

	while (!_stopFlag) {

		if (!attached) {
			if (!createApiPipe()) {
				Sleep(100);
				continue;
			}

			if (!ConnectNamedPipe(_hApiPipe, NULL)) {

				if (GetLastError() == ERROR_PIPE_CONNECTED) {

					MyTrace("%s(): attached", __FUNCTION__);
					attached = true;
					
				} else {

					MyTrace("%s(): ConnectNamedPipe() failed.", __FUNCTION__);
					// assert(false);
					// return -1;
					CloseHandle(_hApiPipe);
					_hApiPipe = INVALID_HANDLE_VALUE;
					Sleep(100);
					continue;
				}

			} else {
				MyTrace("%s(): attached", __FUNCTION__);
				attached = true;
			}
		}

		if (!recvApiCall(inPkt)) {
			// assert(false);
			// return -1;
			attached = false;
			continue;
		}

		// MyTrace("%s(): ApiCall: id = %d", __FUNCTION__, inPkt.apiId);

		RemoteApiHandlers::iterator it;
		it = _apiHandlers.find(inPkt.apiId);
		if (it == _apiHandlers.end()) {
			assert(false);
			// return -1;
			continue;
		}

		RemoteApiHandler handler = it->second;
		(this->*handler)(inPkt);
		// MyTrace("%s(): ApiCall: id = %d completed", __FUNCTION__, inPkt.apiId);
	}

	return 0;
}
Esempio n. 21
0
long XDbgProxy::run()
{
	MyTrace("XDBG Thread started");
	
	DWORD sleepTime = 0;
	while (!_stopFlag) {

		if (_attached) {

			{
				// MutexGuard guard(this);

				DebugEventPacket event;
				DebugAckPacket ack;

				if (popDbgEvent(event)) {

					MyTrace("%s(): eventId: %d, threadId: %d", __FUNCTION__, event.event.dwDebugEventCode, 
						event.event.dwThreadId);

					switch (event.event.dwDebugEventCode) {

					case EXCEPTION_DEBUG_EVENT:
						_exceptHandleCode = AsyncVectoredHandler(event);
						break;

					case LOAD_DLL_DEBUG_EVENT:

						if (!sendDbgEvent(event, ack)) {
							break;
						}

						if (event.event.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT) {
							free(event.event.u.LoadDll.lpImageName);
						}

						break;
					case UNLOAD_DLL_DEBUG_EVENT:
						if (!sendDbgEvent(event, ack)) {
							break;
						}

						break;

					case CREATE_THREAD_DEBUG_EVENT:

						if (threadIdToHandle(event.event.dwThreadId) != NULL) {
							// reentry
							break;
						}

						if (!sendDbgEvent(event, ack)) {
							break;
						}

						addThread(event.event.dwThreadId);
						break;

					case EXIT_THREAD_DEBUG_EVENT:

						if (threadIdToHandle(event.event.dwThreadId) == NULL) {
							// reentry
							break;;
						}

						if (!sendDbgEvent(event, ack)) {
							break;
						}

						delThread(event.event.dwThreadId);
						break;

					default:
						break;
					}

					SetEvent(_evtQueueEvent);
					sleepTime = 0;
				} else {

					Sleep(sleepTime);
					sleepTime ++;
					if (sleepTime > 15) {
						sleepTime = 15;
						if (!PeekNamedPipe(_hPipe, NULL, 0, NULL, 0, NULL)) {
							_attached = false;
							CloseHandle(_hPipe);
							_hPipe = INVALID_HANDLE_VALUE;
						}
					}
				}
			}

		} else {

			if (!createEventPipe()) {
				continue;
			}

			if (!ConnectNamedPipe(_hPipe, NULL)) {
				if (GetLastError() == ERROR_PIPE_CONNECTED) {
					onDbgConnect();
					_attached = true;
					MyTrace("debugger attached");
					continue;

				} else {
					MyTrace("%s(): ConnectNamedPipe(%p) failed. errCode: %d ", __FUNCTION__, _hPipe, GetLastError());
					// assert(false);
					// return -1;
					CloseHandle(_hPipe);
					_hPipe = INVALID_HANDLE_VALUE;
					Sleep(100);
				}				

			} else {
				onDbgConnect();
				_attached = true;
				MyTrace("debugger attached");
			}
		}
	}

	return 0;
}
Esempio n. 22
0
int iso_patch_list_core(int iso_type, char *iso_file, int need_ecc_edc, char *patch_list_file, int isTestMode, int *ErrorLine)
{
	#define MAX_PATCH_BYTE	32*1024				//一行直接写入的字节最多是多少字节
	#define MAX_FILENAME	1024
	#define MAX_LINE		MAX_PATCH_BYTE+10	//一行最多是多少字节
	char buf_line[MAX_LINE];
	char buf_byte[MAX_PATCH_BYTE];				//直接写入的字节
	char patch_file[MAX_FILENAME];
	int  buf_byte_len;
	char buf_tmp[1024];
	char *off, *param;
	int iso_offset;
	int data_type;
	int patch_bytes_current, patch_bytes_total;

	FILE *fp;
	fp = fopen(patch_list_file, "r");
	if (fp == NULL) return PATCH_ERR_FILE_PATCHLST;
	*ErrorLine = 1;
	patch_bytes_total = 0;

	MyTrace("Mode         : ");
	if (iso_type == ISO9660_M1)
		MyTrace("MODE1\n");
	else if(iso_type == ISO9660_M2F1)
		MyTrace("MODE2 FORM1\n");
	else if(iso_type == ISO9660_M2F2)
		MyTrace("MODE2 FORM2\n");
	else
	{
		MyTrace("bad mode %d\n", iso_type);
		return PATCH_ERR_UNK_MODE;
	}
	MyTrace("ECC/EDC calc : ");
	if(need_ecc_edc)
		MyTrace("on\n");
	else
		MyTrace("off\n");

    MyTrace("list file    : %s\n", patch_list_file);
	MyTrace("iso  file    : %s\n", iso_file);
	MyTrace("\n");

	if(isTestMode)
		MyTrace("loading  list ...\n");
	else
		MyTrace("patching list ...\n");


	MyTrace("-----------------------------------------------------------------------------\n");
	MyTrace("line | offset   <- data      \t| length    \n");
	MyTrace("-----------------------------------------------------------------------------\n");

	for(;;*ErrorLine = *ErrorLine + 1)
	{
		if(feof(fp))break;
		memset(buf_line, 0, sizeof(buf_line));
		fgets(buf_line, MAX_LINE-1, fp);

		if( lstrlen(buf_line) >= MAX_LINE-1) return PATCH_ERR_LINETOOLONG;
		if(buf_line[lstrlen(buf_line)-1] == 0x0a) buf_line[lstrlen(buf_line)-1] = 0;

		if(buf_line[0] == 0)continue;
		if(buf_line[0] == ';')continue;
		if(buf_line[0] == '#')continue;

		data_type = 0;
		if(buf_line[8] == ',')data_type = 1;
		if(buf_line[8] == '*')data_type = 2;
		if(buf_line[8] == '@')data_type = 3;	//直接写入RAW 2352扇区
		if (data_type == 0) return PATCH_ERR_UNK_COMMAND;

		buf_line[8] = 0;	//形成off字符串
		off = buf_line;
		param = buf_line + 8 + 1;
		sscanf(off, "%x", &iso_offset);
		sprintf(buf_tmp, "%08X", iso_offset);
 		if(lstrcmpi(buf_tmp, off) != 0)
		{
			fclose(fp);
			return PATCH_ERR_OFFSET;
		}
		if (iso_offset <= 0) return PATCH_ERR_OFFSET;
		int sector_start = iso_offset % 2352;
		if(data_type != 3)
			if(sector_start < ISO9660_DATA_START[iso_type] || sector_start >= ISO9660_DATA_START[iso_type]+ISO9660_DATA_SIZE[iso_type]) return PATCH_ERR_OFFSET;

		if(data_type == 1)	//命令处理
		{
			get_path(patch_list_file, buf_tmp, sizeof(buf_tmp));
			sprintf(patch_file, "%s%s", buf_tmp, param);

			if(isTestMode)
			{
				MyTrace("%4d | %08X", *ErrorLine, iso_offset);
				MyTrace(" <- %s", param);
				patch_bytes_current = get_filelen(patch_file);
			}
			else
			{
				MyTrace("%4d | %08X", *ErrorLine, iso_offset);
				MyTrace(" <- %s ...", param);
				patch_bytes_current = iso_patch_file(iso_type, iso_file, need_ecc_edc, iso_offset, patch_file);
				if (patch_bytes_current < 0) return patch_bytes_current;
				patch_bytes_total += patch_bytes_current;
			}

		}
		else if (data_type == 2)
		{
			memset(buf_byte, 0, sizeof(buf_byte));
			buf_byte_len = HexStr2Byte(param, buf_byte, sizeof(buf_byte));
			if(buf_byte_len < 0) return PATCH_ERR_BYTES;

			if(isTestMode)
			{
				MyTrace("%4d | %08X", *ErrorLine, iso_offset);
				MyTrace(" <- hex string   ");
				patch_bytes_current = buf_byte_len;
			}
			else
			{
				MyTrace("%4d | %08X", *ErrorLine, iso_offset);
				MyTrace(" <- hex string ...");
				patch_bytes_current = iso_patch_byte(iso_type, iso_file, need_ecc_edc, iso_offset, buf_byte, buf_byte_len);
				if (patch_bytes_current < 0) return patch_bytes_current;
				patch_bytes_total += patch_bytes_current;
			}

		}
		else
		{
			get_path(patch_list_file, buf_tmp, sizeof(buf_tmp));
			sprintf(patch_file, "%s%s", buf_tmp, param);

			if(isTestMode)
			{
				MyTrace("%4d | %08X", *ErrorLine, iso_offset);
				MyTrace(" <-RAW %s", param);
				patch_bytes_current = get_filelen(patch_file);
			}
			else
			{
				MyTrace("%4d | %08X", *ErrorLine, iso_offset);
				MyTrace(" <-RAW %s ...", param);
				patch_bytes_current = iso_patch_sector(iso_file, iso_offset, patch_file);
				if (patch_bytes_current < 0) return patch_bytes_current;
				patch_bytes_total += patch_bytes_current;
			}

		}
		
		MyTrace("\t| %d\n", patch_bytes_current);
	}

	fclose(fp);
	*ErrorLine = 0;
	return patch_bytes_total;
}