Guest User

Untitled

a guest
Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "peread.h"
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. char* fileOpen(char *path);
  8. int fileSave(char*, struct IMAGE_DOS_HEADER*,struct IMAGE_NT_HEADERS*, struct IMAGE_SECTION_HEADER**, char **);
  9. struct IMAGE_SECTION_HEADER* loadSection(struct IMAGE_DOS_HEADER*,struct IMAGE_NT_HEADERS *,char *,int);
  10. int
  11. main(int argc,char **argv){
  12. if(argc != 3){
  13. printf("%s : missing file operand\n",argv[0]);
  14. return -1;
  15. }
  16. int i;
  17. struct IMAGE_DOS_HEADER *d_hdr;
  18. struct IMAGE_NT_HEADERS *nt_hdr;
  19. struct IMAGE_SECTION_HEADER *s_hdr;
  20. char *file_data;
  21. file_data = fileOpen(argv[1]);
  22. if( file_data == NULL){
  23. return 1;
  24. }
  25. d_hdr = (struct IMAGE_DOS_HEADER *) file_data;
  26. nt_hdr = (struct IMAGE_NT_HEADERS *) (file_data + d_hdr->e_lfanew);
  27. s_hdr = (struct IMAGE_SECTION_HEADER *) (file_data+d_hdr->e_lfanew+sizeof(struct IMAGE_NT_HEADERS));
  28. struct IMAGE_SECTION_HEADER *secs[nt_hdr->e_numsec];
  29. char* sections[nt_hdr->e_numsec];
  30. char* pe_hdr;
  31. pe_hdr = malloc(nt_hdr->e_opthdr.e_secalgn);
  32. memcpy(pe_hdr,file_data,1024);
  33. for(i = 0; i < nt_hdr->e_numsec; i++)
  34. {
  35. secs[i] = loadSection(d_hdr,nt_hdr,file_data,i);
  36. printf("SECTION NAME: %s\nVirtual Size: 0x%x\nVirtual Address: 0x%x\nSize Of Raw data: 0x%x\n\n",
  37. secs[i]->s_name,secs[i]->s_virtsize,secs[i]->s_virtaddr,secs[i]->s_rwdtptr);
  38. }
  39. for(i = 0; i < nt_hdr->e_numsec; i++)
  40. {
  41. int section_size = ((secs[i]->s_virtaddr + nt_hdr->e_opthdr.e_secalgn -1)
  42. /nt_hdr->e_opthdr.e_secalgn)*nt_hdr->e_opthdr.e_secalgn ;
  43. // printf("0x%x %x\n",section_size, secs[i]->s_rwdtsize);
  44. sections[i] = malloc(section_size);
  45. memcpy(sections[i],(char *) (file_data+secs[i]->s_rwdtptr),section_size);
  46. printf("Copied 0x%x bytes to %s \n",section_size,secs[i]->s_name);
  47. // int j;
  48. // if (i == 0)
  49. // for (j = 0; j < section_size;){
  50. // printf("%2.2x",(unsigned char)sections[i][j++]);
  51. //
  52. // }
  53. // printf("%.2x\n",sections[i][1]);
  54. }
  55. fileSave(argv[2],d_hdr,nt_hdr,secs,sections);
  56. }
  57.  
  58.  
  59. struct IMAGE_SECTION_HEADER *
  60. loadSection(struct IMAGE_DOS_HEADER *d_hdr,struct IMAGE_NT_HEADERS *nt_hdr,char* importedFile,int secoff){
  61.  
  62. struct IMAGE_SECTION_HEADER *s_hdr;
  63. s_hdr = (struct IMAGE_SECTION_HEADER *) (importedFile + d_hdr->e_lfanew + sizeof(struct IMAGE_NT_HEADERS) +
  64. (sizeof(struct IMAGE_SECTION_HEADER)*secoff) );
  65. // printf("%s\n",s_hdr->s_name);
  66. return s_hdr;
  67.  
  68.  
  69. }
  70.  
  71. int
  72. fileSave(char *path,struct IMAGE_DOS_HEADER *d_hdr, struct IMAGE_NT_HEADERS *nt_hdr,
  73. struct IMAGE_SECTION_HEADER **sec_hdr,char** sections){
  74. struct stat *st;
  75. char *out;
  76. int fd,wf,written,i;
  77. u_int32_t fileSize;
  78. fileSize = sec_hdr[nt_hdr->e_numsec -1]->s_rwdtptr + sec_hdr[nt_hdr->e_numsec -1]->s_rwdtsize;
  79. printf("%x\n",fileSize);
  80. out = malloc (fileSize);
  81. //memset(out,'\0',1000);
  82. memcpy(out,(char *) d_hdr,sizeof(struct IMAGE_DOS_HEADER));
  83. written = sizeof(struct IMAGE_DOS_HEADER);
  84. // memcpy(out+written,((char *)d_hdr)+written,0xb0);
  85. // written += 0xb0;
  86.  
  87. if(written < d_hdr->e_lfanew){
  88. int n = d_hdr->e_lfanew - written;
  89. printf("writing at offset %x\n",n);
  90. memcpy(out+written, ((char *) (d_hdr))+written,n);
  91. written += n;
  92. printf("written : %x\n",written);
  93. }
  94. memcpy(out+written, (char *) nt_hdr, sizeof(struct IMAGE_NT_HEADERS));
  95. written += sizeof(struct IMAGE_NT_HEADERS);
  96. for(i=0; i < nt_hdr->e_numsec; i++){
  97. memcpy(out+written,sec_hdr[i],sizeof(struct IMAGE_SECTION_HEADER));
  98. written += sizeof(struct IMAGE_SECTION_HEADER);
  99.  
  100. }
  101. for(i=0; i< nt_hdr->e_numsec; i++){
  102. int section_size = ((sec_hdr[i]->s_virtsize + nt_hdr->e_opthdr.e_secalgn -1)
  103. /nt_hdr->e_opthdr.e_secalgn)*nt_hdr->e_opthdr.e_secalgn;
  104. memcpy(out+sec_hdr[i]->s_rwdtptr,sections[i],section_size);
  105. printf("writing section %d with size %x\n",i,section_size);
  106.  
  107. }
  108. // *** OPEN FILE *** //
  109. if( (fd = open(path,O_WRONLY | O_CREAT)) < 0){
  110. printf("error: Could not access file\n");
  111. return -1;
  112. }
  113. if( (wf=write(fd,out,fileSize)) < 0){
  114. printf("error: Could not write file\n");
  115. return -1;
  116. }
  117. close(fd);
  118. return 0;
  119. }
  120. char*
  121. fileOpen(char *path){
  122. int fd;
  123. struct stat st;
  124. char *in;
  125. fd = open(path,O_RDONLY);
  126. if(fd < 0){
  127. printf("error: could not read from file\n");
  128. return NULL;
  129. }
  130. if(fstat(fd,&st) < 0){
  131. printf("error: could not access file\n");
  132. return NULL;
  133. }
  134.  
  135. in = malloc(st.st_size+1);
  136. if(in == NULL){
  137. printf("error: could not allocate memory\n");
  138. return NULL;
  139. }
  140. if( read(fd,in,st.st_size) < 0){
  141. printf("error: could not read file\n");
  142. return NULL;
  143. }
  144. in[st.st_size] = 0;
  145. close(fd);
  146. return in;
  147. }
Add Comment
Please, Sign In to add comment