use procmon from Sys Internals to log the process
filter the log for file access
(latest release has a file summary tab that simplifies entering filter expressions to mere mouse clicks)
code for a simple xorrer (takes an input file and writes back a xorred file)
#include <stdio.h>
#include <stdlib.h>
#define ENOENT 2
void main(int argc, char *argv[]) {
if(argc !=3 ) { printf("usage : %s infile outfile",argv[0]); return;}
FILE *fp = 0; errno_t err=ENOENT; long flen =0,bread =0 ; char *buff =0;
if (((err = fopen_s(&fp,argv[1],"rb")) == 0) && (fp !=0)) {
fseek(fp,0,SEEK_END);
flen = ftell(fp);
if((buff = (char *)calloc(flen,sizeof(char))) !=0 ) {
fseek(fp,0,SEEK_SET);
if (( bread = fread(buff,sizeof(char),flen,fp) ) == flen) {
fclose(fp); err=ENOENT;
for(int i = 0; i< flen; i++) {
buff[i] ^= 'A' ;
}
if(((err = fopen_s(&fp,argv[2],"wb")) == 0) && (fp !=0)) {
fwrite(buff,1,flen,fp);
fclose(fp);
free(buff);
}
}
}
}
}
a batfile logging this file access (blind run) and loading the log into procmon again for applying filters and saving back the filtered events as xml
which allows powershell to parse and print
echo off
start procmon.exe /quiet /minimized /backingfile /nofilter .\LogFile.pml
procmon.exe /waitforidle
start /wait encfile.exe rawdata.txt encdata.txt
procmon.exe /terminate
start /wait procmon.exe /openlog .\logfile.pml
powershell ([xml] ( Get-Content .\logfile.xml)).procmon.eventlist.event[2].stack.frame
pause
filter used to save xml file was "Include if path contains xxxx where xxxx is the filename of interest"
here is the stack of the fileRead
PS > ([xml]
( Get-Content .\logfile.xml)).procmon.eventlist.event[2].stack.frame
depth address path location
----- ------- ---- --------
0 0xb9ed5888 C:\WINDOWS\Syste... FltpPerformPreCa...
1 0xb9ed72a0 C:\WINDOWS\Syste... FltpPassThroughI...
2 0xb9ed7c48 C:\WINDOWS\Syste... FltpPassThrough ...
3 0xb9ed8059 C:\WINDOWS\Syste... FltpDispatch + 0...
4 0x804ee129 C:\WINDOWS\syste... IopfCallDriver +...
5 0x80571d9c C:\WINDOWS\syste... NtReadFile + 0x580
6 0x8053d658 C:\WINDOWS\syste... KiFastCallEntry ...
7 0x40364c C:\Documents and... encfile.exe + 0x...
8 0x403ac0 C:\Documents and... encfile.exe + 0x...
9 0x4033a2 C:\Documents and... encfile.exe + 0x...
10 0x4015bf C:\Documents and... encfile.exe + 0x...
11 0x401698 C:\Documents and... encfile.exe + 0x...
12 0x4016d1 C:\Documents and... encfile.exe + 0x...
13 0x4010d3 C:\Documents and... encfile.exe + 0x...
14 0x401d09 C:\Documents and... encfile.exe + 0x...
15 0x7c817077 C:\WINDOWS\syste... BaseProcessStart...
16 0x0
ascertaining the ReadFile call
:\cdb -c "ub 40364c;q" encfile.exe | tail -n 2
00403646 ff1550b04000 call dword ptr [image00400000+0xb050 (0040b050)]
quit:
:\cdb -c ".printf \"%y\n\",poi(40b050);q" encfile.exe | tail -n 2
kernel32!ReadFile (7c801812)
quit:
break fopen64 if strcmp($rdi, "yourfilename") == 1
, if the binary still has function names available (check out withinfo functions
). not sure if mingw-gdb (windows) works in similar ways too – phil294 Aug 15 '18 at 17:09