-
Notifications
You must be signed in to change notification settings - Fork 0
/
applets.c
126 lines (112 loc) · 2.99 KB
/
applets.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
/*
* File: applets.h
* Author: sansinyang
* email: [email protected]
* Created on 2017.11.23
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
/* Generate random alphabet basename */
void rand_name(char *fname);
/* Signal handler */
void signalHandler(int sig);
int main(int argc, char *argv[]){
/* SIGINT contain in mask */
sigset_t unblockSet, prevMask;
sigemptyset(&unblockSet);
sigaddset(&unblockSet, SIGINT);
sigprocmask(SIG_SETMASK, &prevMask, NULL);
sigprocmask(SIG_UNBLOCK, &unblockSet, &prevMask);
pid_t current_pid = getpid();
pid_t parent_pid = getppid();
printf("P:%ld C:%ld\n", parent_pid, current_pid);
/* receive SIGINT */
signal(SIGINT, signalHandler);
while(1)
pause();
}
void signalHandler(int sig){
char *s="\
#include <stdio.h>%c\
#include <signal.h>%c\
#include <string.h>%c\
#include <stdlib.h>%c\
#include <sys/types.h>%c\
#include <unistd.h>%c\
void signalHandler(int sig);\
void rand_name(char *fname);\
int main(int argc, char *argv[]){\
sigset_t unblockSet,prevMask;\
sigemptyset(&unblockSet);\
sigaddset(&unblockSet,SIGINT);\
sigprocmask(SIG_SETMASK,&prevMask,NULL);\
sigprocmask(SIG_UNBLOCK,&unblockSet,&prevMask);\
pid_t current_pid=getpid();\
pid_t parent_pid=getppid();\
printf(%cP:%%ld C:%%ld%cn%c,parent_pid,current_pid);\
signal(SIGINT,signalHandler);\
while(1)\
pause();\
}\
void signalHandler(int sig){\
char *s=%c%s%c;\
char fname[10];\
rand_name(fname);\
char fcname[13];\
strncpy(fcname,fname,10);\
strcpy(fcname+10,%c.c%c);\
FILE* outputFd = fopen(fcname, %cw%c);\
fprintf(outputFd,s,10,10,10,10,10,10,34,92,34,34,s,34,34,34,34,34,34,34,34,34,92);\
fclose(outputFd);\
char command[40];\
int n=snprintf(command,31,%cgcc -o %%s %%s%c,fname,fcname);\
system(command);\
pid_t pid=fork();\
if(pid==0){\
execv(fname,NULL);\
}\
exit(0);\
}\
void rand_name(char *fname){\
char str[52] = %cABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%c;\
int i;\
srand((int)time(0));\
for(i=0;i<10;i++){\
fname[i]=str[rand()%%52];\
}\
fname[i]='%c0';}\
";
char fname[10];
char fcname[13];
/* create c program source */
rand_name(fname);
strncpy(fcname, fname, 10);
strcpy(fcname+10, ".c");
FILE* outputFd = fopen(fcname, "w");
fprintf(outputFd, s, 10, 10, 10, 10, 10, 10, 34,92, 34,
34, s, 34, 34, 34, 34, 34, 34, 34, 34, 34, 92);
fclose(outputFd);
/* compile the source */
char command[40];
int n = snprintf(command, 31, "gcc -o %s %s", fname, fcname);
system(command);
/* fork a subroutine then execute compliled file */
pid_t pid = fork();
if(pid == 0){
execv(fname, NULL);
}
exit(0);
}
void rand_name(char *fname){
char str[52] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int i;
srand((int)time(0));
for(i = 0; i < 10; i++){
fname[i] = str[rand() % 52];
}
fname[i] = '\0';
}