├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── mem-scraper.sln └── mem-scraper ├── mem-scraper.cpp ├── mem-scraper.hpp ├── mem-scraper.vcxproj └── mem-scraper.vcxproj.filters /.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 | # 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 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 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 LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd 364 | 365 | **/cache.txt 366 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 RadonCoding 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 | ![](https://user-images.githubusercontent.com/86915746/217642381-40d52ae2-e706-46d0-ae8b-1f6de966ccd2.gif) 2 | 3 | Usage: 4 | ``` 5 | Usage: mem-scraper.exe [option(s)] 6 | Options: 7 | -H --help Shows the usage of arguments 8 | -P --pid The target process identifier 9 | -N --name The target process name 10 | -F --filter The regex strings have to match (default = none) 11 | -T --target The place to search strings from (1 = heap, 2 = stack, default = both) 12 | -D --delay Delay between scans in milliseconds (default = 1000) 13 | ``` 14 | 15 | ### Prerequisites 16 | 1. Install [Visual Studio](https://visualstudio.microsoft.com/downloads) and enable **Desktop Development with C++** 17 | 18 | ### Compilation 19 | This project uses ANSI strings and C++ 20. Make sure to also link ntdll.lib. 20 | 21 | ### Credits 22 | This project is inspired by https://www.x86matthew.com/view_post?id=stack_scraper. 23 | 24 | ### Info 25 | It works by reading the process memory of an external process and tries to find strings on the heap and the stack. 26 | 27 | ### Contributing 28 | 1. Fork it 29 | 2. Create your branch (`git checkout -b my-change`) 30 | 3. Commit your changes (`git commit -m "changed something"`) 31 | 4. Push to the branch (`git push origin my-change`) 32 | 5. Create new pull request 33 | -------------------------------------------------------------------------------- /mem-scraper.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.4.33213.308 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mem-scraper", "mem-scraper\mem-scraper.vcxproj", "{EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}" 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 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Debug|x64.ActiveCfg = Debug|x64 17 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Debug|x64.Build.0 = Debug|x64 18 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Debug|x86.ActiveCfg = Debug|Win32 19 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Debug|x86.Build.0 = Debug|Win32 20 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Release|x64.ActiveCfg = Release|x64 21 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Release|x64.Build.0 = Release|x64 22 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Release|x86.ActiveCfg = Release|Win32 23 | {EE6C7256-4B7A-45EA-9571-5B6C5A15AF35}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {34CB2710-003A-420F-810A-F39CF7A51D52} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /mem-scraper/mem-scraper.cpp: -------------------------------------------------------------------------------- 1 | #include "mem-scraper.hpp" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | std::fstream _cacheFile(CACHE_PATH, std::ios::in | std::ios::out | std::ios::trunc); 9 | 10 | bool isANSIString(std::string str) { 11 | for (size_t i = 0; i < str.length(); i++) { 12 | char c = str[i]; 13 | 14 | if (c == 0x00) { 15 | break; 16 | } 17 | else if (c > 0x7F || (c < 0x20 && c != '\r' && c != '\n')) { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | 24 | bool isWideString(std::string str) { 25 | for (size_t i = 0; i < str.length(); i++) { 26 | char c = str[i]; 27 | 28 | if (i % 2 == 1) { 29 | if (c != 0x00) { 30 | return false; 31 | } 32 | continue; 33 | } 34 | 35 | if (c == 0x00) { 36 | break; 37 | } 38 | else if (c > 0x7F || (c < 0x20 && c != '\r' && c != '\n')) { 39 | return false; 40 | } 41 | } 42 | return true; 43 | } 44 | 45 | bool isCached(std::string str) { 46 | if (!_cacheFile.is_open()) { 47 | std::cout << "Failed to open cache file!" << std::endl; 48 | exit(EXIT_FAILURE); 49 | } 50 | 51 | _cacheFile.seekg(0, std::ios::beg); 52 | 53 | std::string line; 54 | 55 | while (std::getline(_cacheFile, line)) { 56 | if (line == str) { 57 | return true; 58 | } 59 | } 60 | _cacheFile.clear(); 61 | _cacheFile.seekp(0, std::ios::end); 62 | _cacheFile << str << std::endl; 63 | return false; 64 | } 65 | 66 | void processString(std::vector data, size_t* len, std::string filter, StringSource source) { 67 | size_t end = 0; 68 | 69 | for (size_t i = 0; i < data.size(); i++) { 70 | if (data[i] == '\0') { 71 | end = i; 72 | break; 73 | } 74 | } 75 | 76 | if (!end) return; 77 | 78 | std::string str(&data[0], end); 79 | str.erase(0, str.find_first_not_of(' ')); 80 | 81 | if (str.empty()) return; 82 | if (!isANSIString(str) && !isWideString(str)) return; 83 | if (len) *len = str.length(); 84 | if (str.length() - 1 <= 5) return; 85 | 86 | // Replace line breaks with a dot for ease of printing 87 | for (size_t i = 0; i < str.length(); i++) { 88 | if (str[i] == '\r' || str[i] == '\n') { 89 | str[i] = '.'; 90 | } 91 | } 92 | 93 | if (isCached(str)) return; 94 | 95 | std::smatch match; 96 | 97 | if (!filter.empty() && !std::regex_search(str, match, std::regex(filter))) return; 98 | 99 | switch (source) { 100 | case StringSource::LOCAL: 101 | std::cout << "Found local string: " << str << std::endl; 102 | break; 103 | case StringSource::POINTER: 104 | std::cout << "Found pointer string: " << str << std::endl; 105 | break; 106 | case StringSource::HEAP: 107 | std::cout << "Found heap string: " << str << std::endl; 108 | break; 109 | } 110 | } 111 | 112 | // Finds values from the stack that are raw values 113 | void findLocalStrings(std::vector stack, std::string filter) { 114 | for (size_t i = 0; i < stack.size(); i++) { 115 | size_t copyLen = stack.size() - i; 116 | 117 | if (copyLen > MAX_VALUE_SIZE) copyLen = MAX_VALUE_SIZE; 118 | 119 | std::vector value(copyLen); 120 | memcpy(&value[0], &stack[i], value.capacity()); 121 | 122 | size_t strLen = 0; 123 | processString(value, &strLen, filter, StringSource::LOCAL); 124 | 125 | if (strLen != 0) { 126 | i += strLen; 127 | } 128 | } 129 | } 130 | 131 | // Finds values from the stack that are pointers and then reads the values 132 | void findPointerStrings(std::vector stack, std::string filter, HANDLE process) { 133 | for (size_t i = 0; i < stack.size(); i++) { 134 | std::vector value(MAX_VALUE_SIZE); 135 | 136 | if (ReadProcessMemory(process, reinterpret_cast(stack[i]), &value[0], value.capacity(), nullptr)) { 137 | processString(value, nullptr, filter, StringSource::POINTER); 138 | } 139 | } 140 | } 141 | 142 | // Initializes the stack and calls the string capture functions 143 | void getStackStrings(HANDLE process, HANDLE thread, std::string filter) { 144 | THREAD_BASIC_INFORMATION tbi; 145 | memset(&tbi, 0, sizeof(tbi)); 146 | 147 | if (!NT_SUCCESS(NtQueryInformationThread(thread, static_cast(ThreadBasicInformation), &tbi, sizeof(tbi), nullptr))) { 148 | return; 149 | } 150 | 151 | NT_TIB teb; 152 | memset(&teb, 0, sizeof(teb)); 153 | 154 | if (!ReadProcessMemory(process, tbi.TebBaseAddress, &teb, sizeof(teb), 0)) { 155 | return; 156 | } 157 | 158 | size_t size = reinterpret_cast(teb.StackBase) - reinterpret_cast(teb.StackLimit); 159 | 160 | std::vector stack(size); 161 | 162 | if (!ReadProcessMemory(process, teb.StackLimit, &stack[0], stack.capacity(), nullptr)) { 163 | return; 164 | } 165 | 166 | findPointerStrings(stack, filter, process); 167 | findLocalStrings(stack, filter); 168 | } 169 | 170 | // Finds strings from the process heap 171 | void getHeapStrings(HANDLE process, std::string filter) { 172 | MEMORY_BASIC_INFORMATION mbi; 173 | 174 | // Loop all the memory pages and search contents for strings 175 | for (uint8_t* addr = nullptr; VirtualQueryEx(process, addr, &mbi, sizeof(mbi)); addr += mbi.RegionSize) { 176 | if (mbi.State != MEM_COMMIT || (mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD | PAGE_EXECUTE))) { 177 | continue; 178 | } 179 | 180 | std::vector page(mbi.RegionSize); 181 | 182 | if (!ReadProcessMemory(process, mbi.BaseAddress, &page[0], page.capacity(), nullptr)) { 183 | continue; 184 | } 185 | 186 | for (size_t i = 0; i < page.size(); i++) { 187 | if (page[i] == '\0') continue; 188 | 189 | size_t copyLen = page.size() - i; 190 | 191 | if (copyLen > MAX_VALUE_SIZE) copyLen = MAX_VALUE_SIZE; 192 | 193 | std::vector heapValue(copyLen); 194 | memcpy(&heapValue[0], &page[i], heapValue.capacity()); 195 | 196 | size_t strLen = 0; 197 | processString(heapValue, &strLen, filter, StringSource::HEAP); 198 | 199 | if (strLen != 0) i += strLen; 200 | } 201 | } 202 | } 203 | 204 | // Gets the system information for all processes in the system 205 | SYSTEM_PROCESS_INFORMATION* getProcessInfo() { 206 | ULONG length; 207 | NtQuerySystemInformation(SystemProcessInformation, nullptr, 0, &length); 208 | 209 | SYSTEM_PROCESS_INFORMATION* spi = reinterpret_cast(malloc(length)); 210 | 211 | if (!spi) { 212 | std::cout << std::format("Failed to allocate {} bytes of memory!", length) << std::endl; 213 | return nullptr; 214 | } 215 | 216 | if (!NT_SUCCESS(NtQuerySystemInformation(SystemProcessInformation, spi, length, nullptr))) { 217 | free(spi); 218 | std::cout << "Failed to get the process information! Trying again..." << std::endl; 219 | return getProcessInfo(); 220 | } 221 | return spi; 222 | } 223 | 224 | bool scanProcess(uint32_t pid, std::string filter, int target) { 225 | HANDLE process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, false, pid); 226 | 227 | if (!process) { 228 | std::cout << "Failed to find the process!" << std::endl; 229 | return false; 230 | } 231 | 232 | if (target == 0 || target == 1) { 233 | getHeapStrings(process, filter); 234 | } 235 | 236 | if (target == 0 || target == 2) { 237 | SYSTEM_PROCESS_INFORMATION* spi = getProcessInfo(); 238 | 239 | if (!spi) { 240 | return false; 241 | } 242 | 243 | SYSTEM_PROCESS_INFORMATION* current = reinterpret_cast( 244 | reinterpret_cast(spi) + spi->NextEntryOffset); 245 | 246 | // Loop until the current entry is the target process 247 | while (reinterpret_cast(current->UniqueProcessId) != pid) { 248 | if (!current->NextEntryOffset) { 249 | std::cout << "Failed to find the process!" << std::endl; 250 | return false; 251 | } 252 | current = reinterpret_cast(reinterpret_cast(current) + current->NextEntryOffset); 253 | } 254 | 255 | // The thread information is at the end of SYSTEN_PROCESS_INFORMATION 256 | SYSTEM_THREAD_INFORMATION* sti = reinterpret_cast(reinterpret_cast(current) + sizeof(SYSTEM_PROCESS_INFORMATION)); 257 | 258 | // Loop all the threads and capture the strings from the stack 259 | for (uint32_t i = 0; i < current->NumberOfThreads; i++) { 260 | HANDLE thread = nullptr; 261 | 262 | OBJECT_ATTRIBUTES attr; 263 | memset(&attr, 0, sizeof(attr)); 264 | attr.Length = sizeof(attr); 265 | 266 | // We use NtOpenThread so we can pass the CLIENT_ID which OpenThread can't do 267 | 268 | if (NT_SUCCESS(NtOpenThread(&thread, THREAD_QUERY_INFORMATION, &attr, &sti->ClientId))) { 269 | getStackStrings(process, thread, filter); 270 | CloseHandle(thread); 271 | } 272 | sti++; 273 | } 274 | free(spi); 275 | } 276 | 277 | CloseHandle(process); 278 | 279 | return true; 280 | } 281 | 282 | uint32_t getProcessByName(std::string name) { 283 | PROCESSENTRY32 entry; 284 | memset(&entry, 0, sizeof(PROCESSENTRY32)); 285 | entry.dwSize = sizeof(PROCESSENTRY32); 286 | 287 | HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 288 | 289 | uint32_t pid = 0; 290 | 291 | if (Process32First(hSnapshot, &entry)) { 292 | while (Process32Next(hSnapshot, &entry)) { 293 | if (entry.szExeFile == name) { 294 | pid = entry.th32ProcessID; 295 | break; 296 | } 297 | } 298 | } 299 | CloseHandle(hSnapshot); 300 | return pid; 301 | } 302 | 303 | int main(int argc, char** argv) { 304 | InputParser input(argc, argv); 305 | input.parse(); 306 | 307 | if (input.contains("help")) { 308 | input.usage(); 309 | } 310 | 311 | std::string filter; 312 | input.get("filter", &filter); 313 | 314 | uint32_t pid = 0; 315 | input.get("pid", &pid); 316 | 317 | std::string name; 318 | 319 | if (input.get("name", &name)) { 320 | pid = getProcessByName(name); 321 | 322 | if (!pid) { 323 | std::cout << "Could not find the process!" << std::endl; 324 | return -1; 325 | } 326 | } 327 | 328 | if (!pid) { 329 | input.usage(); 330 | return -1; 331 | } 332 | 333 | int target = 0; 334 | input.get("target", &target); 335 | 336 | DWORD delay = 1000; 337 | input.get("delay", &delay); 338 | 339 | while (scanProcess(pid, filter, target)) { 340 | Sleep(delay); 341 | } 342 | _cacheFile.close(); 343 | return 0; 344 | } 345 | -------------------------------------------------------------------------------- /mem-scraper/mem-scraper.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef struct _THREAD_BASIC_INFORMATION 11 | { 12 | NTSTATUS ExitStatus; 13 | PVOID TebBaseAddress; 14 | CLIENT_ID ClientId; 15 | KAFFINITY AffinityMask; 16 | KPRIORITY Priority; 17 | KPRIORITY BasePriority; 18 | } THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION; 19 | 20 | typedef NTSTATUS(WINAPI *f_NtOpenThread)(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, CLIENT_ID *ClientId); 21 | 22 | f_NtOpenThread NtOpenThread = (f_NtOpenThread)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtOpenThread"); 23 | 24 | constexpr DWORD ThreadBasicInformation = 0; 25 | constexpr DWORD MAX_STACK_SIZE = (1024 * 1024) / sizeof(DWORD); 26 | constexpr DWORD MAX_VALUE_SIZE = 1024; 27 | constexpr const char *CACHE_PATH = "cache.txt"; 28 | 29 | enum StringSource 30 | { 31 | LOCAL, 32 | POINTER, 33 | HEAP 34 | }; 35 | 36 | class Argument 37 | { 38 | public: 39 | std::string id; 40 | std::string shortToken; 41 | std::string longToken; 42 | std::string description; 43 | 44 | Argument(std::string id, std::string shortToken, std::string longToken, std::string description) 45 | { 46 | this->id = id; 47 | this->shortToken = shortToken; 48 | this->longToken = longToken; 49 | this->description = description; 50 | } 51 | }; 52 | 53 | std::vector arguments = { 54 | Argument("help", "-H", "--help", "Shows the usage of arguments"), 55 | Argument("pid", "-P", "--pid", "The target process identifier"), 56 | Argument("name", "-N", "--name", "The target process name"), 57 | Argument("filter", "-F", "--filter", "The regex strings have to match (default = none)"), 58 | Argument("target", "-T", "--target", "The place to search strings from (1 = heap, 2 = stack, default = both)"), 59 | Argument("delay", "-D", "--delay", "Delay between scans in milliseconds (default = 1000)")}; 60 | 61 | class InputParser 62 | { 63 | public: 64 | InputParser(int &argc, char **argv) 65 | { 66 | std::string path(argv[0]); 67 | this->filename = path.substr(path.find_last_of("/\\") + 1); 68 | 69 | for (int i = 1; i < argc; i++) 70 | { 71 | this->tokens.push_back(std::string(argv[i])); 72 | } 73 | } 74 | 75 | void parse() 76 | { 77 | for (Argument arg : arguments) 78 | { 79 | std::vector::const_iterator itShort = std::find(this->tokens.begin(), this->tokens.end(), arg.shortToken), 80 | itLong = std::find(this->tokens.begin(), this->tokens.end(), arg.longToken); 81 | 82 | if (itShort != this->tokens.end() && ++itShort != this->tokens.end()) 83 | { 84 | this->parsed.insert(std::make_pair(arg.id, *itShort)); 85 | } 86 | else if (itLong != this->tokens.end() && ++itLong != this->tokens.end()) 87 | { 88 | this->parsed.insert(std::make_pair(arg.id, *itLong)); 89 | } 90 | } 91 | } 92 | 93 | template 94 | bool get(std::string name, T *value) 95 | { 96 | if (!this->contains(name)) 97 | { 98 | return false; 99 | } 100 | 101 | if (value) 102 | { 103 | std::stringstream ss{this->parsed[name]}; 104 | ss >> *value; 105 | } 106 | return true; 107 | } 108 | 109 | bool contains(std::string name) 110 | { 111 | return this->parsed.contains(name); 112 | } 113 | 114 | void usage() 115 | { 116 | std::cout << std::format("Usage: {} [option(s)]", this->filename) << std::endl; 117 | std::cout << "Options: " << std::endl; 118 | 119 | for (Argument arg : arguments) 120 | { 121 | std::cout << std::format("{} {} {}", arg.shortToken, arg.longToken, arg.description) << std::endl; 122 | } 123 | } 124 | 125 | private: 126 | std::string filename; 127 | std::vector tokens; 128 | std::map parsed; 129 | }; 130 | -------------------------------------------------------------------------------- /mem-scraper/mem-scraper.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 | 16.0 23 | Win32Proj 24 | {ee6c7256-4b7a-45ea-9571-5b6c5a15af35} 25 | memscraper 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 | MultiByte 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | MultiByte 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | stdcpp20 108 | 109 | 110 | Console 111 | true 112 | ntdll.lib;%(AdditionalDependencies) 113 | 114 | 115 | 116 | 117 | Level3 118 | true 119 | true 120 | true 121 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 122 | true 123 | stdcpp20 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | ntdll.lib;%(AdditionalDependencies) 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /mem-scraper/mem-scraper.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 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | --------------------------------------------------------------------------------