Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
12 KiB

  1. #ifndef _NESSY_INC
  2. #define _NESSY_INC
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. // A nesy database consists of an index file named nesy.x and a data file called nesy.bin.
  7. // The index file is composed of 3 DWORD records that tell the id of the blob, where the
  8. // blob starts in the nesy.bin file and how large the blob is in bytes. The index file is
  9. // simply read into memory and searched sequentially for the requested record. Normal
  10. // blobs in a nesy database begin with id 1000. The numbers below 1000 are reserved for
  11. // special uses. For application compatibility the blob who's id is 0 is special. This blob
  12. // is used to store the structure that is read by the loader to hook and executible. The
  13. // format of Blob0 at a high level is like this:
  14. //
  15. // Blob0
  16. // {
  17. // A list of modules (mainly system DLLs) that should be excluded from shimming
  18. // A list of shim DLL's that shim API's
  19. // for each DLL {
  20. // A list of modules or specific calls to exclude from shimming (added to the global list)
  21. // A list of modules or specific calls to include for shimming (contrary to the global list)
  22. // }
  23. // A list of in memory patch modules
  24. // A list of exes that have patches
  25. // for each exe {
  26. // A list of matching files
  27. // An array of patches to apply
  28. // A list of DLLs to apply
  29. // for each DLL {
  30. // A list of modules or specific calls to exclude from shimming (added to the DLL list and global list)
  31. // A list of modules or specific calls to include for shimming (contrary to the global and/or DLL list)
  32. // }
  33. // }
  34. // }
  35. //
  36. // This is another way of looking at blob0
  37. //
  38. // At a high level Blob0 is structured like this:
  39. //
  40. // [BLOB0]
  41. // | |
  42. // | [GLOBAL EXCLUDE 1] -> [GLOBAL EXCLUDE 2] -> NULL
  43. // |
  44. // [SHIM DLL1] -> [SHIM DLL2] -> NULL
  45. // | | |
  46. // | | [same as SHIM DLL 1]
  47. // | |
  48. // | [INCLUDE 1] -> [INCLUDE 2] -> NULL
  49. // |
  50. // [PATCH NAME1] -> [PATCH NAME2] -> [PATCH NAME3] -> NULL
  51. // |
  52. // [EXE 1] -> [EXE 2] -> NULL
  53. // | | | |
  54. // | | | [same as EXE 1]
  55. // | | |
  56. // | | [DLL REF 1] -> [DLL REF 2] -> NULL
  57. // | | | |
  58. // | | | [same as DLL REF 1]
  59. // | | |
  60. // | | [INCLUDE 1] -> [INCLUDE 2] -> NULL
  61. // | |
  62. // | [ARRAY OF PATCH REFS (blob IDs)]
  63. // |
  64. // [MATCHING INFO 1] -> [MATCHING INFO 2] -> NULL
  65. //
  66. // PEXELIST is an exe name list
  67. // PMATCHINGINFO is a matching list there is one matching list for an exe
  68. // PDLLNAMELIST is a shim dll list
  69. // PPATCHNAMELIST is an in memory patch name list
  70. #define NESY_VERSION 11
  71. #define NESY_MAGIC 0x7973656E // 'nesy' (reversed because of little-endian ordering)
  72. #define BLOB_APPNAME_CATALOG 0 // Special Blob list of apps that have blobs in the data base
  73. #define BLOB_SPECIAL_LOWEST 0 // Special reserved blob id First one
  74. #define BLOB_SPECIAL_LAST 1000 // Special reserved blob id last one
  75. //
  76. // blobs 1001 and up are for user patches and other binary data.
  77. //
  78. #pragma pack(1)
  79. typedef enum _INCTYPE {
  80. INCLUDE = 0,
  81. EXCLUDE
  82. } INCTYPE;
  83. typedef struct _INDEXRECORD
  84. {
  85. DWORD dwID; // id for this index record.
  86. DWORD dwDataFileBlobOffset; // offset in data file where the data is stored.
  87. DWORD dwDataFileBlobLength; // length of the data in the data file.
  88. } INDEXRECORD, *PINDEXRECORD;
  89. typedef struct _INDEXFILEHEADER
  90. {
  91. DWORD dwVersion; // version of index file
  92. DWORD dwMagic;
  93. DWORD dwlastIndexUsed; // next index record
  94. DWORD dwTotalRecords; // total records in index file
  95. DWORD dwLastID; // last id used
  96. } INDEXFILEHEADER, *PINDEXFILEHEADER;
  97. typedef struct _INDEXFILE
  98. {
  99. INDEXFILEHEADER hdr; // index file header
  100. #pragma warning( disable : 4200 )
  101. INDEXRECORD indexRecords[]; // one or more index records
  102. #pragma warning( default : 4200 )
  103. } INDEXFILE, *PINDEXFILE;
  104. // The following definations are for blob 0 which lists the DLL's and Applications and Exes
  105. // that are hooked and patches by the shim.
  106. // this is a definition for an inclusion list, which is also used for exclusion lists as well
  107. //
  108. // NOTE: contains two variable-length strings; the second can be accessed as follows:
  109. //
  110. // TCHAR *szLocalAPI = NULL;
  111. // /* we assume INCLUDELIST *pIncludes;, and that it is valid */
  112. //
  113. // if (pIncludes->dwOffsetToAPI) {
  114. // szLocalAPI = (PBYTE)pIncludes + dwOffsetToAPI;
  115. // }
  116. //
  117. typedef struct _INCLUDELIST
  118. {
  119. DWORD dwNext; // offset of next inclusion or 0 if this is the last
  120. INCTYPE eType; // whether this is an include or exclude
  121. DWORD dwModuleOffset; // the offset within the calling module of the call that should be included/excluded
  122. DWORD dwOffsetToAPI; // offset from struct begin to the string that tells what the API is, or 0 if there is none specified
  123. #pragma warning( disable : 4200 )
  124. TCHAR szModule[]; // text description for this patch blob
  125. #pragma warning( default : 4200 )
  126. // TCHAR szAPI [] // this is to let folks know that there is potentially another string -- see code above
  127. } INCLUDELIST, *PINCLUDELIST;
  128. // The following definations are for blob 0 which lists the DLL's and Applications and Exes
  129. // that are hooked and patches by the shim.
  130. typedef struct _DLLNAMELIST
  131. {
  132. DWORD dwNext; // offset of next DLL file in list or 0 if this is the last file in the list.
  133. DWORD dwBlobID; // id of blob image associated with this dll
  134. DWORD dwIncludeOffset; // offset of inclusion list
  135. PINCLUDELIST pIncludeList; // pointer to inclusion list
  136. #pragma warning( disable : 4200 )
  137. TCHAR szFileName[]; // file name for this blob
  138. #pragma warning( default : 4200 )
  139. } DLLNAMELIST, *PDLLNAMELIST;
  140. PDLLNAMELIST NextDllName(PDLLNAMELIST pCurrent);
  141. // The following definations are for blob 0 which lists the DLL's and Applications and Exes
  142. // that are hooked and patches by the shim.
  143. typedef struct _PATCHNAMELIST
  144. {
  145. DWORD dwNext; // offset of next DLL file in list or 0 if this is the last file in the list.
  146. DWORD dwBlobID; // id of blob image associated with this patch
  147. #pragma warning( disable : 4200 )
  148. TCHAR szDescription[]; // text description for this patch blob
  149. #pragma warning( default : 4200 )
  150. } PATCHNAMELIST, *PPATCHNAMELIST;
  151. // The following definations are for blob 0 which lists the DLL's and Applications and Exes
  152. // that are hooked and patches by the shim.
  153. typedef struct _MATCHINGINFO
  154. {
  155. DWORD dwNext; // next matching structure in list.
  156. DWORD dwSize; // size of binary image, 0 if size is not to be used as a matching criteria
  157. DWORD dwCrc; // crc of binary image, 0 if crc is not to be used.
  158. FILETIME ft; // date time file was created, 0 if time is not to be used.
  159. #pragma warning( disable : 4200 )
  160. TCHAR szFileName[]; // relative path file name of file to match against. This path is
  161. // relative to the EXE file above this one.
  162. #pragma warning( default : 4200 )
  163. } MATCHINGINFO, *PMATCHINGINFO;
  164. typedef struct _DLLREFLIST
  165. {
  166. DWORD dwNext;
  167. DWORD dwBlobID;
  168. DWORD dwIncludeOffset; // offset of inclusion list
  169. PINCLUDELIST pIncludeList; // pointer to inclusion list
  170. } DLLREFLIST, *PDLLREFLIST;
  171. typedef struct _EXELIST
  172. {
  173. DWORD dwNext; // next exe in list
  174. DWORD dwExeID; // id unique to this exe
  175. LARGE_INTEGER qwFlags; // the kernel flags
  176. PDWORD pdwBlobPatchID; // pointer to blob id array
  177. DWORD dwBlobPatchOffset; // offset of blob array
  178. DWORD dwTotalPatchBlobs; // total hook DLL's for this exe
  179. DWORD dwMatchInfoOffset; // offset of match info
  180. PMATCHINGINFO pMatchInfo; // pointer to matching info list
  181. DWORD dwDllListOffset; // offset to DLL list for this EXE
  182. PDLLREFLIST pDllList; // pointer to DLL list for this EXE
  183. #pragma warning( disable : 4200 )
  184. TCHAR szFileName[]; // file name that we are hooking with the shim dlls or patching with a patch.
  185. #pragma warning( default : 4200 )
  186. } EXELIST, *PEXELIST;
  187. typedef struct _BLOB0
  188. {
  189. DWORD dwVersion; // version of format
  190. DWORD dwMagic; // more verification of format
  191. DWORD dwBlobSize; // size of blob
  192. DWORD dwDllNameOffset; // offset in bytes where the dll list begins.
  193. DWORD dwPatchNameOffset; // offset in bytes where the patch list begins.
  194. DWORD dwExeGroupOffset; // offset in bytes where the Application group list begins.
  195. DWORD dwIncludeOffset; // offset in bytes where the global include list begins
  196. PDLLNAMELIST pDllNameList; // pointer to shim dll info
  197. PPATCHNAMELIST pPatchNameList; // pointer to patch blob info
  198. PEXELIST pExeList; // pointer to application blob info list
  199. PINCLUDELIST pIncludeList; // pointer to global include list (which generally only has excludes)
  200. } BLOB0, *PBLOB0;
  201. #pragma pack()
  202. // at init time, call InitializeDatabase to init the DBs global variables
  203. //
  204. // then before querying any information, call GetBlob0, and pass the returned pointer
  205. // into any query functions.
  206. //
  207. // When done with the DB, call FreeBlob0, followed by RestoreDatabase
  208. BOOL InitializeDatabase(LPSTR szPath);
  209. PBLOB0 GetBlob0(void);
  210. void FreeBlob0(PBLOB0 pBlob0);
  211. void RestoreDatabase(void);
  212. PEXELIST GetExe(
  213. PBLOB0 pBlob0,
  214. LPSTR szModuleName
  215. );
  216. DWORD GetExeHookDLLCount(
  217. PBLOB0 pBlob0,
  218. PEXELIST pExe
  219. );
  220. DWORD GetExePatchCount(
  221. PBLOB0 pBlob0,
  222. PEXELIST pExe
  223. );
  224. BOOL GetExeFlags(
  225. PEXELIST pExe,
  226. LARGE_INTEGER *pqwFlags // passed back flags
  227. );
  228. PBYTE GetHookDLLBlob(
  229. DWORD dwBlobID
  230. );
  231. PBYTE GetPatchBlob(
  232. DWORD dwBlobID
  233. );
  234. //
  235. // helpful iterators for moving around in Blob0
  236. //
  237. PEXELIST NextExe(PEXELIST pCurrent);
  238. PMATCHINGINFO NextMatchInfo(PMATCHINGINFO pCurrent);
  239. PINCLUDELIST NextInclude(PINCLUDELIST pCurrent);
  240. TCHAR *szGetAPIPtr(PINCLUDELIST pInclude); // get the API string out of the structure
  241. PPATCHNAMELIST NextPatchName(PPATCHNAMELIST pCurrent);
  242. PDLLNAMELIST NextDllName(PDLLNAMELIST pCurrent);
  243. PDLLREFLIST NextDllRef(PDLLREFLIST pCurrent);
  244. #ifdef __cplusplus
  245. };
  246. #endif
  247. #endif