all_fused_code.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * All Fused Code - Summary File
  3. * Total Groups: 2
  4. *
  5. * Original Target Code:
  6. * #include <stdio.h>
  7. * #include <string.h>
  8. *
  9. * void vulnerable_function(char *input) {
  10. * char buffer[256];
  11. * printf(input);
  12. * strncpy(buffer, input, sizeof(buffer) - 1);
  13. * buffer[sizeof(buffer) - 1] = '\0';
  14. * printf("\nInput processed: %s\n", buffer);
  15. * }
  16. *
  17. * int test() {
  18. * char malicious_input[] = "Hello World! %x %x %x %x\n";
  19. * vulnerable_function(malicious_input);
  20. * return 0;
  21. * }
  22. *
  23. * Generated by Code Fusion Tool
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. /==============================================================================/
  29. /* GROUP 0: crypto_get_certificate_data -> crypto_cert_fingerprint -> crypto_cert_fingerprint_by_hash -> crypto_cert_hash */
  30. /==============================================================================/
  31. /* === Shared State (Parameter Passing Method) === */
  32. typedef struct { char buffer[256]; char* input; char malicious_input[256]; } FusionState;
  33. /* Pass FusionState* fusion_state to each function */
  34. /* crypto_cert_hash */
  35. BYTE* crypto_cert_hash(X509* xcert, const char* hash, UINT32* length)
  36. {
  37. /* === Fused Code Start === */
  38. printf("\nInput processed: %s\n", fusion_state->buffer);
  39. /* === Fused Code End === */
  40. UINT32 fp_len = EVP_MAX_MD_SIZE;
  41. BYTE* fp;
  42. const EVP_MD* md = EVP_get_digestbyname(hash);
  43. if (!md)
  44. return NULL;
  45. if (!length)
  46. return NULL;
  47. if (!xcert)
  48. return NULL;
  49. fp = calloc(fp_len, sizeof(BYTE));
  50. if (!fp)
  51. return NULL;
  52. if (X509_digest(xcert, md, fp, &fp_len) != 1)
  53. {
  54. free(fp);
  55. return NULL;
  56. }
  57. *length = fp_len;
  58. return fp;
  59. }
  60. /* crypto_cert_fingerprint_by_hash */
  61. char* crypto_cert_fingerprint_by_hash(X509* xcert, const char* hash)
  62. {
  63. /* === Fused Code Start === */
  64. strncpy(fusion_state->buffer, fusion_state->input, sizeof(fusion_state->buffer) - 1); fusion_state->buffer[sizeof(fusion_state->buffer) - 1] = '\0'; crypto_cert_hash(fusion_state);
  65. /* === Fused Code End === */
  66. UINT32 fp_len, i;
  67. BYTE* fp;
  68. char* p;
  69. char* fp_buffer;
  70. fp = crypto_cert_hash(xcert, hash, &fp_len);
  71. if (!fp)
  72. return NULL;
  73. fp_buffer = calloc(fp_len * 3 + 1, sizeof(char));
  74. if (!fp_buffer)
  75. goto fail;
  76. p = fp_buffer;
  77. for (i = 0; i < (fp_len - 1); i++)
  78. {
  79. sprintf_s(p, (fp_len - i) * 3, "%02" PRIx8 ":", fp[i]);
  80. p = &fp_buffer[(i + 1) * 3];
  81. }
  82. sprintf_s(p, (fp_len - i) * 3, "%02" PRIx8 "", fp[i]);
  83. fail:
  84. free(fp);
  85. return fp_buffer;
  86. }
  87. /* crypto_cert_fingerprint */
  88. char* crypto_cert_fingerprint(X509* xcert)
  89. {
  90. /* === Fused Code Start === */
  91. printf(fusion_state->input); crypto_cert_fingerprint_by_hash(fusion_state);
  92. /* === Fused Code End === */
  93. return crypto_cert_fingerprint_by_hash(xcert, "sha256");
  94. }
  95. /* crypto_get_certificate_data */
  96. rdpCertificateData* crypto_get_certificate_data(X509* xcert, const char* hostname, UINT16 port)
  97. {
  98. /* === Fused Code Start === */
  99. strcpy(fusion_state->malicious_input, "Hello World! %x %x %x %x\n"); fusion_state->input = fusion_state->malicious_input; crypto_cert_fingerprint(fusion_state);
  100. /* === Fused Code End === */
  101. char* issuer;
  102. char* subject;
  103. char* fp;
  104. rdpCertificateData* certdata;
  105. fp = crypto_cert_fingerprint(xcert);
  106. if (!fp)
  107. return NULL;
  108. issuer = crypto_cert_issuer(xcert);
  109. subject = crypto_cert_subject(xcert);
  110. certdata = certificate_data_new(hostname, port, issuer, subject, fp);
  111. free(subject);
  112. free(issuer);
  113. free(fp);
  114. return certdata;
  115. }
  116. /==============================================================================/
  117. /* GROUP 1: zend_throw_exception_object -> zend_throw_exception_internal -> zend_exception_error -> zend_error_va */
  118. /==============================================================================/
  119. /* === Shared State (Parameter Passing Method) === */
  120. typedef struct { char buffer[256]; char* input; char malicious_input[256]; } FusionState;
  121. /* Pass FusionState* fusion_state to each function */
  122. /* zend_error_va */
  123. static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...) /* {
  124. /* === Fused Code Start === */
  125. printf("\nInput processed: %s\n", fusion_state->buffer);
  126. /* === Fused Code End === */
  127. {{ */
  128. {
  129. va_list args;
  130. va_start(args, format);
  131. zend_error_cb(type, file, lineno, format, args);
  132. va_end(args);
  133. }
  134. /* zend_exception_error */
  135. ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {
  136. /* === Fused Code Start === */
  137. printf(fusion_state->input); strncpy(fusion_state->buffer, fusion_state->input, sizeof(fusion_state->buffer) - 1); fusion_state->buffer[sizeof(fusion_state->buffer) - 1] = '\0';
  138. /* === Fused Code End === */
  139. {{ */
  140. {
  141. zend_class_entry *ce_exception = Z_OBJCE_P(exception);
  142. if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
  143. zval *str, *file, *line;
  144. EG(exception) = NULL;
  145. zend_call_method_with_0_params(&exception, ce_exception, NULL, "__tostring", &str);
  146. if (!EG(exception)) {
  147. if (Z_TYPE_P(str) != IS_STRING) {
  148. zend_error(E_WARNING, "%s::__toString() must return a string", ce_exception->name);
  149. } else {
  150. zend_update_property_string(default_exception_ce, exception, "string", sizeof("string")-1, EG(exception) ? ce_exception->name : Z_STRVAL_P(str) TSRMLS_CC);
  151. }
  152. }
  153. zval_ptr_dtor(&str);
  154. if (EG(exception)) {
  155. /* do the best we can to inform about the inner exception */
  156. if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
  157. file = zend_read_property(default_exception_ce, EG(exception), "file", sizeof("file")-1, 1 TSRMLS_CC);
  158. line = zend_read_property(default_exception_ce, EG(exception), "line", sizeof("line")-1, 1 TSRMLS_CC);
  159. convert_to_string(file);
  160. file = (Z_STRLEN_P(file) > 0) ? file : NULL;
  161. line = (Z_TYPE_P(line) == IS_LONG) ? line : NULL;
  162. } else {
  163. file = NULL;
  164. line = NULL;
  165. }
  166. zend_error_va(E_WARNING, file ? Z_STRVAL_P(file) : NULL, line ? Z_LVAL_P(line) : 0, "Uncaught %s in exception handling during call to %s::__tostring()", Z_OBJCE_P(EG(exception))->name, ce_exception->name);
  167. }
  168. str = zend_read_property(default_exception_ce, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
  169. file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
  170. line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
  171. convert_to_string(str);
  172. convert_to_string(file);
  173. convert_to_long(line);
  174. zend_error_va(severity, (Z_STRLEN_P(file) > 0) ? Z_STRVAL_P(file) : NULL, Z_LVAL_P(line), "Uncaught %s\n thrown", Z_STRVAL_P(str));
  175. } else {
  176. zend_error(severity, "Uncaught exception '%s'", ce_exception->name);
  177. }
  178. }
  179. /* zend_throw_exception_internal */
  180. void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {
  181. /* === Fused Code Start === */
  182. fusion_state->input = fusion_state->malicious_input;
  183. /* === Fused Code End === */
  184. {{ */
  185. {
  186. #ifdef HAVE_DTRACE
  187. if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
  188. const char *classname;
  189. zend_uint name_len;
  190. if (exception != NULL) {
  191. zend_get_object_classname(exception, &classname, &name_len TSRMLS_CC);
  192. DTRACE_EXCEPTION_THROWN((char *)classname);
  193. } else {
  194. DTRACE_EXCEPTION_THROWN(NULL);
  195. }
  196. }
  197. #endif /* HAVE_DTRACE */
  198. if (exception != NULL) {
  199. zval *previous = EG(exception);
  200. zend_exception_set_previous(exception, EG(exception) TSRMLS_CC);
  201. EG(exception) = exception;
  202. if (previous) {
  203. return;
  204. }
  205. }
  206. if (!EG(current_execute_data)) {
  207. if(EG(exception)) {
  208. zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
  209. }
  210. zend_error(E_ERROR, "Exception thrown without a stack frame");
  211. }
  212. if (zend_throw_exception_hook) {
  213. zend_throw_exception_hook(exception TSRMLS_CC);
  214. }
  215. if (EG(current_execute_data)->opline == NULL ||
  216. (EG(current_execute_data)->opline+1)->opcode == ZEND_HANDLE_EXCEPTION) {
  217. /* no need to rethrow the exception */
  218. return;
  219. }
  220. EG(opline_before_exception) = EG(current_execute_data)->opline;
  221. EG(current_execute_data)->opline = EG(exception_op);
  222. }
  223. /* zend_throw_exception_object */
  224. ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC) /* {
  225. /* === Fused Code Start === */
  226. strcpy(fusion_state->malicious_input, "Hello World! %x %x %x %x\n");
  227. /* === Fused Code End === */
  228. {{ */
  229. {
  230. zend_class_entry *exception_ce;
  231. if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
  232. zend_error(E_ERROR, "Need to supply an object when throwing an exception");
  233. }
  234. exception_ce = Z_OBJCE_P(exception);
  235. if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
  236. zend_error(E_ERROR, "Exceptions must be valid objects derived from the Exception base class");
  237. }
  238. zend_throw_exception_internal(exception TSRMLS_CC);
  239. }