-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfile_tamper.c
178 lines (124 loc) · 4.73 KB
/
file_tamper.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
// Author : Abhinav Thakur
// Email : [email protected]
// Description : This module contains all the operations that are being performed on files.
/*
struct stat {
dev_t st_dev; // ID of device containing file
ino_t st_ino; // inode number
mode_t st_mode; // protection
nlink_t st_nlink; // number of hard links
uid_t st_uid; // user ID of owner
gid_t st_gid; // group ID of owner
dev_t st_rdev; // device ID (if special file)
off_t st_size; // total size, in bytes
blksize_t st_blksize; // blocksize for file system I/O
blkcnt_t st_blocks; // number of 512B blocks allocated
time_t st_atime; // time of last access
time_t st_mtime; // time of last modification
time_t st_ctime; // time of last status change
};
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include "color.h"
#include "operations.h"
// Initially marks as empty list
FileList *head = NULL;
FileList *current_node = NULL;
// A wrapper function to create a new node to store Absolute path for filename. This does so by
// leveraging a chain of functionality : FileCreateNode() -> AddNode() -> AllocMemory()
void FileCreateNode(char *filepath)
{
// Check for the filepath length
if (strlen(filepath) > 499)
{
fprintf(stderr, RED"[-]"RESET"In %s : %s -> %s too long to be stored into FileList\n", __FILE__, __FUNCTION__, filepath);
exit(0x51);
}
//
AddNode(current_node, filepath);
}
// Checks if the node is the initial or intermediate node setting the current_node pointer
// accordingly and calling AllocMemory()
static void AddNode(FileList *current, char *filepath)
{
// Initialize the FilesList if its currently empty
if (head == NULL)
{
head = AllocMemory(filepath, NULL);
current_node = head;
}
// Add to the next of current node and point current_node to the new node added
else if (head != NULL)
{
current_node->next = AllocMemory(filepath, NULL);
current_node = current_node->next;
}
}
// Allocates space on heap segment for FileNode and is responsible for initializing the structure
// members of FileNode. Returns the address of new malloc'd memory.
static FileList *AllocMemory(char *filepath, FileList *next_node_address)
{
FileList *node = (FileList *) malloc( sizeof(FileList) );
if (node == NULL)
{
// Malloc failed. Exiting
fprintf(stderr, RED"[-] %s : %s while malloc(). Exiting ... \n\n", __FILE__, __FUNCTION__);
exit(0x50);
}
// Clear out the malloced memory and store the (filepath) and (address of next node) in it
// Be carefull while allocating space for node->name (an off-by-one bug can cause mindfucks)
memmove(node, "0", sizeof(FileList));
node->name = (char *) calloc( strlen(filepath) + 1, 1);
strncpy(node->name, filepath, strlen(filepath));
node->next = next_node_address;
return node;
}
// Prints the linked list nodes
void PrintLinkedList(FileList *start)
{
FileList *ptr = start;
fprintf(stdout, "\n\n");
do
{
fflush(stdout);
fprintf(stdout, "| "YELLOW"%s"RESET" : "RED"%p"RESET" | -> ", ptr->name, ptr->next);
ptr = ptr->next;
} while (ptr != NULL);
}
// Function returns filetype of a file reffered by its absolute pathname
const char *GetFileType(char *absolute_path)
{
// To get proper results (i.e. filetype from getfiletype's S_ISBLK() etc.) use the absolute
// pathname for file (i.e. from / directory) to identify correct file type, otherwise we
// won't get proper filetype.
struct stat s;
// extract the properties of the file into struct stat object - 's'
int status = lstat(absolute_path, &s);
// Here S_IFMT is the bitmask for filetype field. Alternatively we could use
// S_ISDIR(s.st_mode) to check if the current file is a directory
switch(s.st_mode & S_IFMT)
{
case S_IFBLK: return "Block device";
break;
case S_IFCHR: return "Character device";
break;
case S_IFDIR: return "Directory";
break;
case S_IFIFO: return "FIFO/PIPE";
break;
case S_IFLNK: return "Symbolic link";
break;
case S_IFREG: return "Regular file";
break;
case S_IFSOCK: return "Socket";
break;
default: return "Unknkown??";
break;
}
}