Get injection target Injection context. Process info in case of new process Error code
| 32 | /// <param name="pi">Process info in case of new process</param> |
| 33 | /// <returns>Error code</returns> |
| 34 | NTSTATUS InjectionCore::GetTargetProcess( InjectContext& context, PROCESS_INFORMATION& pi ) |
| 35 | { |
| 36 | NTSTATUS status = ERROR_SUCCESS; |
| 37 | |
| 38 | // No process required for driver mapping |
| 39 | if (context.cfg.injectMode == Kernel_DriverMap) |
| 40 | return status; |
| 41 | |
| 42 | // Await new process |
| 43 | if (context.cfg.processMode == ManualLaunch) |
| 44 | { |
| 45 | xlog::Normal( "Waiting on process %ls", context.procPath.c_str() ); |
| 46 | |
| 47 | auto procName = blackbone::Utils::StripPath( context.procPath ); |
| 48 | if (procName.empty()) |
| 49 | { |
| 50 | Message::ShowWarning( _hMainDlg, L"Please select executable to wait for\n" ); |
| 51 | return STATUS_NOT_FOUND; |
| 52 | } |
| 53 | |
| 54 | // Filter already existing processes |
| 55 | std::vector<blackbone::ProcessInfo> newList; |
| 56 | |
| 57 | if (context.procList.empty()) |
| 58 | context.procList = blackbone::Process::EnumByNameOrPID( 0, procName ).result( std::vector<blackbone::ProcessInfo>() ); |
| 59 | |
| 60 | // Wait for process |
| 61 | for (context.waitActive = true;; Sleep( 10 )) |
| 62 | { |
| 63 | // Canceled by user |
| 64 | if (!context.waitActive) |
| 65 | { |
| 66 | xlog::Warning( "Process wait canceled by user" ); |
| 67 | return STATUS_REQUEST_ABORTED; |
| 68 | } |
| 69 | |
| 70 | if (!context.procDiff.empty()) |
| 71 | { |
| 72 | context.procList = newList; |
| 73 | |
| 74 | // Skip first N found processes |
| 75 | if (context.skippedCount < context.cfg.skipProc) |
| 76 | { |
| 77 | context.skippedCount++; |
| 78 | context.procDiff.erase( context.procDiff.begin() ); |
| 79 | |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | context.pid = context.procDiff.front().pid; |
| 84 | context.procDiff.erase( context.procDiff.begin() ); |
| 85 | |
| 86 | xlog::Verbose( "Got process %d", context.pid ); |
| 87 | break; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // Detect new process |