简体   繁体   English

linux&c-通过地址访问磁盘上的文件?

[英]linux & c - access file on disk.. by address?

Is it possible to get to a file by address in ac program or in a terminal (linux)? 是否可以通过ac程序或终端(linux)中的地址获取文件? I know it sounds odd, to me either actually, but I am just experimenting. 我知道这听起来很奇怪,实际上对我来说,但我只是在尝试。

For example, replace 例如,替换

FILE * f = fopen("myfile.txt","r")

by something absolutely stunning like 绝对令人赞叹

int fd = open(&0x545f6f5,"r")

or echo &0x545f6f5 that would grab the part of the concerned file (would need a length so more like echo &0x545f6f5 20 to read the 20 bytes next to the address)? 或回声&0x545f6f5会抓取相关文件的一部分(是否需要像回声&0x545f6f5 20这样的长度才能读取地址旁边的20个字节)?

I know about mmap, but again, my question is much more something like experimentation. 我了解mmap,但是我的问题还是更多,例如实验。

Well, the overall picture is: can one access any part of a file on the linux filesystem with an address (and ideally a length)? 好吧,总体情况是:能否用地址(最好是长度)访问linux文件系统上文件的任何部分?

UPDATE: 更新:

Say my partition id /dev/sda1 and i want to access the raw memory value (readable or not) with an address and not a name. 说我的分区ID / dev / sda1,我想使用地址而不是名称来访问原始内存值(可读或不可读)。 If I seek for address &0x545f6f5 and that it happens to be myfile.txt at an offset of say 64, i would read the byte at this position. 如果我寻找地址&0x545f6f5并且恰好是myfile.txt,偏移量为64,我将在该位置读取该字节。 I hope it makes it clearer :) 我希望它可以使它更清晰:)

Let's say myfile.txt is on the ext3 filesystem mounted at / , and that filesystem is on the partition /dev/sda1 . 假设myfile.txt位于/挂载的ext3文件系统上,而该文件系统位于/dev/sda1分区上。 You could conceivably open the device /dev/sda1 (the partition) or /dev/sda (the entire drive) and access the file's bytes if you knew its offset on disk. 您可以想象打开设备/dev/sda1 (分区)或/dev/sda (整个驱动器),如果知道文件在磁盘上的偏移量,则访问文件的字节。

For example, if you somehow determined that the file's contents were at offset 0xDEADBEEF on the first partition of the first hard drive, you could do: 例如,如果您以某种方式确定文件的内容在第一个硬盘驱动器的第一个分区上的偏移量为0xDEADBEEF ,则可以执行以下操作:

int fd = open("/dev/sda1", O_RDONLY);
lseek(fd, 0xDEADBEEF, SEEK_SET);
read(fd, buffer, 20);

Accessing the raw device this way bypasses the filesystem. 以这种方式访问​​原始设备会绕过文件系统。 Filesystems are where file metadata is stored, such as their names, locations, and sizes. 文件系统是文件元数据的存储位置,例如文件系统的名称,位置和大小。 If you poked around the beginning of /dev/sda1 you could conceivably read the raw filesystem data yourself rather than relying on the kernel filesystem drivers to do it for you. 如果您拨开/dev/sda1的开头,可以想象自己可以读取原始文件系统数据,而不必依靠内核文件系统驱动程序来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM