How to copy files directly on Frida
Frida를 이용하여 파일을 복사해야 하는 경우, 다음의 스크립트를 활용할 수 있다.
1. FileChannel
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Java.perform(function(){ const File = Java.use('java.io.File'); const FileInputStream = Java.use('java.io.FileInputStream'); const FileOutputStream = Java.use('java.io.FileOutputStream'); const FileChannel = Java.use('java.nio.channels.FileChannel') var sourceFile = File.$new.overload('java.lang.String').call(File, '/data/local/tmp/test.js'); if (sourceFile.exists() && sourceFile.canRead()) { var destinationFile = File.$new.overload('java.lang.String').call(File, '/data/data/net.xiphiasilver.c0pyc4t/cache/test.bak'); destinationFile.createNewFile(); var fileInputStream = FileInputStream.$new.overload('java.io.File').call(FileInputStream, sourceFile); var fileOutputStream = FileOutputStream.$new.overload('java.io.File').call(FileOutputStream, destinationFile); var fileInputStreamChannel = fileInputStream.getChannel(); var fileOutputStreamChannel = fileOutputStream.getChannel(); fileInputStreamChannel.transferTo(0, fileInputStreamChannel.size(), fileOutputStreamChannel); fileInputStream.close(); fileOutputStream.close(); } else { console.log('Error : File cannot read.') } }) |
2. BufferedInput/OutputStream
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Java.perform(function(){ const File = Java.use('java.io.File'); const FileInputStream = Java.use('java.io.FileInputStream'); const FileOutputStream = Java.use('java.io.FileOutputStream'); const BufferedInputStream = Java.use('java.io.BufferedInputStream'); const BufferedOutputStream = Java.use('java.io.BufferedOutputStream'); var sourceFile = File.$new.overload('java.lang.String').call(File, '/data/local/tmp/test.js'); if (sourceFile.exists() && sourceFile.canRead()) { var destinationFile = File.$new.overload('java.lang.String').call(File, '/data/data/net.xiphiasilver.c0pyc4t/cache/test.bak'); destinationFile.createNewFile(); var fileInputStream = FileInputStream.$new.overload('java.io.File').call(FileInputStream, sourceFile); var fileOutputStream = FileOutputStream.$new.overload('java.io.File').call(FileOutputStream, destinationFile); var bufferedInputStream = BufferedInputStream.$new.overload('java.io.InputStream').call(BufferedInputStream, fileInputStream); var bufferedOutputStream = BufferedOutputStream.$new.overload('java.io.OutputStream').call(BufferedOutputStream, fileOutputStream); var data = 0; while ((data = bufferedInputStream.read()) != -1) { bufferedOutputStream.write(data); console.log('buffuredInputStream : ' + data); } bufferedInputStream.close(); fileInputStream.close(); bufferedOutputStream.close(); fileOutputStream.close(); } else { console.log('Error : File cannot read.') } }) |
3. File.copy
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Java.perform(function () { const Files = Java.use("java.nio.file.Files"); const Paths = Java.use("java.nio.file.Paths"); const StandardCopyOption = Java.use("java.nio.file.StandardCopyOption"); // 1. 원본 파일 및 대상 경로 설정 const sourcePath = "/data/user/0/com.package.name/databases/large_data.db"; const targetPath = "/sdcard/extracted_data.db"; try { const source = Paths.get(sourcePath, []); const target = Paths.get(targetPath, []); // REPLACE_EXISTING 옵션 설정 (기존 파일이 있으면 덮어쓰기) const copyOption = Java.cast(StandardCopyOption.REPLACE_EXISTING.value, Java.use("java.nio.file.CopyOption")); // 2. 고속 복사 실행 (OS 레벨 copy) Files.copy(source, target, [copyOption]); } catch (err) { console.error(err); } }); |