├── .gitignore ├── LICENSE.md ├── README.md ├── index.ts ├── package-lock.json ├── package.json └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | /dist/ 2 | /node_modules/ 3 | /npm-debug.log 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 NowSecure, Inc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # frida-memory-stream 2 | 3 | Create a stream from one or more memory regions. 4 | 5 | ## Example 6 | 7 | ```js 8 | import memoryStream from 'frida-memory-stream'; 9 | 10 | const m = Process.enumerateModules()[0]; 11 | memoryStream(m.base, m.size).pipe(networkStream); 12 | ``` 13 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | import { Readable } from 'stream'; 2 | 3 | export default make; 4 | export type RangeSpec = [base: NativePointer, size: number]; 5 | 6 | function make(base: NativePointer, size: number): MemoryStream; 7 | function make(ranges: RangeSpec[]): MemoryStream; 8 | function make(baseOrRanges: NativePointer | RangeSpec[], size?: number) { 9 | if (baseOrRanges instanceof NativePointer) 10 | return new MemoryStream([[baseOrRanges, size as number]]); 11 | else 12 | return new MemoryStream(baseOrRanges); 13 | } 14 | 15 | class MemoryStream extends Readable { 16 | #ranges: RangeSpec[]; 17 | 18 | constructor(ranges: RangeSpec[]) { 19 | super({ 20 | highWaterMark: 4 * 1024 * 1024 21 | }); 22 | 23 | this.#ranges = ranges; 24 | if (ranges.length === 0) 25 | this.push(null); 26 | } 27 | 28 | _read(size: number) { 29 | let wantMoreData = true; 30 | 31 | do { 32 | const range = this.#ranges[0]; 33 | 34 | const address = range[0]; 35 | const remaining = range[1]; 36 | if (remaining === 0) { 37 | this.#ranges.shift(); 38 | if (this.#ranges.length > 0) { 39 | continue; 40 | } else { 41 | this.push(null); 42 | return; 43 | } 44 | } 45 | 46 | const n = Math.min(remaining, size); 47 | 48 | range[0] = address.add(n); 49 | range[1] -= n; 50 | 51 | let chunk: ArrayBuffer; 52 | try { 53 | chunk = address.readByteArray(n) as ArrayBuffer; 54 | } catch (e) { 55 | this.emit('error', e); 56 | this.push(null); 57 | return; 58 | } 59 | 60 | wantMoreData = this.push(Buffer.from(chunk)); 61 | } while (wantMoreData); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frida-memory-stream", 3 | "version": "4.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "frida-memory-stream", 9 | "version": "4.0.0", 10 | "license": "MIT", 11 | "devDependencies": { 12 | "@types/frida-gum": "^18.3.2", 13 | "@types/node": "^20.3.1", 14 | "typescript": "^5.1.3" 15 | } 16 | }, 17 | "node_modules/@types/frida-gum": { 18 | "version": "18.3.2", 19 | "resolved": "https://registry.npmjs.org/@types/frida-gum/-/frida-gum-18.3.2.tgz", 20 | "integrity": "sha512-bso8bJRxrhchNRrOMmT18st/THIdjzxFdcMtJ/gGSYSknJeerQ8ArBwBtlWt/f3pqVVs1Mz1Fg6lvUdjXswa+g==", 21 | "dev": true 22 | }, 23 | "node_modules/@types/node": { 24 | "version": "20.3.1", 25 | "resolved": "https://registry.npmjs.org/@types/node/-/node-20.3.1.tgz", 26 | "integrity": "sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==", 27 | "dev": true 28 | }, 29 | "node_modules/typescript": { 30 | "version": "5.1.3", 31 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz", 32 | "integrity": "sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==", 33 | "dev": true, 34 | "bin": { 35 | "tsc": "bin/tsc", 36 | "tsserver": "bin/tsserver" 37 | }, 38 | "engines": { 39 | "node": ">=14.17" 40 | } 41 | } 42 | }, 43 | "dependencies": { 44 | "@types/frida-gum": { 45 | "version": "18.3.2", 46 | "resolved": "https://registry.npmjs.org/@types/frida-gum/-/frida-gum-18.3.2.tgz", 47 | "integrity": "sha512-bso8bJRxrhchNRrOMmT18st/THIdjzxFdcMtJ/gGSYSknJeerQ8ArBwBtlWt/f3pqVVs1Mz1Fg6lvUdjXswa+g==", 48 | "dev": true 49 | }, 50 | "@types/node": { 51 | "version": "20.3.1", 52 | "resolved": "https://registry.npmjs.org/@types/node/-/node-20.3.1.tgz", 53 | "integrity": "sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==", 54 | "dev": true 55 | }, 56 | "typescript": { 57 | "version": "5.1.3", 58 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz", 59 | "integrity": "sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==", 60 | "dev": true 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frida-memory-stream", 3 | "version": "4.0.0", 4 | "description": "Create a stream from one or more memory regions", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "type": "module", 8 | "files": [ 9 | "/dist/" 10 | ], 11 | "scripts": { 12 | "prepare": "npm run build", 13 | "build": "tsc", 14 | "watch": "tsc -w" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/nowsecure/frida-memory-stream.git" 19 | }, 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/nowsecure/frida-memory-stream/issues" 23 | }, 24 | "homepage": "https://github.com/nowsecure/frida-memory-stream#readme", 25 | "devDependencies": { 26 | "@types/frida-gum": "^18.3.2", 27 | "@types/node": "^20.3.1", 28 | "typescript": "^5.1.3" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "lib": ["ES2020"], 5 | "moduleResolution": "Node16", 6 | "outDir": "dist", 7 | "declaration": true, 8 | "strict": true 9 | }, 10 | "include": ["index.ts"] 11 | } 12 | --------------------------------------------------------------------------------