├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── universal-syscall-64.sln └── universal-syscall-64 ├── Program.cs ├── USysCall64.cs └── universal-syscall-64.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Jack Ullrich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # universal-syscall-64 2 | Resolve syscall numbers at runtime for all Windows versions. 3 | -------------------------------------------------------------------------------- /universal-syscall-64.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30907.101 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "universal-syscall-64", "universal-syscall-64\universal-syscall-64.csproj", "{BC304609-8B9B-4E7C-8B70-A2EFFEAFB4DB}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {BC304609-8B9B-4E7C-8B70-A2EFFEAFB4DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {BC304609-8B9B-4E7C-8B70-A2EFFEAFB4DB}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {BC304609-8B9B-4E7C-8B70-A2EFFEAFB4DB}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {BC304609-8B9B-4E7C-8B70-A2EFFEAFB4DB}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {D641DC66-0D94-408E-995A-91C7985570AB} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /universal-syscall-64/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | 4 | namespace winternl 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | if (IntPtr.Size == 4) 11 | { 12 | return; 13 | } 14 | 15 | uint dwOld = 0; 16 | IntPtr allocMem = USysCall64.AllocateMemory((IntPtr)0, 0x1000, 0x00001000, 0x40); 17 | bool bSuccess = USysCall64.ProtectMemory(allocMem, 0x1000, 0x10, ref dwOld); 18 | 19 | Debugger.Break(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /universal-syscall-64/USysCall64.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.IO.MemoryMappedFiles; 5 | using System.Linq; 6 | using System.Runtime.InteropServices; 7 | 8 | /* 9 | * Author: @winternl 10 | */ 11 | 12 | public unsafe class USysCall64 13 | { 14 | #region PINVOKE 15 | 16 | [DllImport("ntdll")] 17 | private static extern long LdrGetDllHandle(IntPtr pwPath, IntPtr unused, ref UNICODE_STRING pszModule, ref UIntPtr ldrHandle); 18 | 19 | #endregion 20 | 21 | #region STRUCTS 22 | 23 | [StructLayout(LayoutKind.Sequential)] 24 | private struct UNICODE_STRING : IDisposable 25 | { 26 | public ushort Length; 27 | public ushort MaximumLength; 28 | private IntPtr buffer; 29 | 30 | public UNICODE_STRING(string s) 31 | { 32 | Length = (ushort)(s.Length * 2); 33 | MaximumLength = (ushort)(Length + 2); 34 | buffer = Marshal.StringToHGlobalUni(s); 35 | } 36 | 37 | public void Dispose() 38 | { 39 | Marshal.FreeHGlobal(buffer); 40 | buffer = IntPtr.Zero; 41 | } 42 | 43 | public override string ToString() 44 | { 45 | return Marshal.PtrToStringUni(buffer); 46 | } 47 | } 48 | 49 | [StructLayout(LayoutKind.Sequential, Pack = 1)] 50 | private struct IMAGE_DOS_HEADER 51 | { 52 | public UInt16 e_magic; // Magic number 53 | public UInt16 e_cblp; // Bytes on last page of file 54 | public UInt16 e_cp; // Pages in file 55 | public UInt16 e_crlc; // Relocations 56 | public UInt16 e_cparhdr; // Size of header in paragraphs 57 | public UInt16 e_minalloc; // Minimum extra paragraphs needed 58 | public UInt16 e_maxalloc; // Maximum extra paragraphs needed 59 | public UInt16 e_ss; // Initial (relative) SS value 60 | public UInt16 e_sp; // Initial SP value 61 | public UInt16 e_csum; // Checksum 62 | public UInt16 e_ip; // Initial IP value 63 | public UInt16 e_cs; // Initial (relative) CS value 64 | public UInt16 e_lfarlc; // File address of relocation table 65 | public UInt16 e_ovno; // Overlay number 66 | public UInt16 e_res_0; // Reserved words 67 | public UInt16 e_res_1; // Reserved words 68 | public UInt16 e_res_2; // Reserved words 69 | public UInt16 e_res_3; // Reserved words 70 | public UInt16 e_oemid; // OEM identifier (for e_oeminfo) 71 | public UInt16 e_oeminfo; // OEM information; e_oemid specific 72 | public UInt16 e_res2_0; // Reserved words 73 | public UInt16 e_res2_1; // Reserved words 74 | public UInt16 e_res2_2; // Reserved words 75 | public UInt16 e_res2_3; // Reserved words 76 | public UInt16 e_res2_4; // Reserved words 77 | public UInt16 e_res2_5; // Reserved words 78 | public UInt16 e_res2_6; // Reserved words 79 | public UInt16 e_res2_7; // Reserved words 80 | public UInt16 e_res2_8; // Reserved words 81 | public UInt16 e_res2_9; // Reserved words 82 | public UInt32 e_lfanew; // File address of new exe header 83 | } 84 | 85 | [StructLayout(LayoutKind.Sequential, Pack = 1)] 86 | private struct IMAGE_OPTIONAL_HEADER64 87 | { 88 | public UInt16 Magic; 89 | public Byte MajorLinkerVersion; 90 | public Byte MinorLinkerVersion; 91 | public UInt32 SizeOfCode; 92 | public UInt32 SizeOfInitializedData; 93 | public UInt32 SizeOfUninitializedData; 94 | public UInt32 AddressOfEntryPoint; 95 | public UInt32 BaseOfCode; 96 | public UInt64 ImageBase; 97 | public UInt32 SectionAlignment; 98 | public UInt32 FileAlignment; 99 | public UInt16 MajorOperatingSystemVersion; 100 | public UInt16 MinorOperatingSystemVersion; 101 | public UInt16 MajorImageVersion; 102 | public UInt16 MinorImageVersion; 103 | public UInt16 MajorSubsystemVersion; 104 | public UInt16 MinorSubsystemVersion; 105 | public UInt32 Win32VersionValue; 106 | public UInt32 SizeOfImage; 107 | public UInt32 SizeOfHeaders; 108 | public UInt32 CheckSum; 109 | public UInt16 Subsystem; 110 | public UInt16 DllCharacteristics; 111 | public UInt64 SizeOfStackReserve; 112 | public UInt64 SizeOfStackCommit; 113 | public UInt64 SizeOfHeapReserve; 114 | public UInt64 SizeOfHeapCommit; 115 | public UInt32 LoaderFlags; 116 | public UInt32 NumberOfRvaAndSizes; 117 | 118 | public IMAGE_DATA_DIRECTORY ExportTable; 119 | public IMAGE_DATA_DIRECTORY ImportTable; 120 | public IMAGE_DATA_DIRECTORY ResourceTable; 121 | public IMAGE_DATA_DIRECTORY ExceptionTable; 122 | public IMAGE_DATA_DIRECTORY CertificateTable; 123 | public IMAGE_DATA_DIRECTORY BaseRelocationTable; 124 | public IMAGE_DATA_DIRECTORY Debug; 125 | public IMAGE_DATA_DIRECTORY Architecture; 126 | public IMAGE_DATA_DIRECTORY GlobalPtr; 127 | public IMAGE_DATA_DIRECTORY TLSTable; 128 | public IMAGE_DATA_DIRECTORY LoadConfigTable; 129 | public IMAGE_DATA_DIRECTORY BoundImport; 130 | public IMAGE_DATA_DIRECTORY IAT; 131 | public IMAGE_DATA_DIRECTORY DelayImportDescriptor; 132 | public IMAGE_DATA_DIRECTORY CLRRuntimeHeader; 133 | public IMAGE_DATA_DIRECTORY Reserved; 134 | } 135 | 136 | [StructLayout(LayoutKind.Sequential)] 137 | private struct IMAGE_DATA_DIRECTORY 138 | { 139 | public UInt32 VirtualAddress; 140 | public UInt32 Size; 141 | } 142 | 143 | [StructLayout(LayoutKind.Sequential)] 144 | private struct IMAGE_EXPORT_DIRECTORY 145 | { 146 | public UInt32 Characteristics; 147 | public UInt32 TimeDateStamp; 148 | public UInt16 MajorVersion; 149 | public UInt16 MinorVersion; 150 | public UInt32 Name; 151 | public UInt32 Base; 152 | public UInt32 NumberOfFunctions; 153 | public UInt32 NumberOfNames; 154 | public UInt32 AddressOfFunctions; 155 | public UInt32 AddressOfNames; 156 | public UInt32 AddressOfNameOrdinals; 157 | } 158 | 159 | #endregion 160 | 161 | private static readonly Dictionary SysCallTable; 162 | 163 | // TODO: Stack alignment 164 | private static readonly byte[] SysCallStub = 165 | { 166 | 0x4C, 0x8B, 0xD1, // mov r10, rcx 167 | 0xB8, 0x00, 0x00, 0x00, 0x00, // mov eax, sys_no 168 | 0x0F, 0x05, // syscall 169 | 0xC3 // retn 170 | }; 171 | 172 | static unsafe USysCall64() 173 | { 174 | UNICODE_STRING szNtdll = new UNICODE_STRING("ntdll"); 175 | UIntPtr ptrNtdll = UIntPtr.Zero; 176 | 177 | long ntstatus = LdrGetDllHandle(IntPtr.Zero, IntPtr.Zero, ref szNtdll, ref ptrNtdll); 178 | 179 | if (ntstatus != 0) 180 | { 181 | Debugger.Break(); 182 | } 183 | 184 | byte* lpNtdll = (byte*)ptrNtdll; 185 | IMAGE_DOS_HEADER* piDH = (IMAGE_DOS_HEADER*)lpNtdll; 186 | IMAGE_OPTIONAL_HEADER64* piOH = (IMAGE_OPTIONAL_HEADER64*)(lpNtdll + piDH->e_lfanew + 0x18); 187 | IMAGE_EXPORT_DIRECTORY* exportDir = (IMAGE_EXPORT_DIRECTORY*)(lpNtdll + piOH->ExportTable.VirtualAddress); 188 | 189 | uint* names = (uint*)(lpNtdll + exportDir->AddressOfNames); 190 | uint* functions = (uint*)(lpNtdll + exportDir->AddressOfFunctions); 191 | ushort* ordinals = (ushort*)(lpNtdll + exportDir->AddressOfNameOrdinals); 192 | 193 | var listOfNames = new List(); 194 | 195 | var dictOfZwFunctions = new Dictionary(); 196 | 197 | for (int i = 0; i < exportDir->NumberOfNames; i++) 198 | { 199 | var name = Marshal.PtrToStringAnsi(new IntPtr(lpNtdll + names[i])); 200 | 201 | if (!name.StartsWith("Zw")) 202 | { 203 | continue; 204 | } 205 | 206 | var fnAddr = new UIntPtr(lpNtdll + functions[ordinals[i]]); 207 | 208 | dictOfZwFunctions.Add(name, fnAddr.ToUInt64()); 209 | } 210 | 211 | var sortedByAddr = dictOfZwFunctions 212 | .OrderBy(x => x.Value) 213 | .ToDictionary(x => "Nt" + x.Key.Substring(2, x.Key.Length - 2), x => x.Value); 214 | 215 | var sysCallLookup = new Dictionary(); 216 | 217 | uint sysNo = 0; 218 | 219 | foreach (var entry in sortedByAddr) 220 | { 221 | sysCallLookup.Add(entry.Key, sysNo); 222 | sysNo++; 223 | } 224 | 225 | SysCallTable = sysCallLookup; 226 | } 227 | 228 | private static unsafe T AllocRWX(byte[] lpBuffer) where T : class 229 | { 230 | try 231 | { 232 | var mapName = Guid.NewGuid().ToString(); 233 | var mapFile = MemoryMappedFile.CreateNew(mapName, lpBuffer.Length, MemoryMappedFileAccess.ReadWriteExecute); 234 | var mapView = mapFile.CreateViewAccessor(0, lpBuffer.Length, MemoryMappedFileAccess.ReadWriteExecute); 235 | 236 | mapView.WriteArray(0, lpBuffer, 0, lpBuffer.Length); 237 | 238 | byte* ptrShellcode = (byte*)IntPtr.Zero; 239 | mapView.SafeMemoryMappedViewHandle.AcquirePointer(ref ptrShellcode); 240 | 241 | return (T)(object)Marshal.GetDelegateForFunctionPointer((IntPtr)ptrShellcode, typeof(T)); 242 | } 243 | catch 244 | { 245 | return null; 246 | } 247 | } 248 | private static byte[] GetSysCallStub(uint sysNo) 249 | { 250 | byte[] locBuffer = new byte[SysCallStub.Length]; 251 | byte[] no = BitConverter.GetBytes(sysNo); 252 | 253 | SysCallStub.CopyTo(locBuffer, 0); 254 | Buffer.BlockCopy(no, 0, locBuffer, 4, 4); 255 | return locBuffer; 256 | } 257 | 258 | #region NTAPI Delegates 259 | 260 | private delegate long NtAllocateVirtualMemory(IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, ref IntPtr RegionSize, uint AllocationType, uint Protect); 261 | 262 | private delegate long NtProtectVirtualMemory(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref uint NumberOfBytesToProtect, uint NewAccessProtection, ref uint OldAccessProtection); 263 | 264 | #endregion 265 | 266 | public static IntPtr AllocateMemory(IntPtr Address, uint Size, uint AllocType, uint Protect) 267 | { 268 | return AllocateMemoryEx((IntPtr)(-1), Address, Size, AllocType, Protect); 269 | } 270 | 271 | public static IntPtr AllocateMemoryEx(IntPtr ProcessHandle, IntPtr Address, uint Size, uint AllocType, uint Protect) 272 | { 273 | var sc = GetSysCallStub(SysCallTable["NtAllocateVirtualMemory"]); 274 | 275 | var AllocMem = AllocRWX(sc); 276 | 277 | if (AllocMem == null) 278 | { 279 | return IntPtr.Zero; 280 | } 281 | 282 | IntPtr baseAddress = Address; 283 | IntPtr regionSize = (IntPtr)Size; 284 | 285 | long ntStatus = AllocMem(ProcessHandle, ref baseAddress, IntPtr.Zero, ref regionSize, AllocType, Protect); 286 | 287 | if (ntStatus != 0) 288 | { 289 | return IntPtr.Zero; 290 | } 291 | else 292 | { 293 | return baseAddress; 294 | } 295 | } 296 | 297 | public static bool ProtectMemory(IntPtr Address, uint ProtectSize, uint NewProtection, ref uint OldProtection) 298 | { 299 | return ProtectMemoryEx((IntPtr)(-1), Address, ProtectSize, NewProtection, ref OldProtection); 300 | } 301 | 302 | public static bool ProtectMemoryEx(IntPtr ProcessHandle, IntPtr Address, uint ProtectSize, uint NewProtection, ref uint OldProtection) 303 | { 304 | var sc = GetSysCallStub(SysCallTable["NtProtectVirtualMemory"]); 305 | var ProtectMem = AllocRWX(sc); 306 | 307 | if (ProtectMem == null) 308 | { 309 | return false; 310 | } 311 | 312 | IntPtr baseAddress = Address; 313 | uint protSize = ProtectSize; 314 | 315 | long ntStatus = ProtectMem((IntPtr)ProcessHandle, ref baseAddress, ref protSize, NewProtection, ref OldProtection); 316 | 317 | if (ntStatus != 0) 318 | { 319 | return false; 320 | } 321 | else 322 | { 323 | return true; 324 | } 325 | } 326 | } -------------------------------------------------------------------------------- /universal-syscall-64/universal-syscall-64.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {BC304609-8B9B-4E7C-8B70-A2EFFEAFB4DB} 8 | Exe 9 | winternl 10 | universal-syscall-64 11 | v4.0 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | true 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | true 35 | 36 | 37 | 38 | 39 | 40 | 41 | --------------------------------------------------------------------------------