龙马谷

 找回密码
 立即注册

QQ登录

只需一步,快速开始

龙马谷VIP会员办理客服QQ:82926983(如果临时会话没有收到回复,请先加QQ好友再发。)
1 [已完结] GG修改器新手入门与实战教程 31课 2 [已完结] GG修改器美化修改教程 6课 3 [已完结] GG修改器Lua脚本新手入门教程 12课
4 [已完结] 触动精灵脚本新手入门必学教程 22课 5 [已完结] 手游自动化脚本入门实战教程 9课 6 [已完结] C++射击游戏方框骨骼透视与自瞄教程 27课
7 [已完结] C++零基础UE4逆向开发FPS透视自瞄教程 29课 8 [已完结] C++零基础大漠模拟器手游自动化辅助教程 22课 9 [已完结] C++零基础开发DXF内存脚本辅助教程 32课
以下是天马阁VIP教程,本站与天马阁合作,赞助VIP可以获得天马阁对应VIP会员,名额有限! 点击进入天马阁论坛
1 [已完结] x64CE与x64dbg入门基础教程 7课 2 [已完结] x64汇编语言基础教程 16课 3 [已完结] x64辅助入门基础教程 9课
4 [已完结] C++x64内存辅助实战技术教程 149课 5 [已完结] C++x64内存检测与过检测技术教程 10课 6 [已完结] C+x64二叉树分析遍历与LUA自动登陆教程 19课
7 [已完结] C++BT功能原理与x64实战教程 29课 8 [已完结] C+FPS框透视与自瞄x64实现原理及防护思路
查看: 5001|回复: 0

进程、注册表路径 内核函数封装源码

[复制链接]

8

主题

1

回帖

15

积分

编程入门

Rank: 1

龙马币
56



  1. //依据EPROCESS得到进程全路径
  2. extern VOID GetFullPathByEprocess( ULONG eprocess,PCHAR ProcessImageName );

  3. //得到当前调用函数的进程信息
  4. extern VOID GetCurrentProcess(PULONG pid, PCHAR name, PCHAR path);

  5. //路径解析出子进程名
  6. extern VOID GetSonName( PCHAR ProcessPath, PCHAR ProcessName );

  7. //根据SectionHandle得到进程全路径
  8. extern VOID GetFullPathBySectionHandle( HANDLE SectionHandle, PCHAR ProcessImageName);

  9. //根据ProcessHandle得到进程全路径
  10. extern VOID GetFullPathByProcessHandle( HANDLE ProcessHandle, PCHAR ProcessImageName , PULONG pid );

  11. //FileObject得到进程全路径
  12. extern VOID GetFullPathByFileObject( PFILE_OBJECT FileObject, PCHAR ProcessImageName);

  13. //KeyHandle得到注册表全路径
  14. extern BOOLEAN GetRegKeyNameByHandle(HANDLE handle, char *realpath);

  15. //
  16. extern VOID UnicodeTochar(PUNICODE_STRING dst , char *src);
  17. //
  18. extern VOID WcharToChar(PWCHAR src,PCHAR dst);

  19. 代码:
  20. extern POBJECT_TYPE *PsProcessType;

  21. NTKERNELAPI
  22. UCHAR *
  23. PsGetProcessImageFileName(
  24. PEPROCESS Process);

  25. NTKERNELAPI
  26. NTSTATUS
  27. ObQueryNameString(
  28. INPVOID Object,
  29. OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
  30. INULONG Length,
  31. OUT PULONG ReturnLength);

  32. //路径解析出子进程名
  33. VOIDGetSonName( char *ProcessPath, char *ProcessName )
  34. {
  35. ULONG n = strlen( ProcessPath) - 1;
  36. ULONG i = n;
  37. //KdPrint(("%d",n));
  38. while( ProcessPath[i] != '\\')
  39. {
  40. i = i-1;
  41. }
  42. strncpy( ProcessName,ProcessPath+i+1,n-i);
  43. }

  44. //依据EPROCESS得到进程全路径
  45. VOID GetFullPathByEprocess( ULONG eprocess,PCHAR ProcessImageName )
  46. {
  47. //原理Eprocess->sectionobject(0x138)->Segment(0x014)->ControlAera(0x000)->FilePointer(0x024)->(FileObject->FileName,FileObject->DeviceObject)
  48. ULONG object;
  49. PFILE_OBJECT FileObject;
  50. UNICODE_STRING FilePath;
  51. UNICODE_STRING DosName;
  52. STRING AnsiString;

  53. FileObject = NULL;
  54. FilePath.Buffer = NULL;
  55. FilePath.Length = 0;
  56. *ProcessImageName = 0;

  57. if(MmIsAddressValid((PULONG)(eprocess+0x138)))//Eprocess->sectionobject(0x138)
  58. {
  59. object=(*(PULONG)(eprocess+0x138));
  60. //KdPrint(("[GetProcessFileName] sectionobject :0x%x\n",object));
  61. if(MmIsAddressValid((PULONG)((ULONG)object+0x014)))
  62. {
  63. object=*(PULONG)((ULONG)object+0x014);
  64. //KdPrint(("[GetProcessFileName] Segment :0x%x\n",object));
  65. if(MmIsAddressValid((PULONG)((ULONG)object+0x0)))
  66. {
  67. object=*(PULONG)((ULONG_PTR)object+0x0);
  68. //KdPrint(("[GetProcessFileName] ControlAera :0x%x\n",object));
  69. if(MmIsAddressValid((PULONG)((ULONG)object+0x024)))
  70. {
  71. object=*(PULONG)((ULONG)object+0x024);
  72. //KdPrint(("[GetProcessFileName] FilePointer :0x%x\n",object));
  73. }
  74. else
  75. return ;
  76. }
  77. else
  78. return ;
  79. }
  80. else
  81. return ;
  82. }
  83. else
  84. return ;
  85. FileObject=(PFILE_OBJECT)object;

  86. FilePath.Buffer = ExAllocatePool(PagedPool,0x200);
  87. FilePath.MaximumLength = 0x200;
  88. //KdPrint(("[GetProcessFileName] FilePointer :%wZ\n",&FilePointer->FileName));
  89. ObReferenceObjectByPointer((PVOID)FileObject,0,NULL,KernelMode);//引用计数+1,操作对象

  90. RtlVolumeDeviceToDosName(FileObject-> DeviceObject, &DosName);
  91. RtlCopyUnicodeString(&FilePath, &DosName);
  92. RtlAppendUnicodeStringToString(&FilePath, &FileObject->FileName);
  93. ObDereferenceObject(FileObject);

  94. RtlUnicodeStringToAnsiString(&AnsiString, &FilePath, TRUE);
  95. if ( AnsiString.Length >= 216 )
  96. {
  97. memcpy(ProcessImageName, AnsiString.Buffer, 0x100u);
  98. *(ProcessImageName + 215) = 0;
  99. }
  100. else
  101. {
  102. memcpy(ProcessImageName, AnsiString.Buffer, AnsiString.Length);
  103. ProcessImageName[AnsiString.Length] = 0;
  104. }
  105. RtlFreeAnsiString(&AnsiString);
  106. ExFreePool(DosName.Buffer);
  107. ExFreePool(FilePath.Buffer);
  108. }


  109. //
  110. VOID GetCurrentProcess(PULONG pid, PCHAR name, PCHAR path)
  111. {
  112. PEPROCESS Cprocess;
  113. Cprocess = PsGetCurrentProcess();
  114. *pid = *(PULONG)((ULONG)Cprocess+0x84);
  115. strcpy(name ,PsGetProcessImageFileName(Cprocess));
  116. GetFullPathByEprocess((ULONG)Cprocess,path);
  117. }


  118. //根据SectionHandle得到进程全路径
  119. VOID GetFullPathBySectionHandle( HANDLE SectionHandle, PCHAR ProcessImageName )
  120. {
  121. PVOID SectionObject;
  122. PFILE_OBJECT FileObject;
  123. UNICODE_STRING FilePath;
  124. UNICODE_STRING DosName;
  125. NTSTATUS Status;
  126. STRING AnsiString;

  127. SectionObject = NULL;
  128. FileObject = NULL;
  129. FilePath.Buffer = NULL;
  130. FilePath.Length = 0;
  131. *ProcessImageName = 0;
  132. Status = ObReferenceObjectByHandle(SectionHandle, 0, NULL, KernelMode, &SectionObject, NULL);

  133. if ( NT_SUCCESS(Status) )
  134. {
  135. FilePath.Buffer = ExAllocatePool(PagedPool,0x200);
  136. FilePath.MaximumLength = 0x200;
  137. FileObject = (PFILE_OBJECT)(*((ULONG *)SectionObject + 5)); // PSEGMENT
  138. FileObject = *(PFILE_OBJECT *)FileObject; // CONTROL_AREA
  139. FileObject = *(PFILE_OBJECT *)((ULONG)FileObject + 36); // FILE_OBJECT
  140. ObReferenceObjectByPointer((PVOID)FileObject, 0, NULL, KernelMode);
  141. RtlVolumeDeviceToDosName(FileObject-> DeviceObject, &DosName);
  142. RtlCopyUnicodeString(&FilePath, &DosName);
  143. RtlAppendUnicodeStringToString(&FilePath, &FileObject->FileName);
  144. ObDereferenceObject(FileObject);
  145. ObDereferenceObject(SectionObject);
  146. RtlUnicodeStringToAnsiString(&AnsiString, &FilePath, TRUE);
  147. if ( AnsiString.Length >= 216 )
  148. {
  149. memcpy(ProcessImageName, AnsiString.Buffer, 0x100u);
  150. *(ProcessImageName + 215) = 0;
  151. }
  152. else
  153. {
  154. memcpy(ProcessImageName, AnsiString.Buffer, AnsiString.Length);
  155. ProcessImageName[AnsiString.Length] = 0;
  156. }
  157. RtlFreeAnsiString(&AnsiString);
  158. ExFreePool(DosName.Buffer);
  159. ExFreePool(FilePath.Buffer);
  160. }
  161. }


  162. //根据ProcessHandle得到EPROCESS然后得到进程全路径
  163. VOID GetFullPathByProcessHandle( HANDLE ProcessHandle, PCHAR ProcessImageName , PULONG pid )
  164. {
  165. NTSTATUS status;
  166. PVOID ProcessObject;
  167. ULONG eprocess;

  168. status = ObReferenceObjectByHandle( ProcessHandle ,0,*PsProcessType,KernelMode, &ProcessObject, NULL);
  169. if(!NT_SUCCESS(status)) //失败
  170. {
  171. DbgPrint("Object Error");
  172. KdPrint(("[GetFullPathByProcessHandle] error status:0x%x\n",status));
  173. return;
  174. }
  175. //KdPrint(("[GetTerminateProcessPath] Eprocess :0x%x\n",(ULONG)ProcessObject));
  176. //Object转换成EPROCESS: object低二位清零
  177. eprocess = ((ULONG)ProcessObject) & 0xFFFFFFFC;
  178. *pid = *(PULONG)((ULONG)eprocess+0x84);
  179. ObDereferenceObject(ProcessObject);
  180. GetFullPathByEprocess( eprocess ,ProcessImageName);
  181. }


  182. //根据FileObject得到全路径
  183. VOID GetFullPathByFileObject( PFILE_OBJECT FileObject, PCHAR ProcessImageName)
  184. {

  185. UNICODE_STRING FilePath;
  186. UNICODE_STRING DosName;
  187. STRING AnsiString;

  188. FilePath.Buffer = NULL;
  189. FilePath.Length = 0;
  190. *ProcessImageName = 0;

  191. FilePath.Buffer = ExAllocatePool(PagedPool,0x200);
  192. FilePath.MaximumLength = 0x200;
  193. //KdPrint(("[GetProcessFileName] FilePointer :%wZ\n",&FilePointer->FileName));
  194. ObReferenceObjectByPointer((PVOID)FileObject,0,NULL,KernelMode);//引用计数+1,操作对象

  195. RtlVolumeDeviceToDosName(FileObject-> DeviceObject, &DosName);
  196. RtlCopyUnicodeString(&FilePath, &DosName);
  197. RtlAppendUnicodeStringToString(&FilePath, &FileObject->FileName);
  198. ObDereferenceObject(FileObject);

  199. RtlUnicodeStringToAnsiString(&AnsiString, &FilePath, TRUE);
  200. if ( AnsiString.Length >= 216 )
  201. {
  202. memcpy(ProcessImageName, AnsiString.Buffer, 0x100u);
  203. *(ProcessImageName + 215) = 0;
  204. }
  205. else
  206. {
  207. memcpy(ProcessImageName, AnsiString.Buffer, AnsiString.Length);
  208. ProcessImageName[AnsiString.Length] = 0;
  209. }
  210. RtlFreeAnsiString(&AnsiString);
  211. ExFreePool(DosName.Buffer);
  212. ExFreePool(FilePath.Buffer);
  213. }


  214. //解析注册表路径
  215. BOOLEAN StandardPrintHkey(char * path,char *realpath)
  216. {

  217. int judgeTop;
  218. int judgeSecond;
  219. int judgeThird;
  220. inti;
  221. int j;
  222. int t;
  223. int k;
  224. int lencur;
  225. char realname[255]={0};
  226. j=0;
  227. k=0;
  228. t=0;
  229. judgeTop=strncmp("\\REGISTRY\\USER",path,14);

  230. if(judgeTop==0)
  231. {

  232. lencur=strlen(path);
  233. for(i=0;i<lencur;i++)
  234. {
  235. if(path[i]=='-')
  236. {
  237. if(path[i+1]=='5')
  238. {
  239. if(path[i+2]=='0')
  240. {
  241. if(path[i+3]=='0')
  242. {if(path[i+4]=='_')
  243. {
  244. k=i+12;
  245. t=1;
  246. }
  247. else
  248. {
  249. j=i+4;
  250. t=1;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }

  257. DbgPrint("[j]%d\n",j);
  258. DbgPrint("[k]%d\n",k);
  259. if((k==0)&&(t==1))
  260. {
  261. strcpy(realname,"HKEY_CURRENT_USER");
  262. strncat(realname,&path[j],sizeof(path)-j);
  263. DbgPrint("[HKEY_CURRENT_USER]%s",path);
  264. }
  265. if((j==0)&&(t==1))
  266. {
  267. strcpy(realname,"HKEY_CLASSES_ROOT");
  268. strncat(realname,&path[k],sizeof(path)-k);
  269. DbgPrint("[HKEY_CLASSES_ROOT]%s",path);
  270. }
  271. if(t==0)
  272. {
  273. strcpy(realname,"HKEY_USERS");
  274. strncat(realname,&path[14],sizeof(path)-14);
  275. DbgPrint("[HKEY_USER]%s",path);
  276. }
  277. }
  278. else
  279. {
  280. judgeThird=strncmp("\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\Hardware Profiles\\0001",path,61);
  281. if(judgeThird==0)
  282. {
  283. strcpy(realname,"HKEY_CURRENT_CONFIG");
  284. strncat(realname,&path[61],sizeof(path)-61);
  285. DbgPrint("[HKEY_CURRENT_CONFIG]%s",path);
  286. }
  287. else
  288. {


  289. strcpy(realname,"HKEY_LOCAL_MACHINE");
  290. strncat(realname,&path[17],sizeof(path)-17);
  291. DbgPrint("[HKEY_LOCAL_MACHINE]%s",path);


  292. }
  293. }
  294. strcpy(realpath,realname);
  295. return TRUE;
  296. }


  297. //注册表根据KeyHandle得到键
  298. BOOLEAN GetRegKeyNameByHandle(HANDLE handle, char *realpath)
  299. {

  300. ULONG uactLength;
  301. POBJECT_NAME_INFORMATIONpustr;
  302. ANSI_STRING astr;
  303. PVOID pObj;
  304. NTSTATUS ns;
  305. char pch[256]={0};
  306. ns = ObReferenceObjectByHandle( handle, 0, NULL, KernelMode, &pObj, NULL );
  307. if (!NT_SUCCESS(ns))
  308. {
  309. KdPrint(("111!\n"));
  310. KdPrint(("0x%x\n",ns));
  311. return FALSE;
  312. }
  313. pustr = ExAllocatePool(NonPagedPool,1024+4);

  314. if (pObj==NULL||pch==NULL)
  315. return FALSE;

  316. ns = ObQueryNameString(pObj,pustr,512,&uactLength);

  317. if (NT_SUCCESS(ns))
  318. {
  319. RtlUnicodeStringToAnsiString(&astr,(PUNICODE_STRING)pustr,TRUE);
  320. strncpy(pch,astr.Buffer,256);
  321. }
  322. ExFreePool(pustr);
  323. RtlFreeAnsiString( &astr );
  324. if (pObj)
  325. {
  326. ObDereferenceObject(pObj);
  327. }
  328. StandardPrintHkey(pch,realpath);
  329. return TRUE;
  330. }


  331. //UnicodeTochar
  332. VOID UnicodeTochar(PUNICODE_STRING dst , char *src)
  333. {
  334. ANSI_STRING string;
  335. RtlUnicodeStringToAnsiString(&string,dst, TRUE);
  336. strcpy(src,string.Buffer);
  337. RtlFreeAnsiString(&string);
  338. }


  339. //wcharTochar
  340. VOID WcharToChar(PWCHAR src,PCHAR dst)
  341. {
  342. UNICODE_STRING uString;
  343. ANSI_STRING aString;
  344. RtlInitUnicodeString(&uString,src);
  345. RtlUnicodeStringToAnsiString(&aString,&uString,TRUE);
  346. strcpy(dst,aString.Buffer);
  347. RtlFreeAnsiString(&aString);
  348. }


复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

龙马谷| C/C++辅助教程| 安卓逆向安全| 论坛导航| 免责申明|Archiver|
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表龙马谷立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2018-2021 All Right Reserved.
在线客服
快速回复 返回顶部 返回列表