-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocks.c
176 lines (123 loc) · 3.5 KB
/
blocks.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "ext4.h"
unsigned long long file_size = 0;
unsigned long long cur_size = 0;
void hexdump(char *buf, int size)
{
int index = 0 ;
for (; size ; size--)
printf("%c",*buf++);
}
void print_content(int f, int length, unsigned long long off)
{
unsigned long long _off = lseek(f, 0, SEEK_CUR);
lseek(f, off*4096L, SEEK_SET);
char buffer[4096] = {} ;
int j = 0 ;
int length_1 = 0;
for(; j<length; j++) {
if ( (file_size-cur_size) > 4096)
length_1 = 4096;
else
length_1 = file_size % 4096 ;
read(f, buffer, length_1);
hexdump(buffer, length_1);
cur_size+=length_1;
}
lseek(f, _off, SEEK_SET);
}
void parse_ex_tree(int f, unsigned long long off)
{
struct ext4_extent_header eh ;
struct ext4_extent_idx ehid;
struct ext4_extent ex ;
struct ext4_extent *exp ;
if (off)
lseek(f, off*4096L, SEEK_SET);
read(f, &eh, sizeof (eh));
if (eh.eh_depth == 0 ) {
int j = 0;
for (; j<eh.eh_entries; j++) {
read(f, &ex, sizeof(ex));
if ( ex.ee_block != ex.ee_block+ex.ee_len-1 )
printf ("(%d-%d):%d-%d, ",ex.ee_block, ex.ee_block+ex.ee_len-1,
ex.ee_start_lo | (ex.ee_start_hi << 32),
(ex.ee_start_lo | (ex.ee_start_hi << 32)) + ex.ee_len-1);
else
printf ("(%d):%d, ",ex.ee_block,
ex.ee_start_lo | (ex.ee_start_hi << 32));
}
}else {
int j = 0;
for (; j<eh.eh_entries; j++) {
read(f, &ehid, sizeof(ehid));
printf ("(ETB0):%d, ", ehid.ei_leaf_lo| (ehid.ei_leaf_hi <<32));
parse_ex_tree( f, ehid.ei_leaf_lo| (ehid.ei_leaf_hi <<32) );
}
}
}
int main (int argc, char *argv[])
{
int f;
int index = 0;
int itable ;
char content[4096] = {} ;
struct ext4_inode ext4_inode;
struct ext4_group_desc gd ;
struct ext4_extent_header eh ;
struct ext4_extent_idx ehid;
struct ext4_extent ex ;
struct ext4_extent *exp ;
int f_size = 0;
if (argc < 3) {
printf ("Throw me an Disk name and Inode boss !!\n");
return 1;
}
f = open(DISK_NAME, O_RDONLY);
if ( f == -1 ) {
printf ("run with root permissions\n");
return -1;
}
int inode = atoi(argv[2]) -1;
int group = inode/8192 ;
int index_1 = inode % 8192;
int offset = index_1 * 256;
int gd_count = get_gd_count(f);
//printf ("gd size %d\n", gd_count);
lseek(f, 1024+1024, SEEK_CUR);
// leave out these x86 boot sector and padding && //readout superblock
lseek(f, 32 * (sizeof(struct ext4_group_desc)), SEEK_CUR);
int size_of_gd = get_desc_size(f);
for (; index < gd_count; index++){
read(f, &gd, size_of_gd);
if (gd.bg_checksum && index == group) {
//print_gd_info(index, &gd) ;
itable = (((ext4_64_t)(gd.bg_inode_table_hi) << 32) | gd.bg_inode_table_lo);
memset(&gd, 0, sizeof (gd));
}
}
int constant = 1 ;
lseek(f, 0, SEEK_SET);
lseek(f, itable*4096L, SEEK_CUR);
lseek(f, offset, SEEK_CUR);
memset(&ext4_inode, 0, sizeof (struct ext4_inode));
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
unsigned long long _off = read(f, 0, SEEK_CUR);
read(f, &ext4_inode, sizeof (struct ext4_inode));
if ( ! S_ISLNK(ext4_inode.i_mode) && ! S_ISREG(ext4_inode.i_mode)) {
printf ("Not a Regular file\n");
}
if (S_ISLNK(ext4_inode.i_mode)) {
char *uma = ext4_inode.i_block ;
printf ("%s\n", uma);
}else {
int loop_index = 0 ;
lseek(f, -sizeof (struct ext4_inode), SEEK_CUR);
lseek(f, offsetof(struct ext4_inode, i_block), SEEK_CUR);
file_size = ((ext4_inode.i_size_high << 32) | ext4_inode.i_size_lo);
parse_ex_tree(f, 0);
puts("");
}
return ;
printf ("pid %d \n", getpid());
while(1);
}