@@ -32,7 +32,7 @@ namespace NtObjectManager.Cmdlets.Object
3232 /// <para>Duplicate an object to another process. If the desintation process is the current process an object is returned, otherwise a handle is returned.</para>
3333 /// </example>
3434 /// <example>
35- /// <code>Copy-NtObject -Handle 1234 -SourceProcess $proc</code>
35+ /// <code>Copy-NtObject -SourceHandle 1234 -SourceProcess $proc</code>
3636 /// <para>Duplicate an object from another process to the current process.</para>
3737 /// </example>
3838 [ Cmdlet ( VerbsCommon . Copy , "NtObject" ) ]
@@ -45,6 +45,12 @@ public sealed class CopyNtObjectCmdlet : PSCmdlet
4545 [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = "FromObject" , ValueFromPipeline = true ) ]
4646 public NtObject [ ] Object { get ; set ; }
4747
48+ /// <summary>
49+ /// <para type="description">Specify the object to duplicate in the current process.</para>
50+ /// </summary>
51+ [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = "FromNtHandle" , ValueFromPipeline = true ) ]
52+ public NtHandle [ ] Handle { get ; set ; }
53+
4854 /// <summary>
4955 /// <para type="description">Specify the object to duplicate as a handle.</para>
5056 /// </summary>
@@ -156,6 +162,27 @@ private object GetHandle(IntPtr handle)
156162 GetDesiredAccess ( ) , ObjectAttributes ?? 0 , GetOptions ( ) ) ;
157163 }
158164
165+ private object GetObject ( NtHandle handle )
166+ {
167+ using ( var proc = NtProcess . Open ( handle . ProcessId , ProcessAccessRights . DupHandle ) )
168+ {
169+ using ( var dup_obj = NtGeneric . DuplicateFrom ( proc , new IntPtr ( handle . Handle ) ,
170+ GetDesiredAccess ( ) , ObjectAttributes ?? 0 , GetOptions ( ) ) )
171+ {
172+ return dup_obj . ToTypedObject ( ) ;
173+ }
174+ }
175+ }
176+
177+ private object GetHandle ( NtHandle handle )
178+ {
179+ using ( var proc = NtProcess . Open ( handle . ProcessId , ProcessAccessRights . DupHandle ) )
180+ {
181+ return NtObject . DuplicateHandle ( proc , new IntPtr ( handle . Handle ) , DestinationProcess ,
182+ GetDesiredAccess ( ) , ObjectAttributes ?? 0 , GetOptions ( ) ) ;
183+ }
184+ }
185+
159186 private object GetObject ( NtObject obj )
160187 {
161188 return obj . DuplicateObject ( GetDesiredAccess ( ) , ObjectAttributes ?? 0 , GetOptions ( ) ) ;
@@ -188,6 +215,32 @@ protected override void ProcessRecord()
188215 WriteObject ( func ( obj ) ) ;
189216 }
190217 }
218+ else if ( ParameterSetName == "FromNtHandle" )
219+ {
220+ Func < NtHandle , object > func ;
221+ if ( DestinationProcess . ProcessId == NtProcess . Current . ProcessId )
222+ {
223+ func = GetObject ;
224+ }
225+ else
226+ {
227+ func = GetHandle ;
228+ }
229+
230+ foreach ( var obj in Handle )
231+ {
232+ try
233+ {
234+ WriteObject ( func ( obj ) ) ;
235+ }
236+ catch ( NtException ex )
237+ {
238+ if ( Handle . Length == 1 )
239+ throw ;
240+ WriteError ( new ErrorRecord ( ex , "Error" , ErrorCategory . OpenError , obj ) ) ;
241+ }
242+ }
243+ }
191244 else
192245 {
193246 Func < IntPtr , object > func ;
0 commit comments