param([Parameter(Mandatory=$true)][int]$TargetProcessId)
$ErrorActionPreference = 'Stop'
Add-Type -TypeDefinition @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
public static class MarketFileHandles {
[DllImport("ntdll.dll")] static extern int NtQuerySystemInformation(int c,IntPtr b,int n,out int needed);
[DllImport("kernel32.dll",SetLastError=true)] static extern IntPtr OpenProcess(uint access,bool inherit,int pid);
[DllImport("kernel32.dll")] static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll",SetLastError=true)] static extern bool DuplicateHandle(IntPtr source,IntPtr handle,IntPtr target,out IntPtr copy,uint access,bool inherit,uint options);
[DllImport("kernel32.dll")] static extern uint GetFileType(IntPtr h);
[DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern uint GetFinalPathNameByHandle(IntPtr h,StringBuilder s,uint n,uint flags);
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
[StructLayout(LayoutKind.Sequential)] struct Entry {
public IntPtr Object,Pid,Handle;
public uint Access;
public ushort Backtrace,Type;
public uint Attributes,Reserved;
}
public static string[] Read(int pid) {
int size=1024*1024,needed,status; IntPtr buffer;
while(true) {
buffer=Marshal.AllocHGlobal(size);
status=NtQuerySystemInformation(64,buffer,size,out needed);
if(status==0) break;
Marshal.FreeHGlobal(buffer);
if(status!=unchecked((int)0xc0000004)) throw new Exception("NtQuerySystemInformation: "+status);
size=Math.Max(size*2,needed+65536);
}
var paths=new List<string>();
IntPtr process=OpenProcess(0x40,false,pid);
if(process==IntPtr.Zero) throw new Exception("OpenProcess: "+Marshal.GetLastWin32Error());
try {
long count=Marshal.ReadIntPtr(buffer).ToInt64(); int stride=Marshal.SizeOf<Entry>();
for(long i=0;i<count;i++) {
Entry entry=Marshal.PtrToStructure<Entry>(IntPtr.Add(buffer,checked(16+(int)(i*stride))));
if(entry.Pid.ToInt64()!=pid) continue;
IntPtr copy;
if(!DuplicateHandle(process,entry.Handle,GetCurrentProcess(),out copy,0,false,2)) continue;
try {
if(GetFileType(copy)!=1) continue;
var path=new StringBuilder(4096);
if(GetFinalPathNameByHandle(copy,path,4096,0)>0) {
var value=path.ToString();
if(value.IndexOf("markets",StringComparison.OrdinalIgnoreCase)>=0||value.IndexOf("game-events.db",StringComparison.OrdinalIgnoreCase)>=0) paths.Add(value);
}
} finally {CloseHandle(copy);}
}
} finally {CloseHandle(process);Marshal.FreeHGlobal(buffer);}
return paths.ToArray();
}
}
'@
[MarketFileHandles]::Read($TargetProcessId) | Sort-Object -Unique
param([Parameter(Mandatory=$true)][int]$TargetProcessId)
$ErrorActionPreference = 'Stop'
Add-Type -TypeDefinition @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
public static class MarketFileHandles {
[DllImport("ntdll.dll")] static extern int NtQuerySystemInformation(int c,IntPtr b,int n,out int needed);
[DllImport("kernel32.dll",SetLastError=true)] static extern IntPtr OpenProcess(uint access,bool inherit,int pid);
[DllImport("kernel32.dll")] static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll",SetLastError=true)] static extern bool DuplicateHandle(IntPtr source,IntPtr handle,IntPtr target,out IntPtr copy,uint access,bool inherit,uint options);
[DllImport("kernel32.dll")] static extern uint GetFileType(IntPtr h);
[DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern uint GetFinalPathNameByHandle(IntPtr h,StringBuilder s,uint n,uint flags);
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
[StructLayout(LayoutKind.Sequential)] struct Entry {
public IntPtr Object,Pid,Handle;
public uint Access;
public ushort Backtrace,Type;
public uint Attributes,Reserved;
}
public static string[] Read(int pid) {
int size=1024*1024,needed,status; IntPtr buffer;
while(true) {
buffer=Marshal.AllocHGlobal(size);
status=NtQuerySystemInformation(64,buffer,size,out needed);
if(status==0) break;
Marshal.FreeHGlobal(buffer);
if(status!=unchecked((int)0xc0000004)) throw new Exception("NtQuerySystemInformation: "+status);
size=Math.Max(size*2,needed+65536);
}
var paths=new List<string>();
IntPtr process=OpenProcess(0x40,false,pid);
if(process==IntPtr.Zero) throw new Exception("OpenProcess: "+Marshal.GetLastWin32Error());
try {
long count=Marshal.ReadIntPtr(buffer).ToInt64(); int stride=Marshal.SizeOf<Entry>();
for(long i=0;i<count;i++) {
Entry entry=Marshal.PtrToStructure<Entry>(IntPtr.Add(buffer,checked(16+(int)(i*stride))));
if(entry.Pid.ToInt64()!=pid) continue;
IntPtr copy;
if(!DuplicateHandle(process,entry.Handle,GetCurrentProcess(),out copy,0,false,2)) continue;
try {
if(GetFileType(copy)!=1) continue;
var path=new StringBuilder(4096);
if(GetFinalPathNameByHandle(copy,path,4096,0)>0) {
var value=path.ToString();
if(value.IndexOf("markets",StringComparison.OrdinalIgnoreCase)>=0||value.IndexOf("game-events.db",StringComparison.OrdinalIgnoreCase)>=0) paths.Add(value);
}
} finally {CloseHandle(copy);}
}
} finally {CloseHandle(process);Marshal.FreeHGlobal(buffer);}
return paths.ToArray();
}
}
'@
[MarketFileHandles]::Read($TargetProcessId) | Sort-Object -Unique