extern "C" WCHAR* __stdcall GetSQLiteVersion() { #ifdef WIN64BIT return AllocAndCopy(_T(SQLITE_VERSION) _T(" (64 bit)")); #else return AllocAndCopy(_T(SQLITE_VERSION) _T(" (32 bit)")); #endif }
extern "C" WCHAR* __stdcall GetString(HANDLE hDB, HANDLE hRet, int nRow, int nCol) { RowsRet * pRows = (RowsRet*) hRet; wstring sRet = pRows->Rows[nRow][nCol]; return AllocAndCopy(sRet.c_str()); }
BOOL dump(DWORD dwOEP, struct dll *NewDLL, DWORD dwStartIAT) { DWORD dwBase; DWORD dwLen; BYTE modulePath[MAX_PATH + 8]; PBYTE pDump; PIMAGE_DOS_HEADER pDosHeader; PIMAGE_NT_HEADERS pPE; PIMAGE_SECTION_HEADER pSection; PIMAGE_SECTION_HEADER pSectionHeaders; DWORD curseur, i; HANDLE hFile; DWORD NbByteWritten; PBYTE IAT = NULL; DWORD dwAllocSize = 0; DWORD dwRVAIAT = 0; dwBase = (DWORD)GetModuleHandle(NULL); if (((dwLen = GetModuleFileNameA((HMODULE) dwBase, modulePath, MAX_PATH + 1)) >= MAX_PATH) || (!dwLen)) return FALSE; if (!(pDump = AllocAndCopy(dwBase, &dwAllocSize))) return FALSE; pDosHeader = (PIMAGE_DOS_HEADER)pDump; pPE = (PIMAGE_NT_HEADERS)(pDump + pDosHeader->e_lfanew); pSection = (PIMAGE_SECTION_HEADER)((PCHAR)pPE + sizeof(IMAGE_FILE_HEADER) + pPE->FileHeader.SizeOfOptionalHeader + sizeof(DWORD)); pPE->OptionalHeader.FileAlignment = 0x200; for (curseur = AlignSize(pPE->OptionalHeader.SizeOfHeaders, pPE->OptionalHeader.FileAlignment) - 1; ! pDump[curseur]; curseur --); pSectionHeaders = (PIMAGE_SECTION_HEADER)((PBYTE)pPE + sizeof(IMAGE_FILE_HEADER) + pPE->FileHeader.SizeOfOptionalHeader + sizeof(DWORD)); dwRVAIAT = AlignSize(pSectionHeaders[pPE->FileHeader.NumberOfSections - 1].VirtualAddress + pSectionHeaders[pPE->FileHeader.NumberOfSections - 1].Misc.VirtualSize, pPE->OptionalHeader.SectionAlignment); IAT = Reconstruct(dwStartIAT, NewDLL, dwRVAIAT); memcpy(pDump, dwBase, dwAllocSize); curseur = AlignSize(curseur + 1, pPE->OptionalHeader.FileAlignment); pPE->OptionalHeader.SizeOfHeaders = curseur; for (i = 0; i < pPE->FileHeader.NumberOfSections; i++) { memcpy(pDump + curseur, pDump + pSection[i].VirtualAddress, pSection[i].Misc.VirtualSize); pSection[i].PointerToRawData = curseur; curseur += pSection[i].Misc.VirtualSize - 1; //AlignCurseur(pDump, &curseur); while ((pDump[curseur] == 0) && (((int)curseur) >= -1)) curseur--; curseur = AlignSize(curseur + 1, pPE->OptionalHeader.FileAlignment); pSection[i].SizeOfRawData = curseur - pSection[i].PointerToRawData; } strcpy(pSection[pPE->FileHeader.NumberOfSections].Name, ".suce"); pSection[pPE->FileHeader.NumberOfSections].PointerToRawData = curseur; pSection[pPE->FileHeader.NumberOfSections].Misc.VirtualSize = AlignSize(computeSizeIAT(NewDLL), pPE->OptionalHeader.SectionAlignment); pSection[pPE->FileHeader.NumberOfSections].VirtualAddress = dwRVAIAT; pSection[pPE->FileHeader.NumberOfSections].Characteristics = 0xE0000060; memcpy(pDump + curseur, IAT, pSection[pPE->FileHeader.NumberOfSections].Misc.VirtualSize); curseur += pSection[pPE->FileHeader.NumberOfSections].Misc.VirtualSize - 1; while ((pDump[curseur] == 0) && (((int)curseur) >= -1)) curseur--; curseur = AlignSize(curseur + 1, pPE->OptionalHeader.FileAlignment); pSection[pPE->FileHeader.NumberOfSections].SizeOfRawData = curseur - pSection[pPE->FileHeader.NumberOfSections].PointerToRawData; pPE->FileHeader.NumberOfSections += 1; pPE->OptionalHeader.DataDirectory[1].VirtualAddress = dwRVAIAT; pPE->OptionalHeader.DataDirectory[1].Size = computeSizeIAT(NewDLL); pPE->OptionalHeader.AddressOfEntryPoint = dwOEP - (DWORD)GetModuleHandle(0); pPE->OptionalHeader.SizeOfImage += AlignSize(computeSizeIAT(NewDLL), pPE->OptionalHeader.SectionAlignment); modulePath[dwLen - 4] = '-'; modulePath[dwLen - 3] = 'd'; modulePath[dwLen - 2] = 'u'; modulePath[dwLen - 1] = 'm'; modulePath[dwLen] = 'p'; modulePath[dwLen + 1] = 'e'; modulePath[dwLen + 2] = 'd'; modulePath[dwLen + 3] = '.'; modulePath[dwLen + 4] = 'e'; modulePath[dwLen + 5] = 'x'; modulePath[dwLen + 6] = 'e'; modulePath[dwLen + 7] = 0; if ((hFile = CreateFileA(modulePath,(GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ | FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE) return FALSE; WriteFile(hFile, pDump, curseur, &NbByteWritten, NULL); if (NbByteWritten != curseur) return FALSE; return TRUE; }
extern "C" WCHAR* __stdcall GetDBLastError(HANDLE hDB) { DbHandle* dbh = (DbHandle*) hDB; return AllocAndCopy(dbh->szLastError); }
// This function parses the xml body of the device description response from the router. Basically, we look to make sure this is a response // referencing a service we care about (WANIPConnection or WANPPPConnection), look for the "controlURL" header immediately following, and copy the addressing and URL info we need mDNSlocal void handleLNTDeviceDescriptionResponse(tcpLNTInfo *tcpInfo) { mDNS *m = tcpInfo->m; char *ptr = (char *)tcpInfo->Reply; char *end = (char *)tcpInfo->Reply + tcpInfo->nread; char *stop = mDNSNULL; mDNSs16 http_result; if (!mDNSIPPortIsZero(m->UPnPSOAPPort)) return; // already have the info we need http_result = ParseHTTPResponseCode((mDNSu8**)&ptr, (mDNSu8*)end); // Note: modifies ptr if (http_result == HTTPCode_404) LNT_ClearState(m); if (http_result != HTTPCode_200) { mDNSASLLog((uuid_t *)&m->asl_uuid, "natt.legacy.DeviceDescription", "noop", "HTTP Result", "HTTP code: %d", http_result); return; } // Always reset our flag to use WANIPConnection. We'll use WANPPPConnection if we find it and don't find WANIPConnection. m->UPnPWANPPPConnection = mDNSfalse; // find either service we care about while (ptr && ptr < end) { if (*ptr == 'W' && (strncasecmp(ptr, "WANIPConnection:1", 17) == 0)) break; ptr++; } if (ptr == end) { ptr = (char *)tcpInfo->Reply; while (ptr && ptr < end) { if (*ptr == 'W' && (strncasecmp(ptr, "WANPPPConnection:1", 18) == 0)) { m->UPnPWANPPPConnection = mDNStrue; break; } ptr++; } } if (ptr == mDNSNULL || ptr == end) { LogInfo("handleLNTDeviceDescriptionResponse: didn't find WANIPConnection:1 or WANPPPConnection:1 string"); return; } // find "controlURL", starting from where we left off while (ptr && ptr < end) { if (*ptr == 'c' && (strncasecmp(ptr, "controlURL", 10) == 0)) break; // find the first 'c'; is this controlURL? if not, keep looking ptr++; } if (ptr == mDNSNULL || ptr == end) { LogInfo("handleLNTDeviceDescriptionResponse: didn't find controlURL string"); return; } ptr += 11; // skip over "controlURL>" if (ptr >= end) { LogInfo("handleLNTDeviceDescriptionResponse: past end of buffer and no body!"); return; } // check ptr again in case we skipped over the end of the buffer // find the end of the controlURL element for (stop = ptr; stop < end; stop++) { if (*stop == '<') { end = stop; break; } } // fill in default port m->UPnPSOAPPort = m->UPnPRouterPort; // free string pointers and set to NULL if (m->UPnPSOAPAddressString != mDNSNULL) { mDNSPlatformMemFree(m->UPnPSOAPAddressString); m->UPnPSOAPAddressString = mDNSNULL; } if (m->UPnPSOAPURL != mDNSNULL) { mDNSPlatformMemFree(m->UPnPSOAPURL); m->UPnPSOAPURL = mDNSNULL; } if (ParseHttpUrl(ptr, end, &m->UPnPSOAPAddressString, &m->UPnPSOAPPort, &m->UPnPSOAPURL) != mStatus_NoError) return; // the SOAPURL should look something like "/uuid:0013-108c-4b3f0000f3dc" if (m->UPnPSOAPAddressString == mDNSNULL) { ptr = (char *)tcpInfo->Reply; while (ptr && ptr < end) { if (*ptr == 'U' && (strncasecmp(ptr, "URLBase", 7) == 0)) break; ptr++; } if (ptr < end) // found URLBase { LogInfo("handleLNTDeviceDescriptionResponse: found URLBase"); ptr += 8; // skip over "URLBase>" // find the end of the URLBase element for (stop = ptr; stop < end; stop++) { if (*stop == '<') { end = stop; break; } } if (ParseHttpUrl(ptr, end, &m->UPnPSOAPAddressString, &m->UPnPSOAPPort, mDNSNULL) != mStatus_NoError) { LogInfo("handleLNTDeviceDescriptionResponse: failed to parse URLBase"); } } // if all else fails, use the router address string if (m->UPnPSOAPAddressString == mDNSNULL) AllocAndCopy(&m->UPnPSOAPAddressString, m->UPnPRouterAddressString); } if (m->UPnPSOAPAddressString == mDNSNULL) LogMsg("handleLNTDeviceDescriptionResponse: UPnPSOAPAddressString is NULL"); else LogInfo("handleLNTDeviceDescriptionResponse: SOAP address string [%s]", m->UPnPSOAPAddressString); if (m->UPnPSOAPURL == mDNSNULL) AllocAndCopy(&m->UPnPSOAPURL, m->UPnPRouterURL); if (m->UPnPSOAPURL == mDNSNULL) LogMsg("handleLNTDeviceDescriptionResponse: UPnPSOAPURL is NULL"); else LogInfo("handleLNTDeviceDescriptionResponse: SOAP URL [%s]", m->UPnPSOAPURL); }