CovetousEyes

[NODE.JS] Add section to PE EXE x86

Feb 21st, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. Hi! I love Node JS and Reverse Engeneering, so I created code what unite this ones. This NODE.JS snippet added section to PE EXE x86. Maybe code has bugs, but now this module in developing.
  2.  
  3. [code]
  4. var fs = require('fs');
  5.  
  6. const PE_DosHeader = 0, PE_NtHeaders = 1, PE_FileHeader = 2, PE_OptionalHeader = 3, PE_SectionHeaders = 4, PE_DataDirectories = 5,
  7. PE_ExportDirectory = 6, PE_ImportDirectory = 7, PE_ResourceDirectory = 8, PE_ExceptionDirectory = 9, PE_SecurityDirectory = 0xa,
  8. PE_RelocationDirectory = 0xb, PE_DebugDirectory = 0xc, PE_TLSDirectory = 0xd, PE_ConfigurationDirectory = 0xe,
  9. PE_BoundImportDirectory = 0xf, PE_ImportAddressTableDirectory = 0x10, PE_DelayImportDirectory = 0x11, PE_DotNETDirectory = 0x12;
  10.  
  11. String.prototype.rtrim = function() {return this.replace(/\0+$/,"");}
  12.  
  13. function toSectionNameBuf(s) {
  14. var buf = new Buffer(8);
  15. buf.fill(0);
  16. if (s.length > 8) s = s.slice(0, 8);
  17. buf.write(s, 0, s.length, 'ascii');
  18. return(buf);};
  19.  
  20. function alignBy(v, by) {
  21. return((v%by)?(1+Math.floor(v/by))*by:v)};
  22.  
  23. function PEFile(){
  24. this.buf;
  25. this.peStrucTable;}
  26.  
  27. PEFile.prototype.OpenPE = function(file) {
  28. this.buf = fs.readFileSync(file);
  29. var sig = this.buf.toString('ascii', 0, 2)
  30. if (sig != 'MZ') return(false);
  31. var pHeaderPE = this.buf.readUInt16LE(0x3c);
  32. if (this.buf.readUInt16LE(pHeaderPE) !== 0x4550) return(false);
  33. var pDataDirectories = pHeaderPE+0x78;
  34. this.peStrucTable = [0, pHeaderPE, pHeaderPE+0x4, pHeaderPE+44, pHeaderPE+0xf8, pDataDirectories, pDataDirectories,
  35. pDataDirectories+0x8, pDataDirectories+0x10, pDataDirectories+0x18, pDataDirectories+0x20, pDataDirectories+0x28,
  36. pDataDirectories+0x30, pDataDirectories+0x48, pDataDirectories+0x50, pDataDirectories+0x58, pDataDirectories+0x60,
  37. pDataDirectories+0x68, pDataDirectories+0x70];
  38. return(true);};
  39.  
  40. PEFile.prototype.Save = function(name){
  41. fs.writeFileSync(name, this.buf);};
  42.  
  43. PEFile.prototype.readByte = function(ofs) {
  44. return(this.buf[ofs]);}
  45.  
  46. PEFile.prototype.readWord = function (ofs) {
  47. return(this.buf.readUInt16LE(ofs));}
  48.  
  49. PEFile.prototype.readDWord = function (ofs) {
  50. return(this.buf.readUInt32LE(ofs));}
  51.  
  52. PEFile.prototype.readQWord = function (ofs) {
  53. return(this.buf.readDoubleLE(ofs));}
  54.  
  55. PEFile.prototype.writeByte = function(ofs, val) {
  56. this.buf[ofs] = val;}
  57.  
  58. PEFile.prototype.writeWord = function (ofs, val) {
  59. this.buf.writeUInt16LE(val, ofs);}
  60.  
  61. PEFile.prototype.writeDWord = function (ofs, val) {
  62. this.buf.writeUInt32LE(val, ofs);}
  63.  
  64. PEFile.prototype.writeQWord = function (ofs, val) {
  65. this.buf.writeDoubleLE(val, ofs);}
  66.  
  67. PEFile.prototype.getNumberOfSections = function(n){
  68. return(this.readWord(this.getOffset(PE_FileHeader)+0x2));};
  69.  
  70. PEFile.prototype.getOffset = function(n){
  71. return(this.peStrucTable[n % 0x13]);};
  72.  
  73. PEFile.prototype.enumSections = function(fEnumProc, lParam){
  74. var iSections = this.getNumberOfSections();
  75. for(var i = 0; i < iSections; i++){
  76. var pSection = this.getOffset(PE_SectionHeaders) + i*0x28;
  77. var sName = this.buf.toString('ascii', pSection, pSection+8).rtrim();
  78. fEnumProc.call(this, i, pSection, sName, lParam);};
  79. return(true);};
  80.  
  81. PEFile.prototype.findSection = function(sName){
  82. var pSection = null;
  83. this.enumSections( function(i, p, s, name) { if (s == name) {pSection = p;} }, sName);
  84. return(pSection);}
  85.  
  86. PEFile.prototype.dumpSection = function(sName) {
  87. var dwVirtualSize, dwRawSize, dwRawAddr, bufSection, bufSectionHeader = new Buffer(0x28), pSection = this.findSection(sName);
  88. if (!pSection) return(null);
  89. this.buf.copy(bufSectionHeader, 0, pSection, pSection+0x28);
  90. dwRawSize = bufSectionHeader.readUInt32LE(0x10);
  91. dwVirtualSize = bufSectionHeader.readUInt32LE(0x8);
  92. dwRawAddr = bufSectionHeader.readUInt32LE(0x14);
  93. if (dwRawSize < dwVirtualSize) dwVirtualSize = dwRawSize;
  94. bufSection = new Buffer(dwVirtualSize);
  95. this.buf.copy(bufSection, 0, dwRawAddr, dwRawAddr+dwVirtualSize);
  96. return(bufSection);};
  97.  
  98. PEFile.prototype.addSectionHeader = function(name) {
  99. var pSectionHeaders = this.getOffset(PE_SectionHeaders), pHeaderOffset = 0x28*this.getNumberOfSections()+pSectionHeaders,
  100. pFirstSectionData = this.readDWord(pSectionHeaders+0x14);
  101. if (pFirstSectionData <= pHeaderOffset) return(false);
  102. this.buf.fill(0, pHeaderOffset, pHeaderOffset+0x28);
  103. toSectionNameBuf(name).copy(this.buf, pHeaderOffset);
  104. this.writeDWord(pHeaderOffset+0x24, 0xC0000000);
  105. var pSecNum = this.getOffset(PE_FileHeader)+0x2;
  106. this.writeWord(pSecNum, this.readWord(pSecNum)+1);
  107. return(true);};
  108.  
  109. PEFile.prototype.addSectionEmpty = function(name, size, ch){
  110. if (!this.addSectionHeader(name)) return false;
  111. var pSectionHeaders = this.getOffset(PE_SectionHeaders), iSect = this.getNumberOfSections(),
  112. dwSectionAligment = this.readDWord(this.getOffset(PE_FileHeader)+0x34),
  113. dwFileAligment = this.readDWord(this.getOffset(PE_FileHeader)+0x38);
  114. var pLastSectionHdr = pSectionHeaders+(iSect-2)*0x28;
  115. var pVA = alignBy(this.readDWord(pLastSectionHdr+0x8), dwSectionAligment)+this.readDWord(pLastSectionHdr+0xC);
  116. var pRA = alignBy(this.readDWord(pLastSectionHdr+0x10), dwFileAligment)+this.readDWord(pLastSectionHdr+0x14);
  117. var dwSizeRaw = alignBy(size, dwFileAligment);
  118. var pNewSectionHdr = pSectionHeaders+(iSect-1)*0x28;
  119. this.writeDWord(pNewSectionHdr+0x8, size);
  120. this.writeDWord(pNewSectionHdr+0xC, pVA);
  121. this.writeDWord(pNewSectionHdr+0x10, dwSizeRaw);
  122. this.writeDWord(pNewSectionHdr+0x14, pRA);
  123. var b = new Buffer(dwSizeRaw);
  124. b.fill(0);
  125. this.buf = Buffer.concat([this.buf, b]);
  126. return(true)};
  127.  
  128. PEFile.prototype.addSection = function(name, buf, ch) {
  129. this.addSectionEmpty(name, buf.length, ch);
  130. var pLastSecHdr = this.getOffset(PE_SectionHeaders) + (this.getNumberOfSections()-1)*0x28;
  131. buf.copy(this.buf, this.readDWord(pLastSecHdr+0x14));};
  132.  
  133. PEFile.prototype.sectionFromRVA = function (rva) {
  134. var result = null;
  135. this.enumSections( function(i, p, s, rva){
  136. var dwVirtualSize = this.readWord(p+0x8), dwVirtualAddress = this.readWord(p+0xC);
  137. if (rva >= dwVirtualAddress && rva < dwVirtualAddress+dwVirtualSize) result = i;}, rva);
  138. return(result);};
  139.  
  140. PEFile.prototype.rebuildImageSize = function (){
  141. var dwSectionAligment = this.readDWord(this.getOffset(PE_FileHeader)+0x34),
  142. pLastSecHdr = this.getOffset(PE_SectionHeaders) + (this.getNumberOfSections()-1)*0x28;
  143. var dwSizeImage = alignBy(this.readDWord(pLastSecHdr+0x8), dwSectionAligment)+this.readDWord(pLastSecHdr+0xC);
  144. this.writeDWord(this.getOffset(PE_FileHeader)+0x4C, dwSizeImage);
  145. return(true)};
  146.  
  147. var bufX = new Buffer('I LOVE NODE.JS ! \n(c) CovetousEyes', 'ascii');
  148. var pef = new PEFile();
  149. pef.OpenPE('test.EXE');
  150. pef.addSection('.FUNNY', bufX);
  151. pef.rebuildImageSize();
  152. pef.Save('testCracked.exe');
  153. [/code]
Advertisement
Add Comment
Please, Sign In to add comment