This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeek.c
163 lines (143 loc) · 3.16 KB
/
peek.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
#include <stdio.h>
#include <windows.h>
/*
- ipc telnet with logging and save to file
*/
void chomp(char *s);
int main(int argc, char **argv)
{
if(argc != 2)
{
printf(" [-] usage: %s {process-id}\n",argv[0]);
return 0;
}
char *pipeName = (char *)malloc(1024);
memset(pipeName,0,1024);
sprintf(pipeName,"\\\\.\\pipe\\shackle-%d",atoi(argv[1]));
HANDLE hPipe = INVALID_HANDLE_VALUE;
while(1)
{
hPipe = CreateFile(pipeName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hPipe != INVALID_HANDLE_VALUE)
{
break;
}
if(GetLastError() != ERROR_PIPE_BUSY)
{
printf(" [ERR] could not open pipe, gle=%d\n",GetLastError());
return -1;
}
if(!WaitNamedPipe(pipeName,10000))
{
printf(" [ERR] could not open pipe, timed out\n");
return -1;
}
}
DWORD dwMode = PIPE_READMODE_MESSAGE;
BOOL fSuccess = FALSE;
char *chBuf = (char *)malloc(1024);
DWORD cbWritten = 0, cbRead = 0;
fSuccess = SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL);
if(!fSuccess)
{
printf(" [ERR] could not set pipe to read message mode, gle=%d\n",GetLastError());
return -1;
}
BOOL keepWaiting = TRUE;
while(keepWaiting == TRUE)
{
do{
memset(chBuf,0,1024);
fSuccess = ReadFile(hPipe,chBuf,1024,&cbRead,NULL);
if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
break;
if ( !(strncmp(chBuf,"INITFINISHED",12) == 0 && chBuf[12] == '\0') )
{
// don't print a newline
printf("%s",chBuf);
}
}while(!fSuccess);
if(!fSuccess)
{
printf(" [ERR] read failed, gle=%d\n",GetLastError());
keepWaiting = FALSE;
return -1;
}
// if(strncmp(chBuf,"NEXTCMDREADY",12) == 0 && chBuf[12] == '\0')
if(strncmp(chBuf,"INITFINISHED",12) == 0 && chBuf[12] == '\0')
{
// =)
keepWaiting = FALSE;
}
}
printf(" > ");
// err...
while(1)
{
memset(chBuf,0,1024);
fgets(chBuf,1024,stdin);
chomp(chBuf);
if(strlen(chBuf) == 0)
{
continue;
}
if(chBuf[0] == '.')
{
// local command
if(strcmp(chBuf,".q") == 0 || strcmp(chBuf,".quit") == 0 || strcmp(chBuf,".quit()") == 0)
{
break;
}
}
fSuccess = WriteFile(hPipe,chBuf,strlen(chBuf) + 1,&cbWritten,NULL);
if(!fSuccess)
{
printf(" [ERR] write failed, gle=%d\n",GetLastError());
return -1;
}
BOOL keepWaiting = TRUE;
while(keepWaiting == TRUE)
{
do{
memset(chBuf,0,1024);
fSuccess = ReadFile(hPipe,chBuf,1024,&cbRead,NULL);
if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
break;
if ( !(strncmp(chBuf,"NEXTCMDREADY",12) == 0 && chBuf[12] == '\0') )
{
// don't print a newline
printf("%s",chBuf);
}
}while(!fSuccess);
if(!fSuccess)
{
printf(" [ERR] read failed, gle=%d\n",GetLastError());
keepWaiting = FALSE;
return -1;
}
if(strncmp(chBuf,"NEXTCMDREADY",12) == 0 && chBuf[12] == '\0')
{
// =)
keepWaiting = FALSE;
}
}
printf(" > ");
}
printf(" [INFO] done, cleaning up\n");
free(chBuf);
CloseHandle(hPipe);
return 0;
}
void chomp(char *s)
{
int i = 0;
int stop = strlen (s);
for (i = 0; i < stop; i++)
{
if (!(isprint (s[i])) || s[i] == '\r' || s[i] == '\n')
{
s[i] = 0;
return;
}
}
}