Exemplo n.º 1
0
DWORD Inject(HANDLE hProc, LPWSTR engine)
{
	LPVOID lpvAllocAddr = 0;
	DWORD dwWrite = 0x1000, len = 0;
	HANDLE hTH;
	WCHAR path[MAX_PATH];
	LPWSTR p;
	if (!IthCheckFile(DllName)) return -1;
	p = GetMainModulePath();
	len = wcslen(p);
	memcpy(path, p, len << 1);
	memset(path + len, 0, (MAX_PATH - len) << 1);
	for (p = path + len; *p != L'\\'; p--); //Always a \ after drive letter.
	p++;
	wcscpy(p, DllName);

	NtAllocateVirtualMemory(hProc, &lpvAllocAddr, 0, &dwWrite, MEM_COMMIT, PAGE_READWRITE);
	if (lpvAllocAddr == 0) return -1;

	CheckThreadStart();

	//Copy module path into address space of target process.
	NtWriteVirtualMemory(hProc, lpvAllocAddr, path, MAX_PATH << 1, &dwWrite);

	hTH = IthCreateThread(LoadLibrary, (DWORD)lpvAllocAddr, hProc);
	if (hTH == 0 || hTH == INVALID_HANDLE_VALUE)
	{
		ConsoleOutput(ErrorRemoteThread);
		return -1;
	}
	NtWaitForSingleObject(hTH, 0, 0);

	THREAD_BASIC_INFORMATION info;
	NtQueryInformationThread(hTH, ThreadBasicInformation, &info, sizeof(info), &dwWrite);
	NtClose(hTH);
	if (info.ExitStatus != 0)
	{
		wcscpy(p, engine);
		NtWriteVirtualMemory(hProc, lpvAllocAddr, path, MAX_PATH << 1, &dwWrite);
		hTH = IthCreateThread(LoadLibrary, (DWORD)lpvAllocAddr, hProc);
		if (hTH == 0 || hTH == INVALID_HANDLE_VALUE)
		{
			ConsoleOutput(ErrorRemoteThread);
			return -1;
		}
		NtWaitForSingleObject(hTH, 0, 0);
		NtClose(hTH);
	}

	dwWrite = 0;
	NtFreeVirtualMemory(hProc, &lpvAllocAddr, &dwWrite, MEM_RELEASE);
	return info.ExitStatus;
}
Exemplo n.º 2
0
//---------------------------------------------------------------------------
void tTJSScriptBlock::Dump() const
{
    std::list<tTJSInterCodeContext *>::const_iterator i =
        InterCodeContextList.begin();
    while(i != InterCodeContextList.end())
    {
        ConsoleOutput(TJS_W(""), (void*)this);
        tjs_char ptr[256];
        TJS_sprintf(ptr, TJS_W(" 0x%p"), (*i));
        ConsoleOutput((ttstr(TJS_W("(")) + ttstr((*i)->GetContextTypeName()) +
                       TJS_W(") ") + ttstr((*i)->GetName()) + ptr).c_str(), (void*)this);
        (*i)->Disassemble(ConsoleOutput, (void*)this);
        i++;
    }
}
Exemplo n.º 3
0
void
Debug::TimeStart(const uni_char* key, const uni_char* s)
{
	if (!g_dbg_timing || !DoDebugging(key))
		return;

	struct DebugTime time;
	GetTime(time);

	TimeStringLink* link = (TimeStringLink*)Find(&g_dbg_timers, key);
	if (link == NULL)
	{
		// Not found, so put one in.
		OP_NEW(TimeStringLink, (time, key, &g_dbg_timers));
	}
	else
	{
		link->time = time;
	}

	Indent();

    uni_snprintf(g_dbg_mybuff, DEBUG_DEBUGBUFFERSIZE, UNI_L("%s: %s %ld.%03d\n"), key, s, time.sec, time.msec);

	if (g_dbg_use_file)
		PrintToFile(g_dbg_mybuff);

	if (g_dbg_system_debug)
		dbg_systemoutput(g_dbg_mybuff);

#ifdef OPERA_CONSOLE
	if (g_dbg_console)
		ConsoleOutput(g_dbg_mybuff);
#endif
}
Exemplo n.º 4
0
IHFSERVICE DWORD IHFAPI IHF_InjectByPID(DWORD pid, LPWSTR engine)
{
	WCHAR str[0x80];
	DWORD s;
	if (!running) return 0;
	if (pid == current_process_id) 
	{
		ConsoleOutput(SelfAttach);
		return -1;
	}
	if (man->GetProcessRecord(pid))
	{
		ConsoleOutput(AlreadyAttach);
		return -1;
	}
	swprintf(str, L"ITH_HOOKMAN_%d", pid);
	NtClose(IthCreateMutex(str, 0, &s));
	if (s) return -1;
	CLIENT_ID id;
	OBJECT_ATTRIBUTES oa = {};
	HANDLE hProc;
	id.UniqueProcess = pid;
	id.UniqueThread = 0;
	oa.uLength=sizeof(oa);
	if (!NT_SUCCESS(NtOpenProcess(&hProc,
		PROCESS_QUERY_INFORMATION|
		PROCESS_CREATE_THREAD|
		PROCESS_VM_OPERATION|
		PROCESS_VM_READ|
		PROCESS_VM_WRITE,
		&oa, &id)))
	{
		ConsoleOutput(ErrorOpenProcess);
		return -1;
	}
	
	if (engine == 0) engine = EngineName;
	DWORD module = Inject(hProc,engine);
	NtClose(hProc);
	if (module == -1) return -1;
	swprintf(str, FormatInject, pid, module);
	ConsoleOutput(str);
	return module;
}
Exemplo n.º 5
0
/* static */
void Debug::FlushDbgBuffer()
{
	if (g_dbg_use_file)
		PrintToFile(g_dbg_mybuff);

	if (g_dbg_system_debug)
		dbg_systemoutput(g_dbg_mybuff);

#ifdef OPERA_CONSOLE
	if (g_dbg_console)
		ConsoleOutput(g_dbg_mybuff);
#endif
}
Exemplo n.º 6
0
//---------------------------------------------------------------------------
void tTJSScriptBlock::ExecuteTopLevelScript(tTJSVariant *result,
        iTJSDispatch2 * context)
{
    if(TopLevelContext)
    {
#ifdef TJS_DEBUG_PROFILE_TIME
        clock_t start = clock();
#endif
        TopLevelContext->FuncCall(0, NULL, NULL, result, 0, NULL, context);
#ifdef TJS_DEBUG_PROFILE_TIME
        tjs_char str[100];
        TJS_sprintf(str, TJS_W("%d"), clock() - start);
        ConsoleOutput(str, (void*)this);
#endif
    }
}
Exemplo n.º 7
0
void
Debug::TimeEnd(const uni_char* key, const uni_char* s)
{
	if (!g_dbg_timing || !DoDebugging(key))
		return;

	struct DebugTime time;
	GetTime(time);

	TimeStringLink* link = (TimeStringLink*)Find(&g_dbg_timers, key);
	if (link == NULL)
	{
		// Not found!
		// This puts an entry in the g_dbg_timers vector
		// it won't be very informative, but it keeps
		// things going OK.
		link = OP_NEW(TimeStringLink, (time, key, &g_dbg_timers));
	}

    int secs, millis;
    millis = time.msec - link->time.msec;
    secs = time.sec - link->time.sec;
    if(millis < 0)
	{
		millis += 1000;
		--secs;
	}

	Indent();

    uni_snprintf(g_dbg_mybuff, DEBUG_DEBUGBUFFERSIZE, UNI_L("%s: %s %d.%03d\n"), key, s, secs, millis);

	if (g_dbg_use_file)
		PrintToFile(g_dbg_mybuff);

	if (g_dbg_system_debug)
		dbg_systemoutput(g_dbg_mybuff);

#ifdef OPERA_CONSOLE
	if (g_dbg_console)
		ConsoleOutput(g_dbg_mybuff);
#endif
}
Exemplo n.º 8
0
IHFSERVICE DWORD IHFAPI IHF_GetPIDByName(LPWSTR pwcTarget)
{
	DWORD dwSize = 0x20000, dwExpectSize = 0;
	LPVOID pBuffer = 0;
	SYSTEM_PROCESS_INFORMATION *spiProcessInfo;
	DWORD dwPid = 0;
	DWORD dwStatus;

	NtAllocateVirtualMemory(NtCurrentProcess(), &pBuffer, 0, &dwSize, MEM_COMMIT, PAGE_READWRITE);
	dwStatus = NtQuerySystemInformation(SystemProcessInformation, pBuffer, dwSize, &dwExpectSize);
	if (!NT_SUCCESS(dwStatus))
	{
		NtFreeVirtualMemory(NtCurrentProcess(),&pBuffer,&dwSize,MEM_RELEASE);
		if (dwStatus != STATUS_INFO_LENGTH_MISMATCH || dwExpectSize < dwSize) return 0;
		dwSize = (dwExpectSize | 0xFFF) + 0x4001; //
		pBuffer = 0;
		NtAllocateVirtualMemory(NtCurrentProcess(), &pBuffer, 0, &dwSize, MEM_COMMIT, PAGE_READWRITE);
		dwStatus = NtQuerySystemInformation(SystemProcessInformation, pBuffer, dwSize, &dwExpectSize);
		if (!NT_SUCCESS(dwStatus)) goto _end;
	}

	for (spiProcessInfo = (SYSTEM_PROCESS_INFORMATION*)pBuffer; spiProcessInfo->dNext;)
	{
		spiProcessInfo = (SYSTEM_PROCESS_INFORMATION*)
			((DWORD)spiProcessInfo + spiProcessInfo -> dNext);
		if (_wcsicmp(pwcTarget, spiProcessInfo -> usName.Buffer) == 0) 
		{
			dwPid = spiProcessInfo->dUniqueProcessId;
			break;
		}
	}
	if (dwPid == 0) ConsoleOutput(ErrorNoProcess);
_end:
	NtFreeVirtualMemory(NtCurrentProcess(),&pBuffer,&dwSize,MEM_RELEASE);
	return dwPid;
}
Exemplo n.º 9
0
void tTJSScriptBlock::SetText(tTJSVariant *result, const tjs_char *text,
                              iTJSDispatch2 * context, bool isexpression)
{
    TJS_F_TRACE("tTJSScriptBlock::SetText");


    // compiles text and executes its global level scripts.
    // the script will be compiled as an expression if isexpressn is true.
    if(!text) return;
    if(!text[0]) return;

    TJS_D((TJS_W("Counting lines ...\n")))

    Script = new tjs_char[TJS_strlen(text)+1];
    TJS_strcpy(Script, text);

    // calculation of line-count
    tjs_char *ls = Script;
    tjs_char *p = Script;
    while(*p)
    {
        if(*p == TJS_W('\r') || *p == TJS_W('\n'))
        {
            LineVector.push_back(int(ls - Script));
            LineLengthVector.push_back(int(p - ls));
            if(*p == TJS_W('\r') && p[1] == TJS_W('\n')) p++;
            p++;
            ls = p;
        }
        else
        {
            p++;
        }
    }

    if(p!=ls)
    {
        LineVector.push_back(int(ls - Script));
        LineLengthVector.push_back(int(p - ls));
    }

    try
    {

        // parse and execute
#ifdef TJS_DEBUG_PROFILE_TIME
        {
            tTJSTimeProfiler p(parsetime);
#endif

            Parse(text, isexpression, result != NULL);

#ifdef TJS_DEBUG_PROFILE_TIME
        }

        {
            char buf[256];
            sprintf(buf, "parsing : %d", parsetime);
            OutputDebugString(buf);
            if(parsetime)
            {
                sprintf(buf, "Commit : %d (%d%%)", time_Commit, time_Commit*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "yylex : %d (%d%%)", time_yylex, time_yylex*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "MakeNP : %d (%d%%)", time_make_np, time_make_np*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "GenNodeCode : %d (%d%%)", time_GenNodeCode, time_GenNodeCode*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "  PutCode : %d (%d%%)", time_PutCode, time_PutCode*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "  PutData : %d (%d%%)", time_PutData, time_PutData*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "  this_proxy : %d (%d%%)", time_this_proxy, time_this_proxy*100/parsetime);
                OutputDebugString(buf);

                sprintf(buf, "ns::Push : %d (%d%%)", time_ns_Push, time_ns_Push*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Pop : %d (%d%%)", time_ns_Pop, time_ns_Pop*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Find : %d (%d%%)", time_ns_Find, time_ns_Find*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Remove : %d (%d%%)", time_ns_Remove, time_ns_Remove*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Commit : %d (%d%%)", time_ns_Commit, time_ns_Commit*100/parsetime);
                OutputDebugString(buf);

            }
        }
#endif

#ifdef TJS_DEBUG_DISASM
        std::list<tTJSInterCodeContext *>::iterator i =
            InterCodeContextList.begin();
        while(i != InterCodeContextList.end())
        {
            ConsoleOutput(TJS_W(""), (void*)this);
            ConsoleOutput((*i)->GetName(), (void*)this);
            (*i)->Disassemble(ConsoleOutput, (void*)this);
            i++;
        }
#endif

        // execute global level script
        ExecuteTopLevelScript(result, context);
    }
    catch(...)
    {
        if(InterCodeContextList.size() != 1)
        {
            if(TopLevelContext) TopLevelContext->Release(), TopLevelContext = NULL;
            while(ContextStack.size())
            {
                ContextStack.top()->Release();
                ContextStack.pop();
            }
        }
        throw;
    }

    if(InterCodeContextList.size() != 1)
    {
        // this is not a single-context script block
        // (may hook itself)
        // release all contexts and global at this time
        if(TopLevelContext) TopLevelContext->Release(), TopLevelContext = NULL;
        while(ContextStack.size())
        {
            ContextStack.top()->Release();
            ContextStack.pop();
        }
    }
}