about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--example/extract_pe_ressources.py2
-rw-r--r--example/unpack_upx.py2
-rw-r--r--miasm/tools/emul_lib/libcodenat.c50
-rw-r--r--miasm/tools/emul_lib/libcodenat_interface.c83
-rw-r--r--miasm/tools/emul_lib/libcodenat_tcc.c8
-rw-r--r--miasm/tools/pe_helper.py9
-rw-r--r--miasm/tools/to_c_helper.py22
7 files changed, 105 insertions, 71 deletions
diff --git a/example/extract_pe_ressources.py b/example/extract_pe_ressources.py
index c21f999c..71d8e226 100644
--- a/example/extract_pe_ressources.py
+++ b/example/extract_pe_ressources.py
@@ -15,7 +15,7 @@ def extract_res(res, name_o = "", num = 0, lvl=-1):
         num += 1
 
         if x.name_s:
-            name = name_o[:]+repr(x.name_s)
+            name = name_o[:]+repr(x.name_s.value[::2])
         else:
             name = name_o[:]
 
diff --git a/example/unpack_upx.py b/example/unpack_upx.py
index cf1e2357..e7ea4b77 100644
--- a/example/unpack_upx.py
+++ b/example/unpack_upx.py
@@ -273,7 +273,7 @@ for s in e.SHList:
     st[s.offset] = e.virt[ad1:ad2]

 e.content = str(st)

 

-e.DirRes = pe_init.DirRes(e)

+e.DirRes = pe.DirRes(e)

 #e.DirImport.impdesc = None

 print repr(e.DirImport.impdesc)

 new_dll = runtime_dll.gen_new_lib(e)

diff --git a/miasm/tools/emul_lib/libcodenat.c b/miasm/tools/emul_lib/libcodenat.c
index 2799c4e4..b241debb 100644
--- a/miasm/tools/emul_lib/libcodenat.c
+++ b/miasm/tools/emul_lib/libcodenat.c
@@ -84,7 +84,7 @@ struct memory_page_node * get_memory_page_from_address(uint64_t ad)
 	if ( mpn && (mpn->ad <= ad) && (ad < mpn->ad + mpn->size))
 		return mpn;
 
-	printf("WARNING: address 0x%"PRIX64" is not mapped in virtual memory:\n", ad);
+	fprintf(stderr, "WARNING: address 0x%"PRIX64" is not mapped in virtual memory:\n", ad);
 	dump_memory_page_pool();
 	dump_gpregs();
 	//exit(-1);
@@ -98,7 +98,7 @@ struct memory_page_node * get_memory_page_from_address(uint64_t ad)
 		if ((mpn->ad <= ad) && (ad < mpn->ad + mpn->size))
 			return mpn;
 	}
-	printf("address %"PRIX64" is not mapped in virtual memory \n", ad);
+	fprintf(stderr, "address %"PRIX64" is not mapped in virtual memory \n", ad);
 	dump_memory_page_pool();
 	dump_gpregs();
 	//exit(-1);
@@ -122,7 +122,7 @@ static inline uint64_t memory_page_read(unsigned int my_size, uint64_t ad)
 		return 0;
 
 	if ((mpn->access & PAGE_READ) == 0){
-		printf("access to non readable page!! %"PRIX64"\n", ad);
+		fprintf(stderr, "access to non readable page!! %"PRIX64"\n", ad);
 		vmcpu.vm_exception_flags |= EXCEPT_ACCESS_VIOL;
 		return 0;
 	}
@@ -154,7 +154,7 @@ static inline uint64_t memory_page_read(unsigned int my_size, uint64_t ad)
 	/* read is multiple page wide */
 	else{
 		unsigned int new_size = my_size;
-		printf("read multiple page! %"PRIX64" %X\n", ad, new_size);
+		fprintf(stderr, "read multiple page! %"PRIX64" %X\n", ad, new_size);
 		dump_memory_page_pool();
 		while (new_size){
 			ret <<=8;
@@ -198,7 +198,7 @@ static inline void memory_page_write(unsigned int my_size,
 		return;
 
 	if ((mpn->access & PAGE_WRITE) == 0){
-		printf("access to non writable page!! %"PRIX64"\n", ad);
+		fprintf(stderr, "access to non writable page!! %"PRIX64"\n", ad);
 		vmcpu.vm_exception_flags |= EXCEPT_ACCESS_VIOL;
 		return ;
 	}
@@ -230,7 +230,7 @@ static inline void memory_page_write(unsigned int my_size,
 	}
 	/* write is multiple page wide */
 	else{
-		printf("write multiple page! %"PRIX64" %X\n", ad, my_size);
+		fprintf(stderr, "write multiple page! %"PRIX64" %X\n", ad, my_size);
 		dump_memory_page_pool();
 		switch(my_size){
 
@@ -278,9 +278,9 @@ inline void check_write_code_bloc(unsigned int my_size, uint64_t addr)
 		LIST_FOREACH(cbp, &code_bloc_pool, next){
 			if ((cbp->ad_start <= addr + my_size/8) &&
 			    (addr < cbp->ad_stop)){
-				printf("self modifying code %"PRIX64" %.8X",
+				fprintf(stderr, "self modifying code %"PRIX64" %.8X",
 				       addr, my_size);
-				printf(" from approx %X\n", vmcpu.eip);
+				fprintf(stderr, " from approx %X\n", vmcpu.eip);
 				vmcpu.vm_exception_flags |= EXCEPT_CODE_AUTOMOD;
 				break;
 			}
@@ -302,9 +302,9 @@ void MEM_WRITE(unsigned int my_size, uint64_t addr, unsigned int src)
 		LIST_FOREACH(cbp, &code_bloc_pool, next){
 			if ((cbp->ad_start <= addr + my_size/8) &&
 			    (addr < cbp->ad_stop)){
-				printf("self modifying code %"PRIX64" %.8X",
+				fprintf(stderr, "self modifying code %"PRIX64" %.8X",
 				       addr, my_size);
-				printf(" from approx %X\n", vmcpu.eip);
+				fprintf(stderr, " from approx %X\n", vmcpu.eip);
 				vmcpu.vm_exception_flags |= EXCEPT_CODE_AUTOMOD;
 				break;
 			}
@@ -409,7 +409,7 @@ int shift_right_arith(unsigned int size, int a, unsigned int b)
 		    i32_a = a;
 		    return (i32_a >> b)&0xffffffff;
 	    default:
-		    printf("inv size in shift %d\n", size);
+		    fprintf(stderr, "inv size in shift %d\n", size);
 		    exit(0);
     }
 }
@@ -452,7 +452,7 @@ unsigned int shift_right_logic(unsigned int size,
 		    u32_a = a;
 		    return (u32_a >> b)&0xffffffff;
 	    default:
-		    printf("inv size in shift %d\n", size);
+		    fprintf(stderr, "inv size in shift %d\n", size);
 		    exit(0);
     }
 }
@@ -488,7 +488,7 @@ int shift_left_logic(unsigned int size, unsigned int a, unsigned int b)
 	    case 32:
 		    return (a<<b)&0xffffffff;
 	    default:
-		    printf("inv size in shift %d\n", size);
+		    fprintf(stderr, "inv size in shift %d\n", size);
 		    exit(0);
     }
 }
@@ -517,7 +517,7 @@ unsigned int mul_lo_op(unsigned int size, unsigned int a, unsigned int b)
 		case 8: mask = 0xff; break;
 		case 16: mask = 0xffff; break;
 		case 32: mask = 0xffffffff; break;
-		default: printf("inv size in mul %d\n", size); exit(0);
+		default: fprintf(stderr, "inv size in mul %d\n", size); exit(0);
 	}
 
 	a &= mask;
@@ -534,7 +534,7 @@ unsigned int mul_hi_op(unsigned int size, unsigned int a, unsigned int b)
 		case 8: mask = 0xff; break;
 		case 16: mask = 0xffff; break;
 		case 32: mask = 0xffffffff; break;
-		default: printf("inv size in mul %d\n", size); exit(0);
+		default: fprintf(stderr, "inv size in mul %d\n", size); exit(0);
 	}
 
 	a &= mask;
@@ -628,7 +628,7 @@ int rot_left(unsigned int size, unsigned int a, unsigned int b)
 		    tmp = (a << b) | ((a&0xFFFFFFFF) >> (size-b));
 		    return tmp&0xffffffff;
 	    default:
-		    printf("inv size in rotleft %d\n", size);
+		    fprintf(stderr, "inv size in rotleft %d\n", size);
 		    exit(0);
     }
 }
@@ -650,7 +650,7 @@ int rot_right(unsigned int size, unsigned int a, unsigned int b)
 		    tmp = ((a&0xFFFFFFFF) >> b) | (a << (size-b));
 		    return tmp&0xffffffff;
 	    default:
-		    printf("inv size in rotleft %d\n", size);
+		    fprintf(stderr, "inv size in rotleft %d\n", size);
 		    exit(0);
     }
 }
@@ -677,7 +677,7 @@ int rcl_rez_op(unsigned int size, unsigned int a, unsigned int b, unsigned int c
 		    tmp = (tmp << b) | ((tmp&0x1FFFFFFFFULL) >> (size-b));
 		    return tmp&0xffffffff;
 	    default:
-		    printf("inv size in rclleft %d\n", size);
+		    fprintf(stderr, "inv size in rclleft %d\n", size);
 		    exit(0);
     }
 }
@@ -710,7 +710,7 @@ int rcl_cf_op(unsigned int size, unsigned int a, unsigned int b, unsigned int cf
 		    tmp = (tmp << b) | ((tmp&0x1FFFFFFFFULL) >> (size-b));
 		    return (tmp>>32)&1;
 	    default:
-		    printf("inv size in rclleft %d\n", size);
+		    fprintf(stderr, "inv size in rclleft %d\n", size);
 		    exit(0);
     }
 }
@@ -758,7 +758,7 @@ unsigned int my_imul08(unsigned int a, unsigned int b)
 unsigned int cpuid(unsigned int a, unsigned int reg_num)
 {
 	if (reg_num >3){
-		printf("zarb cpuid reg %x\n", reg_num);
+		fprintf(stderr, "not implemented cpuid reg %x\n", reg_num);
 		exit(-1);
 	}
 
@@ -788,7 +788,7 @@ unsigned int cpuid(unsigned int a, unsigned int reg_num)
 		}
 	}
 	else{
-		printf("WARNING zarb cpuid index %X!\n", a);
+		fprintf(stderr, "WARNING not implemented cpuid index %X!\n", a);
 		//exit(-1);
 	}
 	return 0;
@@ -892,12 +892,12 @@ struct memory_page_node * create_memory_page_node(uint64_t ad, unsigned int size
 
 	mpn = malloc(sizeof(*mpn));
 	if (!mpn){
-		printf("cannot alloc mpn\n");
+		fprintf(stderr, "cannot alloc mpn\n");
 		exit(-1);
 	}
 	p = malloc(size);
 	if (!p){
-		printf("cannot alloc %d\n", size);
+		fprintf(stderr, "cannot alloc %d\n", size);
 		exit(-1);
 	}
 	mpn->ad = ad;
@@ -915,7 +915,7 @@ struct code_bloc_node * create_code_bloc_node(uint64_t ad_start, uint64_t ad_sto
 
 	cbp = malloc(sizeof(*cbp));
 	if (!cbp){
-		printf("cannot alloc cbp\n");
+		fprintf(stderr, "cannot alloc cbp\n");
 		exit(-1);
 	}
 
@@ -1002,7 +1002,7 @@ void insert_mpn_in_tab(struct memory_page_node* mpn_a)
 	     i<(mpn_a->ad + mpn_a->size + PAGE_SIZE - 1)>>MEMORY_PAGE_POOL_MASK_BIT;
 	     i++){
 		if (memory_page_pool_tab[i] !=NULL){
-			printf("known page in tab\n");
+			fprintf(stderr, "known page in tab\n");
 			exit(1);
 		}
 		memory_page_pool_tab[i] = mpn_a;
diff --git a/miasm/tools/emul_lib/libcodenat_interface.c b/miasm/tools/emul_lib/libcodenat_interface.c
index 56284aa2..6bb04fb1 100644
--- a/miasm/tools/emul_lib/libcodenat_interface.c
+++ b/miasm/tools/emul_lib/libcodenat_interface.c
@@ -112,6 +112,31 @@ PyObject* _vm_get_gpreg(void)
     PyDict_SetItemString(dict, "eip", o);
     Py_DECREF(o);
 
+
+    o = PyInt_FromLong((long)vmcpu.zf);
+    PyDict_SetItemString(dict, "zf", o);
+    Py_DECREF(o);
+    o = PyInt_FromLong((long)vmcpu.nf);
+    PyDict_SetItemString(dict, "nf", o);
+    Py_DECREF(o);
+    o = PyInt_FromLong((long)vmcpu.pf);
+    PyDict_SetItemString(dict, "pf", o);
+    Py_DECREF(o);
+    o = PyInt_FromLong((long)vmcpu.of);
+    PyDict_SetItemString(dict, "of", o);
+    Py_DECREF(o);
+    o = PyInt_FromLong((long)vmcpu.cf);
+    PyDict_SetItemString(dict, "cf", o);
+    Py_DECREF(o);
+    o = PyInt_FromLong((long)vmcpu.af);
+    PyDict_SetItemString(dict, "af", o);
+    Py_DECREF(o);
+    o = PyInt_FromLong((long)vmcpu.df);
+    PyDict_SetItemString(dict, "df", o);
+    Py_DECREF(o);
+
+
+
     return dict;
 }
 
@@ -124,6 +149,16 @@ reg_dict gpreg_dict[] = { {.name = "eax", .ptr = &(vmcpu.eax)},
 			  {.name = "esp", .ptr = &(vmcpu.esp)},
 			  {.name = "ebp", .ptr = &(vmcpu.ebp)},
 			  {.name = "eip", .ptr = &(vmcpu.eip)},
+
+			  {.name = "zf", .ptr = &(vmcpu.zf)},
+			  {.name = "nf", .ptr = &(vmcpu.nf)},
+			  {.name = "pf", .ptr = &(vmcpu.pf)},
+			  {.name = "of", .ptr = &(vmcpu.of)},
+			  {.name = "cf", .ptr = &(vmcpu.cf)},
+			  {.name = "af", .ptr = &(vmcpu.af)},
+			  {.name = "df", .ptr = &(vmcpu.df)},
+
+
 };
 
 PyObject* _vm_set_gpreg(PyObject *dict)
@@ -160,7 +195,7 @@ PyObject* _vm_set_gpreg(PyObject *dict)
 
 	    if (found)
 		    continue;
-	    printf("unkown key: %s\n", PyString_AsString(d_key));
+	    fprintf(stderr, "unkown key: %s\n", PyString_AsString(d_key));
 	    RAISE(PyExc_ValueError, "unkown reg");
     }
     return NULL;
@@ -220,39 +255,39 @@ PyObject* _call_pyfunc_from_globals(char* funcname)
 {
     PyObject  *mod,  *func, *rslt, *globals, *func_globals;
 
-    printf("getting pyfunc %s\n", funcname);
+    fprintf(stderr, "getting pyfunc %s\n", funcname);
     mod = PyEval_GetBuiltins();
 
     if (!mod) {
-	    printf("cannot find module\n");
+	    fprintf(stderr, "cannot find module\n");
 	    exit(0);
     }
 
     func_globals = PyDict_GetItemString(mod, "globals");
     if (!func_globals) {
-	    printf("cannot find function globals\n");
+	    fprintf(stderr, "cannot find function globals\n");
 	    exit(0);
     }
 
     if (!PyCallable_Check (func_globals)) {
-	    printf("function not callable\n");
+	    fprintf(stderr, "function not callable\n");
 	    exit(0);
     }
 
     globals = PyObject_CallObject (func_globals, NULL);
     if (!globals) {
-	    printf("cannot get globals\n");
+	    fprintf(stderr, "cannot get globals\n");
 	    exit(0);
     }
 
     func = PyDict_GetItemString (globals, funcname);
     if (!func) {
-	    printf("cannot find function %s\n", funcname);
+	    fprintf(stderr, "cannot find function %s\n", funcname);
 	    exit(0);
     }
 
     if (!PyCallable_Check (func)) {
-	    printf("function not callable\n");
+	    fprintf(stderr, "function not callable\n");
 	    exit(0);
     }
 
@@ -267,39 +302,39 @@ PyObject* _call_pyfunc_from_eip(void)
     PyObject  *mod,  *func, *rslt, *globals, *func_globals;
     char funcname[0x100];
 
-    printf("getting pybloc %X\n", vmcpu.eip);
+    fprintf(stderr, "getting pybloc %X\n", vmcpu.eip);
     sprintf(funcname, "bloc_%.8X", vmcpu.eip);
-    printf("bloc name %s\n", funcname);
+    fprintf(stderr, "bloc name %s\n", funcname);
 
     mod = PyEval_GetBuiltins();
 
     if (!mod) {
-	    printf("cannot find module\n");
+	    fprintf(stderr, "cannot find module\n");
 	    exit(0);
     }
     func_globals = PyDict_GetItemString(mod, "globals");
     if (!func_globals) {
-	    printf("cannot find function globals\n");
+	    fprintf(stderr, "cannot find function globals\n");
 	    exit(0);
     }
     if (!PyCallable_Check (func_globals)) {
-	    printf("function not callable\n");
+	    fprintf(stderr, "function not callable\n");
 	    exit(0);
     }
     globals = PyObject_CallObject (func_globals, NULL);
     if (!globals) {
-	    printf("cannot get globals\n");
+	    fprintf(stderr, "cannot get globals\n");
 	    exit(0);
     }
 
 
     func = PyDict_GetItemString (globals, funcname);
     if (!func) {
-	    printf("cannot find function %s\n", funcname);
+	    fprintf(stderr, "cannot find function %s\n", funcname);
 	    exit(0);
     }
     if (!PyCallable_Check (func)) {
-	    printf("function not callable\n");
+	    fprintf(stderr, "function not callable\n");
 	    exit(0);
     }
     rslt = PyObject_CallObject (func, NULL);
@@ -379,8 +414,6 @@ PyObject* _vm_set_mem(PyObject *addr, PyObject *item_str)
     buf_size = PyString_Size(item_str);
     PyString_AsStringAndSize(item_str, &buf_data, &length);
 
-    printf("set addr: %X (len %X)\n", val, (unsigned int)length);
-
     mpn = get_memory_page_from_address(val);
     memcpy(mpn->ad_hp + (val-mpn->ad), buf_data, buf_size);
 
@@ -457,7 +490,7 @@ PyObject* _vm_get_str(PyObject *addr, PyObject *item_len)
     my_size = buf_len;
     buf_out = malloc(buf_len);
     if (!buf_out){
-	    printf("cannot alloc read\n");
+	    fprintf(stderr, "cannot alloc read\n");
 	    exit(-1);
     }
 
@@ -815,18 +848,18 @@ PyObject* _vm_exec_blocs(PyObject* self, PyObject* args)
 
 		module = PyObject_GetAttrString(b, "module_c");
 		if (module == NULL){
-			printf("assert eip module_c in pyobject\n");
+			fprintf(stderr, "assert eip module_c in pyobject\n");
 			exit(0);
 		}
 		func = PyObject_GetAttrString(module, "func");
 		if (func == NULL){
-			printf("assert func module_c in pyobject\n");
+			fprintf(stderr, "assert func module_c in pyobject\n");
 			exit(0);
 		}
 
 		Py_DECREF(module);
 		if (!PyCallable_Check (func)) {
-			printf("function not callable\n");
+			fprintf(stderr, "function not callable\n");
 			exit(0);
 		}
 		Py_DECREF(meip);
@@ -835,7 +868,7 @@ PyObject* _vm_exec_blocs(PyObject* self, PyObject* args)
 		Py_DECREF(func);
 		e = PyErr_Occurred ();
 		if (e){
-			printf("exception\n");
+			fprintf(stderr, "exception\n");
 			return meip;
 		}
 
@@ -893,7 +926,7 @@ PyObject* vm_exec_bloc(PyObject* self, PyObject* args)
 		return meip;
 	Py_DECREF(module);
 	if (!PyCallable_Check (func)) {
-		printf("function not callable\n");
+		fprintf(stderr, "function not callable\n");
 		exit(0);
 	}
 	Py_DECREF(meip);
@@ -902,7 +935,7 @@ PyObject* vm_exec_bloc(PyObject* self, PyObject* args)
 	Py_DECREF(func);
 	e = PyErr_Occurred ();
 	if (e){
-		printf("exception\n");
+		fprintf(stderr, "exception\n");
 		return meip;
 	}
 
diff --git a/miasm/tools/emul_lib/libcodenat_tcc.c b/miasm/tools/emul_lib/libcodenat_tcc.c
index 371db4a8..3eda63c6 100644
--- a/miasm/tools/emul_lib/libcodenat_tcc.c
+++ b/miasm/tools/emul_lib/libcodenat_tcc.c
@@ -84,8 +84,8 @@ PyObject* tcc_compil(PyObject* self, PyObject* args)
 
 	tcc_init_state();
 	if (tcc_compile_string(tcc_state, func_code) != 0) {
-		printf("Erreur de compilation !\n");
-		printf("%s\n", func_code);
+		fprintf(stderr, "Erreur de compilation !\n");
+		fprintf(stderr, "%s\n", func_code);
 		exit(0);
 	}
 	/* XXX use tinycc devel with -fPIC patch in makefile */
@@ -93,8 +93,8 @@ PyObject* tcc_compil(PyObject* self, PyObject* args)
 		exit(0);
 	entry = tcc_get_symbol(tcc_state, func_name);
 	if (!entry){
-		printf("Erreur de symbole !\n");
-		printf("%s\n", func_name);
+		fprintf(stderr, "Erreur de symbole !\n");
+		fprintf(stderr, "%s\n", func_name);
 		exit(0);
 	}
 
diff --git a/miasm/tools/pe_helper.py b/miasm/tools/pe_helper.py
index 50428ca8..a63c2b99 100644
--- a/miasm/tools/pe_helper.py
+++ b/miasm/tools/pe_helper.py
@@ -16,6 +16,7 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 from elfesteem import *
+from elfesteem import pe
 from elfesteem import cstruct
 
 from miasm.arch.ia32_arch import *
@@ -27,7 +28,7 @@ import miasm.core.bin_stream
 import os
 import re
 from  miasm.tools import to_c_helper
-import miasm.core.bin_stream
+from miasm.core import bin_stream
 pe_cache = {}
 def pe_from_name(n):
     global pe_cache
@@ -81,7 +82,7 @@ def is_rva_in_code_section(e, rva):
 def guess_func_destack_dis(e, ad):
     job_done = set()
     symbol_pool = asmbloc.asm_symbol_pool()
-    in_str = bin_stream.bin_stream(e.virt)
+    in_str = bin_stream(e.virt)
     
     all_bloc = asmbloc.dis_bloc_all(x86_mn, in_str, ad, job_done, symbol_pool, follow_call = False, patch_instr_symb = False)
     return guess_func_destack(all_bloc)
@@ -143,7 +144,7 @@ def get_import_address(e):
         
         libname = s.dlldescname.name.lower()
         for ii, imp in enumerate(s.impbynames):
-            if isinstance(imp, pe_init.ImportByName):
+            if isinstance(imp, pe.ImportByName):
                 funcname = imp.name
             else:
                 funcname = imp
@@ -577,7 +578,7 @@ def preload_lib(e, runtime_lib, patch_vm_imp = True):
         libname_s = canon_libname_libfunc(libname, libfunc)
         dyn_funcs[libname_s] = ad_libfunc
         if patch_vm_imp:
-            to_c_helper.vm_set_mem(ad, struct.pack(cstruct.size2type[e.wsize], ad_libfunc))
+            to_c_helper.vm_set_mem(ad, struct.pack(cstruct.size2type[e._wsize], ad_libfunc))
         
     return dyn_funcs
 
diff --git a/miasm/tools/to_c_helper.py b/miasm/tools/to_c_helper.py
index df579749..de665aad 100644
--- a/miasm/tools/to_c_helper.py
+++ b/miasm/tools/to_c_helper.py
@@ -289,7 +289,7 @@ def bloc2C(all_bloc, addr2label = None, gen_exception_code = False, dbg_instr =
             if (not filtered_ad) or b.label.offset in filtered_ad:
                 if tick_dbg!=None:
                     out.append('if (my_tick > %d)'%tick_dbg)
-                out.append(r'printf("%s\n");'%str(b.label.name))
+                out.append(r'fprintf(stderr, "%s\n");'%str(b.label.name))
         
         
         for l in b.lines:
@@ -320,7 +320,7 @@ def bloc2C(all_bloc, addr2label = None, gen_exception_code = False, dbg_instr =
                 my_o.append(code_deal_exception_post_instr % (patch_c_id(eip), (l.offset&mask_int), (l.offset&mask_int)))
 
 
-                #my_o.append(r'printf("ecx %.8X\n", ecx );')            
+                #my_o.append(r'fprintf(stderr, "ecx %.8X\n", ecx );')            
                 my_o+= ['if (%s==0) break;'%patch_c_id(ecx)]
                 my_o+=o
                 my_o+= ['%s--;'%patch_c_id(ecx)]
@@ -346,7 +346,7 @@ def bloc2C(all_bloc, addr2label = None, gen_exception_code = False, dbg_instr =
             if dbg_instr and ((not filtered_ad) or l.offset in filtered_ad):
                 if tick_dbg!=None:
                     out.append('if (vmcpu.my_tick > %d)'%tick_dbg)
-                out.append(r'printf("%s\n");'%str(l))
+                out.append(r'fprintf(stderr, "%s\n");'%str(l))
             else:
                 out.append(r'//%s'%str(l))
 
@@ -648,7 +648,7 @@ def gen_dynamic_dispatcher(dispatch_table):
     out2 += ['\t\t}']
     out2 += ['\ti++;']
     out2 += ['\t}']
-    out2 += [r'printf("Unkown destination! 0x%.8X\n", vmcpu.eip);']
+    out2 += [r'fprintf(stderr, "Unkown destination! 0x%.8X\n", vmcpu.eip);']
     out2 += [r'vmcpu.vm_exception_flags |= EXCEPT_UNK_EIP;']
     #out2 += [r'exit(0);']
     out2 += ['return labelref;']
@@ -660,7 +660,7 @@ def gen_dynamic_dispatcher(dispatch_table):
     out += ["#define GOTO_DYNAMIC"]
     out += ["labelref = get_label_from_eip();"]
     out += ["if (labelref == NULL) {"]
-    out += [r'printf("Unkown destination! 0x%.8X\n", vmcpu.eip);']
+    out += [r'fprintf(stderr, "Unkown destination! 0x%.8X\n", vmcpu.eip);']
     out += [r'vmcpu.vm_exception_flags |= EXCEPT_UNK_EIP;']
     out += ["return (PyObject*)vm_get_exception(vm_exception_flags);"]
     out += ['}']
@@ -668,7 +668,7 @@ def gen_dynamic_dispatcher(dispatch_table):
     
     """
     out += ['{']
-    #out += [r'printf("search dst: %X\n", eip);']
+    #out += [r'fprintf(stderr, "search dst: %X\n", eip);']
     
     out += ['switch(eip){']
     for o in offsets:
@@ -677,10 +677,10 @@ def gen_dynamic_dispatcher(dispatch_table):
         out+=['break;']
     
     out += ['case 0x1337beef:']
-    out += [r'printf("return reached %X\n", eip);']
+    out += [r'fprintf(stderr, "return reached %X\n", eip);']
     out += ['return NULL;']
     out += ['default:']
-    out += [r'printf("Unkown destination! 0x%.8X\n", eip);']
+    out += [r'fprintf(stderr, "Unkown destination! 0x%.8X\n", eip);']
     out += [r'vm_exception_flags |= EXCEPT_UNK_EIP;']
     out += ["return (PyObject*)vm_get_exception(vm_exception_flags);"]
     out += ['break;']
@@ -716,13 +716,13 @@ void func_dyn_manager(void)
     
     for (i=0;i<DYN_FUNC_NUM;i++){
         if (dyn_dst == tab_func[i][0]){
-            printf("i %d v@%X r@%X\n", i, tab_func[i][0], tab_func[i][1]);
+            fprintf(stderr, "i %d v@%X r@%X\n", i, tab_func[i][0], tab_func[i][1]);
             tab_func[i][1]();
             return;
         }
     }
     
-    printf("unknown dyn dst!\n");
+    fprintf(stderr, "unknown dyn dst!\n");
     exit(0);
 }
 */
@@ -776,7 +776,7 @@ def gen_known_mems_code(known_mems):
         #code.append("tab_%.8X = malloc(0x%.8X);\n"%(m_ad, len(m_val)))
         code.append("ret = posix_memalign(&tab_%.8X, 0x10000, 0x%.8X);"%(m_ad, len(m_val)))
         code.append("if (ret){")
-        code.append(r'    printf("cannot alloc");')
+        code.append(r'    fprintf(stderr, "cannot alloc");')
         code.append(r'    exit(-1);')
         code.append(r'}')