int processWait(Proc_ID process, int *exitCode, int waitTimeSecs) { int iReturnCode = DIAGLIB_OK; if(process == 0) { return RETURN_LOG_BAD_FUNCTION_CALL; } LOG_TIME(L"Waiting for process pid=%ld --> ",process); HANDLE hHandle; if(NULL == (hHandle = ::OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,0,process))) { LOG_LASTERROR(L"OpenProcess failed"); return RETURN_LOG_INTERNAL_ERROR; } if(DIAGLIB_OK == (iReturnCode = processWaitFromHandle(hHandle, exitCode, waitTimeSecs))) { LOG(L"DONE\n"); } if(!CloseHandle( hHandle )) { LOG_LASTERROR(L"CloseHandle failed"); } return iReturnCode; }
int EDT_CSP_FindCards() { int iRetVal = EDT_OK; DWORD err; HCRYPTPROV hProv = NULL; //Get a context if (CryptAcquireContext(&hProv, NULL, L"Belgium Identity Card CSP", PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) { err = GetLastError(); LOG(L"CryptAcquireContext \"Belgium Identity Card CSP\" failed with %d\n",err); //our old csp failed, try the minidriver if (CryptAcquireContext(&hProv, NULL, L"Microsoft Base Smart Card Crypto Provider", PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) != TRUE) { err = GetLastError(); LOG(L"CryptAcquireContext \"Microsoft Base Smart Card Crypto Provider\" failed with %d\n",err); iRetVal = EDT_ERR_CSP_FAILED; } if(iRetVal == EDT_OK) { EDT_CSP_ReadParam(hProv,PP_SMARTCARD_READER);//PP_SMARTCARD_READER not supported for WinXP, no minidriver there EDT_CSP_ReadParam(hProv,PP_SMARTCARD_GUID);//PP_SMARTCARD_GUID not supported for WinXP, no minidriver there EDT_CSP_ReadParam(hProv,PP_USER_CERTSTORE);//PP_USER_CERTSTORE not supported for WinXP, no minidriver there } } else { LOG(L"CryptAcquireContext \"Belgium Identity Card CSP\" found\n"); } if (hProv!=NULL && !CryptReleaseContext(hProv, 0)) { LOG_LASTERROR(L"CryptReleaseContext failed"); iRetVal = EDT_ERR_CSP_FAILED; } return iRetVal; }
int processStart(Proc_NAME process, Proc_ID *id, int waitTimeSecs, int *exitCode) { int iReturnCode = DIAGLIB_OK; if(process.empty() || id == NULL) { return RETURN_LOG_BAD_FUNCTION_CALL; } LOG_TIME(L"Ask for start process named '%ls' --> ",process.c_str()); STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; ZeroMemory(&siStartupInfo, sizeof(siStartupInfo)); ZeroMemory(&piProcessInfo, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); if (!CreateProcess(NULL,(LPWSTR)process.c_str(),0,0,false,0,NULL,NULL, &siStartupInfo, &piProcessInfo)) { LOG_LASTERROR(L"CreateProcess failed"); iReturnCode = DIAGLIB_ERR_PROCESS_START_FAILED; } else { *id=piProcessInfo.dwProcessId; Sleep(100); LOG(L"STARTED with pid=%ld\n",*id); if(waitTimeSecs>0) { iReturnCode=processWait(*id, exitCode, waitTimeSecs); } } return iReturnCode; }
int isModuleUsedByProcess(DWORD dwPID, LPCTSTR szLibrary, bool *found) { int iReturnCode = DIAGLIB_OK; HANDLE hModuleSnap = INVALID_HANDLE_VALUE; MODULEENTRY32 me32; *found=false; EnablePrivilege(SE_DEBUG_NAME); if(INVALID_HANDLE_VALUE == ( hModuleSnap=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID))) { LOG_LASTERROR(L"CreateToolhelp32Snapshot failed"); return RETURN_LOG_INTERNAL_ERROR; } me32.dwSize = sizeof(MODULEENTRY32); if (!Module32First (hModuleSnap, &me32)) { LOG_LASTERROR(L"Module32First failed"); if(!CloseHandle( hModuleSnap )) { LOG_LASTERROR(L"CloseHandle failed"); } return RETURN_LOG_INTERNAL_ERROR; } do { if (!lstrcmpi(szLibrary, me32.szModule)) { *found=true; break; } } while (Module32Next(hModuleSnap, &me32)); if(!CloseHandle( hModuleSnap )) { LOG_LASTERROR(L"CloseHandle failed"); } return iReturnCode; }
int processKill(Proc_ID process) { int iReturnCode = DIAGLIB_OK; if(process == 0) { return RETURN_LOG_BAD_FUNCTION_CALL; } LOG_TIME(L"Ask for killing pid=%ld --> ",process); HANDLE hHandle = NULL; if(NULL == (hHandle = ::OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION,0,process))) { LOG_LASTERROR(L"OpenProcess failed"); return RETURN_LOG_INTERNAL_ERROR; } DWORD dwExitCode = 0; if(!::TerminateProcess(hHandle,dwExitCode)) { LOG_LASTERROR(L"TerminateProcess failed"); iReturnCode = DIAGLIB_ERR_PROCESS_KILL_FAILED; } else { if(!::GetExitCodeProcess(hHandle,&dwExitCode)) { LOG_LASTERROR(L"GetExitCodeProcess failed"); iReturnCode = RETURN_LOG_INTERNAL_ERROR; } else { LOG(L"FOUND and KILLED (Return code = %ld)\n",dwExitCode); } } if(!CloseHandle( hHandle )) { LOG_LASTERROR(L"CloseHandle failed"); } return iReturnCode; }
int processWaitFromHandle(HANDLE hHandle, int *exitCode, int waitTimeSecs) { int iReturnCode = DIAGLIB_OK; if(hHandle == 0) { return RETURN_LOG_BAD_FUNCTION_CALL; } DWORD dwExitCode = WaitForSingleObject(hHandle, waitTimeSecs*1000); if (dwExitCode == WAIT_TIMEOUT) { LOG_LASTERROR(L"TIMEOUT"); iReturnCode = DIAGLIB_ERR_PROCESS_WAIT_TIMEOUT; } else if (dwExitCode != WAIT_OBJECT_0) { LOG(L"WaitForSingleObject failed\n"); iReturnCode = DIAGLIB_ERR_INTERNAL; } if(iReturnCode == DIAGLIB_OK) { if(!GetExitCodeProcess(hHandle,&dwExitCode)) { LOG_LASTERROR(L"GetExitCodeProcess failed"); } else if(dwExitCode==3010) { // Process ended with non-zero exit code LOG(L"WARNING: A reboot is needed\n"); iReturnCode=DIAGLIB_REBOOT_NEEDED; } else { LOG(L"QUIT NORMALLY\n"); } } if(exitCode) *exitCode=dwExitCode; return iReturnCode; }
int EDT_CSP_ReadParam(HCRYPTPROV hProv,DWORD dwParam) { int iRetVal = EDT_OK; BYTE *pbData = NULL; DWORD dwDataLen = 0; DWORD dwFlags = 0; if (CryptGetProvParam(hProv, dwParam, NULL, &dwDataLen, dwFlags) == TRUE ) { pbData = (BYTE *)malloc(dwDataLen); if(pbData != NULL) { if (CryptGetProvParam(hProv, dwParam, pbData, &dwDataLen, dwFlags) == TRUE ) { GUID *pTheGuid; HCERTSTORE *phCertStore; PCCERT_CONTEXT pPrevCertContext = NULL; switch(dwParam) { case PP_SMARTCARD_READER: LOG(L"SmartCard Reader: %hs\n",pbData); break; case PP_SMARTCARD_GUID: pTheGuid = (GUID *)pbData; LOG(L"SmartCard GUID: "); LOG_BYTE_ARRAY(pTheGuid->Data4,sizeof(pTheGuid->Data4)); break; case PP_USER_CERTSTORE: phCertStore = (HCERTSTORE*)pbData; pPrevCertContext = CertEnumCertificatesInStore(*phCertStore,pPrevCertContext); while(pPrevCertContext != NULL) { LOG(L"Found certificate with length %d\n",pPrevCertContext->cbCertEncoded); pPrevCertContext = CertEnumCertificatesInStore(*phCertStore,pPrevCertContext); } if ( CertCloseStore(*phCertStore,0) == FALSE ) { LOG_ERROR(L"CertCloseStore failed"); } break; default: break; } } else { LOG_LASTERROR(L"CryptGetProvParam failed 2e \n"); } free(pbData); } else { LOG_ERROR(L"malloc failed\n"); } } else { LOG_LASTERROR(L"CryptGetProvParam failed\n"); } return iRetVal; }
//////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////// PUBLIC FUNCTIONS ///////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// int processUsingLibrary(Lib_ID library, Proc_LIST *processList) { int iReturnCode = DIAGLIB_OK; if(library.empty() || processList==NULL) { return RETURN_LOG_BAD_FUNCTION_CALL; } LOG_TIME(L"Check for use of library '%ls' --> ",library.c_str()); processList->clear(); HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; bool bUsed=false; if(INVALID_HANDLE_VALUE == ( hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0))) { LOG_LASTERROR(L"CreateToolhelp32Snapshot failed"); return RETURN_LOG_INTERNAL_ERROR; } pe32.dwSize = sizeof(PROCESSENTRY32); //Get progressMax if request int progressMax = 0; if (!Process32First(hProcessSnap, &pe32)) { LOG_LASTERROR(L"Process32First failed"); if(!CloseHandle( hProcessSnap )) { LOG_LASTERROR(L"CloseHandle failed"); } return RETURN_LOG_INTERNAL_ERROR; } do { progressMax++; } while (Process32Next(hProcessSnap, &pe32)); if (!Process32First(hProcessSnap, &pe32)) { LOG_LASTERROR(L"Process32First failed"); if(!CloseHandle( hProcessSnap )) { LOG_LASTERROR(L"CloseHandle failed"); } return RETURN_LOG_INTERNAL_ERROR; } progressInit(progressMax); int count=0; do { //We avoid the process System because the isModuleUsedByProcess return an error if ( 0 != lstrcmpi(L"System", pe32.szExeFile)) { hProcess = AdvanceOpenProcess(pe32.th32ProcessID, PROCESS_ALL_ACCESS); //If we need the exe name, we need pe32.szExeFile if(DIAGLIB_OK != isModuleUsedByProcess(pe32.th32ProcessID, library.c_str(),&bUsed)) { LOG(L"isModuleUsedByProcess failed with Process '%ls' (pid=%ld)\n",pe32.szExeFile,pe32.th32ProcessID); } else { if(bUsed) { processList->push_back(pe32.th32ProcessID); LOG(L"FOUND Process '%ls' (pid=%ld)\n",pe32.szExeFile,pe32.th32ProcessID); } } } progressIncrement(); } while (Process32Next(hProcessSnap, &pe32)); progressRelease(); if(!CloseHandle( hProcessSnap )) { LOG_LASTERROR(L"CloseHandle failed"); } if(!bUsed) LOG(L"NOT USED\n"); return iReturnCode; }
//////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////// PRIVATE FUNCTIONS //////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// int processFillList(Proc_LIST *processList, const wchar_t *processName) { int iReturnCode = DIAGLIB_OK; if(processList == NULL) { return RETURN_LOG_BAD_FUNCTION_CALL; } if(processName) { LOG_TIME(L"Ask for process list (with name = %ls) --> ",processName); } else { LOG_TIME(L"Ask for list of all process --> "); } processList->clear(); EnablePrivilege(SE_DEBUG_NAME); HANDLE hndl; if(INVALID_HANDLE_VALUE == ( hndl=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0))) { LOG_LASTERROR(L"CreateToolhelp32Snapshot failed"); return RETURN_LOG_INTERNAL_ERROR; } PROCESSENTRY32 procEntry={0}; procEntry.dwSize = sizeof( PROCESSENTRY32 ); //Get progressMax if request int progressMax = 0; if (!Process32First(hndl, &procEntry)) { LOG_LASTERROR(L"Process32First failed"); if(!CloseHandle( hndl )) { LOG_LASTERROR(L"CloseHandle failed"); } return RETURN_LOG_INTERNAL_ERROR; } do { progressMax++; } while (Process32Next(hndl, &procEntry)); if(!Process32First(hndl,&procEntry)) { LOG_LASTERROR(L"Process32First failed"); if(!CloseHandle( hndl )) { LOG_LASTERROR(L"CloseHandle failed"); } return RETURN_LOG_INTERNAL_ERROR; } progressInit(progressMax); do { if(procEntry.th32ProcessID != 0) { if(processName == NULL) { processList->push_back(procEntry.th32ProcessID); } else if(0 == _wcsicmp(procEntry.szExeFile,processName)) { processList->push_back(procEntry.th32ProcessID); } } progressIncrement(); } while(Process32Next(hndl,&procEntry)); progressRelease(); if(!CloseHandle( hndl )) { LOG_LASTERROR(L"CloseHandle failed"); } if(processList->size() == 0) { LOG(L"NO PROCESS FOUND\n"); } else { LOG(L"pid="); for (unsigned long i=0; i < processList->size() ; i++) { LOG(L"%ld,",processList->at(i)); } LOG(L"DONE\n"); } return iReturnCode; }
int processGetInfo(Proc_ID process, Proc_INFO *info) { int iReturnCode = DIAGLIB_OK; if(process == 0) { return RETURN_LOG_BAD_FUNCTION_CALL; } if(info==NULL) { return RETURN_LOG_BAD_FUNCTION_CALL; } LOG_TIME(L"Ask info on pid=%ld --> ",process); DWORD bufferSize; HANDLE hHandle = NULL; if(NULL == (hHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,0,process))) { if(ERROR_ACCESS_DENIED != GetLastError()) { LOG_LASTERROR(L"OpenProcess failed"); return RETURN_LOG_INTERNAL_ERROR; } else { LOG(L"ACCESS DENIED\n"); return DIAGLIB_ERR_PROCESS_ACCESS_DENIED; } } info->id=process; bufferSize = G_BUFFER_SIZE; if(!GetModuleBaseName(hHandle,NULL,g_buffer,bufferSize)) { LOG_LASTERROR(L"GetModuleBaseName failed"); iReturnCode = DIAGLIB_ERR_INTERNAL; } else { info->Name=g_buffer; } bufferSize = G_BUFFER_SIZE; if(!GetModuleFileNameEx(hHandle,NULL,g_buffer,bufferSize)) { LOG_LASTERROR(L"GetModuleFileNameEx failed"); iReturnCode = DIAGLIB_ERR_INTERNAL; } else { info->Path=g_buffer; } bufferSize = G_BUFFER_SIZE; if(!GetProcessImageFileName(hHandle,g_buffer,bufferSize)) { LOG_LASTERROR(L"QueryFullProcessImageName failed"); iReturnCode = DIAGLIB_ERR_INTERNAL; } else { info->FullPath=g_buffer; } HMODULE modulesFound[1024]; DWORD spaceActuallyRequired; if(!EnumProcessModules(hHandle,modulesFound,1024,&spaceActuallyRequired)) { LOG_LASTERROR(L"EnumProcessModules failed"); } else { info->modulesLoaded.clear(); for(unsigned int i=0;i<(spaceActuallyRequired/sizeof(HMODULE));i++) { TCHAR szModName[MAX_PATH]; if(GetModuleBaseName(hHandle,modulesFound[i],szModName,sizeof(szModName)/sizeof(TCHAR))) info->modulesLoaded.insert(szModName); //if(GetModuleFileNameEx(hHandle,modulesFound[i],szModName,sizeof(szModName)/sizeof(TCHAR))) //modInfo.path=szModName; } } if(!CloseHandle( hHandle )) { LOG_LASTERROR(L"CloseHandle failed"); } LOG(L"DONE\n"); return iReturnCode; }
char * mit_krb5_pkinit_cert_hash_str(const mit_krb5_data *cert) { #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H CC_SHA1_CTX ctx; char *outstr, *cpOut; unsigned char digest[CC_SHA1_DIGEST_LENGTH]; unsigned i; LOG_ENTRY(); CC_SHA1_Init(&ctx); CC_SHA1_Update(&ctx, cert->data, cert->length); CC_SHA1_Final(digest, &ctx); cpOut = outstr = (char *)malloc((2 * CC_SHA1_DIGEST_LENGTH) + 1); if(outstr == NULL) return NULL; for(i = 0; i < CC_SHA1_DIGEST_LENGTH; i++, cpOut += 2) sprintf(cpOut, "%02X", (unsigned)digest[i]); *cpOut = '\0'; return outstr; #elif defined(_WIN32) HCRYPTPROV hProv = 0; HCRYPTHASH hHash = 0; char *outstr = NULL; char *outpos; size_t cch_left; BYTE *hash = NULL; DWORD hashSize = 0; DWORD len, i; LOG_ENTRY(); if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_SIG, CRYPT_VERIFYCONTEXT)) { LOG_LASTERROR("CryptAcquireContext failed"); goto done; } if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) { LOG_LASTERROR("CryptCreateHash failed"); goto done; } if (!CryptHashData(hHash, (BYTE *) cert->data, cert->length, 0)) { LOG_LASTERROR("CryptHashData failed"); goto done; } len = sizeof(hashSize); if (!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *) &hashSize, &len, 0)) { LOG_LASTERROR("CryptGetHashParam failed while getting hash size"); goto done; } hash = malloc(hashSize); if (hash == NULL) { goto done; } len = hashSize; if (!CryptGetHashParam(hHash, HP_HASHVAL, hash, &len, 0)) { LOG_LASTERROR("CryptGetHashParam failed while getting hash"); goto done; } outstr = malloc(hashSize * 2 + 1); if (outstr == NULL) { goto done; } outpos = outstr; cch_left = hashSize * 2 + 1; for (i = 0; i < hashSize; i++) { StringCchPrintfExA(outpos, cch_left, &outpos, &cch_left, STRSAFE_FILL_ON_FAILURE, "%02X", (unsigned) hash[i]); } *outpos = '\0'; done: if (hHash != 0) CryptDestroyHash(hHash); if (hProv != 0) CryptReleaseContext(hProv, 0); if (hash != NULL) free(hash); return outstr; #endif }