File: scprofiler.c

package info (click to toggle)
libemu 0.2.0%2Bgit20120122-1.2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 3,360 kB
  • ctags: 1,872
  • sloc: ansic: 43,547; makefile: 211; python: 14
file content (328 lines) | stat: -rw-r--r-- 7,565 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/********************************************************************************
 *                               libemu
 *
 *                    - x86 shellcode emulation -
 *
 *
 * Copyright (C) 2008  Paul Baecher & Markus Koetter
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * 
 *             contact nepenthesdev@users.sourceforge.net  
 *
 *******************************************************************************/


#include "../config.h"

#define HAVE_GETOPT_H
#ifdef HAVE_GETOPT_H
	#include <getopt.h>
#endif

#ifdef HAVE_STDLIB_H
	#include <stdlib.h>
#endif


#include <stdint.h>

#define HAVE_UNISTD
#ifdef HAVE_UNISTD
	#include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>

#include <arpa/inet.h>

#include "emu/environment/emu_profile.h"
#include "emu/environment/win32/emu_env_w32.h"


struct run_options
{
	char *profile_name;
	char *function_name;
	char *argument_name;
	bool profile;
	bool debug_dump;

};


struct run_options opts;

struct emu_profile_function *find_function(struct emu_profile *profile, const char *fnname)
{
	struct emu_profile_function *function;
	for ( function = emu_profile_functions_first(profile->functions); !emu_profile_functions_istail(function); function = emu_profile_functions_next(function) )
	{
		if ( strcmp(function->fnname,fnname) == 0 )
			return function;
	}
	return NULL;
}

struct emu_profile_argument *find_argument(struct emu_profile_function *function, const char *name)
{
	struct emu_profile_argument *argument;
	for ( argument = emu_profile_arguments_first(function->arguments); !emu_profile_arguments_istail(argument); argument = emu_profile_arguments_next(argument) )
	{
		if ( strcmp(argument->argname,name) == 0 )
			return argument;
	}

	return NULL;
}

typedef void (*print_function)(struct emu_profile_function *function, void *args[]);

struct function_render
{
	const char *function;
	print_function printer;
};


void print_connect(struct emu_profile_function *function, void *args[])
{
//	int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
	int retval = *(int *)args[0];
	int sockfd = *(int *)args[1];
	struct sockaddr_in *addrin = *(struct sockaddr_in **)(void **)args[2];
	socklen_t addrlen = *(socklen_t *)args[3];

	printf("%i = %s(int sockfd=%i, const struct sockaddr *serv_addr=%s:%i, socklen_t addrlen=%i);\n",
		   retval,
		   function->fnname,
		   sockfd,
		   inet_ntoa(addrin->sin_addr), 
		   ntohs(addrin->sin_port),
		   addrlen);

}

void print_CreateProcess(struct emu_profile_function *function, void *args[])
{
	/*BOOL CreateProcess( 
	  LPCWSTR pszImageName, 
	  LPCWSTR pszCmdLine, 
	  LPSECURITY_ATTRIBUTES psaProcess, 
	  LPSECURITY_ATTRIBUTES psaThread, 
	  BOOL fInheritHandles, 
	  DWORD fdwCreate, 
	  LPVOID pvEnvironment, 
	  LPWSTR pszCurDir, 
	  LPSTARTUPINFOW psiStartInfo, 
	  LPPROCESS_INFORMATION pProcInfo
	);*/

	bool retval = *(bool *)args[0];
	char *psCmdLine = *(char **)(void **)args[2];

	printf("%i = %s(\"%s\");\n", 
		   retval,
		   function->fnname,
		   psCmdLine);

}

struct function_render function_rendering[] =
{
	{ "connect", print_connect},
	{ "bind", print_connect},
	{ "CreateProcess", print_CreateProcess}
};


int main(int argc, char *argv[])
{
	int option_index = 0;

	memset(&opts, 0, sizeof(struct run_options));

	while ( 1 )
	{
		int c;

		static struct option long_options[] = {
			{"argument"         , 1, 0, 'a'},
			{"debugdump"        , 1, 0, 'd'},
			{"function"         , 1, 0, 'f'},
			{"profile"          , 0, 0, 'p'},
			{0, 0, 0, 0}
		};

		c = getopt_long (argc, argv, "a:df:p", long_options, &option_index);
		if ( c == -1 )
			break;

		switch ( c )
		{
		

		case 'a':
			opts.argument_name = strdup(optarg);
			printf("argument %s\n", optarg);
			break;

		case 'd':
			opts.debug_dump = true;
			break;

		case 'f':
			opts.function_name = strdup(optarg);
			printf("function %s\n", optarg);
			break;

		case 'p':
			opts.profile = true;
			break;

		default:
			printf ("?? getopt returned character code 0%o ??\n", c);
			break;

		}
	}

	if ( optind < argc )
	{
		opts.profile_name = strdup(argv[optind]);
		printf("Profile %s\n", argv[optind]);
	}

	struct emu_profile *profile = emu_profile_new();
	if ( emu_profile_parse(profile, opts.profile_name) != 0 )
	{
		printf("error parsing file %s!\n",opts.profile_name);
		return -1;
	}



	if ( opts.profile )
	{
//		printf("profiling ....\n");
		enum profile_state
		{
			PS_NONE,
			PS_WSASOCKET,
			PS_BIND,
			PS_LISTEN,
			PS_ACCEPT,
			PS_CONNECT,
			PS_CREATEPROCESS,
			PS_URLDOWNLOADTOFILE
		};

		enum profile_state state = PS_NONE;
		struct emu_profile_function *function;
		for ( function = emu_profile_functions_first(profile->functions); !emu_profile_functions_istail(function); function = emu_profile_functions_next(function) )
		{
//			printf("state %i\n", state);
			switch ( state )
			{
			case PS_NONE:
				if ( strcmp("WSAStartup", function->fnname) == 0 )
					state = PS_WSASOCKET;
				else
					if ( strcmp("URLDownloadToFile", function->fnname) == 0 )
					printf("url download\n");
				break;


			case PS_WSASOCKET:
				if ( strcmp("bind", function->fnname) == 0 )
					state = PS_BIND;
				else
					if ( strcmp("connect", function->fnname) == 0 )
					state = PS_CONNECT;
				break;

			case PS_BIND:
				if ( strcmp("listen", function->fnname) == 0 )
					state = PS_LISTEN;

				break;

			case PS_LISTEN:
				if ( strcmp("accept", function->fnname) == 0 )
					state = PS_ACCEPT;
				break;

			case PS_ACCEPT:
				if ( strcmp("CreateProcess", function->fnname) == 0 )
					printf("bindshell\n");
				else
					if ( strcmp("recv", function->fnname) == 0 )
					printf("bindfiletransfer");
				break;

			case PS_CONNECT:
				if ( strcmp("CreateProcess", function->fnname) == 0 )
					printf("connectbackshell\n");
				else
					if ( strcmp("recv", function->fnname) == 0 )
					printf("connectback transfer");

				break;

			default:
				break;

			}
		}
	}

	if ( opts.debug_dump )
	{
		emu_profile_debug(profile);
	}

	struct emu_profile_function *function;
	for ( function = emu_profile_functions_first(profile->functions); !emu_profile_functions_istail(function); function = emu_profile_functions_next(function) )
	{

		int argc = emu_profile_arguments_length(function->arguments);
		void **args = malloc( (argc+1) * sizeof(void *));

		int i;
		for ( i = 0; i <= argc; i++ )
			args[i] = emu_profile_function_argument_get(function, i);

		for ( i=0;i< sizeof(function_rendering)/sizeof(struct function_render);i++ )
		{
			if ( strcmp(function->fnname, function_rendering[i].function) == 0 )
			{
				function_rendering[i].printer(function, args);
				goto found_function;
			}
		}

		printf("could not find function %s\n",function->fnname);

		found_function:
		for ( i = 0; i < argc+1; i++ )
			free(args[i]);
	}

//	emu_profile_debug(profile);
	emu_profile_free(profile);
	return 0;
}