-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack.c
131 lines (113 loc) · 2.89 KB
/
hack.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#define BUFSIZE 128
typedef char * string;
// taken from: https://stackoverflow.com/questions/47116974/remove-a-substring-from-a-string-in-c
char * strremove(char * str,
const char * sub) {
char * p, * q, * r;
if ( * sub && (q = r = strstr(str, sub)) != NULL) {
size_t len = strlen(sub);
while ((r = strstr(p = r + len, sub)) != NULL) {
while (p < r)
*
q++ = * p++;
}
while (( * q++ = * p++) != '\0')
continue;
}
return str;
}
string webCheck(char url[BUFSIZE]) {
char command[BUFSIZE];
strcpy(command, "curl -s -I -L -o /dev/null -w \"%{scheme} %{url_effective}\" ");
FILE * fp;
char buf[BUFSIZE];
int status;
char string_code[BUFSIZE];
char url_header[BUFSIZE];
strcpy(command, strcat(command, url));
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n");
exit(1);
}
printf("checking website\n");
fgets(buf, BUFSIZE, fp);
if (buf == NULL) {
printf("error\n");
exit(1);
}
strcpy(url_header, strtok(buf, " "));
// make url_header lowercase
for (int i = 0; i < strlen(url_header); i++) {
url_header[i] = tolower(url_header[i]);
}
//append :// to url_header
strcpy(url_header, strcat(url_header, "://"));
strcpy(url, strtok(NULL, " "));
// removing / from the end of the url
if (url[strlen(url) - 1] == 47) {
url[strlen(url) - 1] = '\0';
}
// remove url_header from the beginning of the url
strcpy(url, strremove(url, url_header));
return url;
}
int main(int argc, string argv[]) {
system("clear");
printf("\x1b[40;38;5;34m");
if (argc != 2) {
printf("Usage: %s <website-url>\n", argv[0]);
return 1;
}
if (getuid()) {
printf("This program requires root acces!\n");
printf("Try running this program with sudo.\n");
printf("Like: sudo %s %s\n", argv[0], argv[1]);
return 1;
}
char urla[BUFSIZE];
char url[BUFSIZE];
// running the webCheck function and getting the url
strcpy(urla, webCheck(argv[1]));
char hosts[BUFSIZE] = "127.0.0.1 ";
strcpy(hosts, strcat(hosts, urla));
strcpy(hosts, strcat(hosts, "\n"));
FILE * fp = fopen("/etc/hosts", "r");
if (fp == NULL) {
printf("Failed to open file\n");
exit(1);
}
fseek(fp, 0, SEEK_SET);
FILE * fp1 = fopen("temp.txt", "w");
if (fp1 == NULL) {
printf("Failed to open file\n");
exit(1);
}
int i = 0;
while (fgets(url, BUFSIZE, fp) != NULL) {
if (i == 2) {
fprintf(fp1, "%s", hosts);
i++;
}
fprintf(fp1, "%s", url);
i++;
}
fclose(fp);
fclose(fp1);
fp = fopen("/etc/hosts", "w");
fp1 = fopen("temp.txt", "r");
while (fgets(url, BUFSIZE, fp1) != NULL) {
fprintf(fp, "%s", url);
}
fclose(fp);
fclose(fp1);
remove("temp.txt");
printf("%s is down!\n", urla);
printf("\x1b[0m"); // reset color
return 0;
}