PEUtil.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SoftEther VPN Source Code
  2. // Build Utility
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2014 Daiyuu Nobori.
  7. // Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2014 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Comments: Tetsuo Sugiyama, Ph.D.
  16. //
  17. //
  18. // This program is free software; you can redistribute it and/or
  19. // modify it under the terms of the GNU General Public License
  20. // version 2 as published by the Free Software Foundation.
  21. //
  22. // This program is distributed in the hope that it will be useful,
  23. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. // GNU General Public License for more details.
  26. //
  27. // You should have received a copy of the GNU General Public License version 2
  28. // along with this program; if not, write to the Free Software
  29. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. //
  31. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  34. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  35. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  36. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  37. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. //
  39. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  40. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  41. //
  42. //
  43. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  44. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  45. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  46. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  47. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  48. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  49. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  50. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  51. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  52. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  53. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  54. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  55. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  56. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  57. //
  58. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  59. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  60. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  61. // COUNTRIES MIGHT BE RESTRICTED.
  62. //
  63. //
  64. // SOURCE CODE CONTRIBUTION
  65. // ------------------------
  66. //
  67. // Your contribution to SoftEther VPN Project is much appreciated.
  68. // Please send patches to us through GitHub.
  69. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  70. // http://www.softether.org/5-download/src/9.patch
  71. //
  72. //
  73. // DEAR SECURITY EXPERTS
  74. // ---------------------
  75. //
  76. // If you find a bug or a security vulnerability please kindly inform us
  77. // about the problem immediately so that we can fix the security problem
  78. // to protect a lot of users around the world as soon as possible.
  79. //
  80. // Our e-mail address for security reports is:
  81. // softether-vpn-security [at] softether.org
  82. //
  83. // Please note that the above e-mail address is not a technical support
  84. // inquiry address. If you need technical assistance, please visit
  85. // http://www.softether.org/ and ask your question on the users forum.
  86. //
  87. // Thank you for your cooperation.
  88. using System;
  89. using System.Threading;
  90. using System.Text;
  91. using System.Configuration;
  92. using System.Collections;
  93. using System.Collections.Generic;
  94. using System.Collections.Specialized;
  95. using System.Security.Cryptography;
  96. using System.Web;
  97. using System.Web.Security;
  98. using System.Web.UI;
  99. using System.Web.UI.WebControls;
  100. using System.Web.UI.WebControls.WebParts;
  101. using System.Web.UI.HtmlControls;
  102. using System.IO;
  103. using System.Drawing;
  104. using System.Drawing.Imaging;
  105. using System.Drawing.Drawing2D;
  106. using System.Diagnostics;
  107. using System.Net;
  108. using System.Net.Security;
  109. using System.Security.Cryptography.X509Certificates;
  110. using CoreUtil;
  111. namespace BuildUtil
  112. {
  113. public static class PEUtil
  114. {
  115. public const int NumRetries = 5;
  116. public const int RetryIntervals = 200;
  117. public const string MutexName = "peutil_setmanifest_mutex";
  118. // Set the version of the PE header to 4 (to work in Windows 98, etc.)
  119. public static void SetPEVersionTo4(byte[] srcData)
  120. {
  121. int offset = 0x140 + (int)((uint)srcData[0x3c] + ((uint)srcData[0x3d] * 256)) - 0xf8;
  122. if (!((srcData[offset] == 0x04 || srcData[offset] == 0x05) && srcData[offset + 1] == 0x00))
  123. {
  124. throw new ApplicationException("The specified file is not PE file.");
  125. }
  126. srcData[offset] = 0x04;
  127. }
  128. public static void SetPEVersionTo4(string fileName)
  129. {
  130. FileInfo fi = new FileInfo(fileName);
  131. byte[] data = File.ReadAllBytes(fileName);
  132. SetPEVersionTo4(data);
  133. int i;
  134. for (i = 0;; i++)
  135. {
  136. try
  137. {
  138. File.WriteAllBytes(fileName, data);
  139. break;
  140. }
  141. catch (Exception ex)
  142. {
  143. if (i >= (NumRetries - 1))
  144. {
  145. throw ex;
  146. }
  147. Kernel.SleepThread(RetryIntervals);
  148. }
  149. }
  150. File.SetCreationTime(fileName, fi.CreationTime);
  151. File.SetLastAccessTime(fileName, fi.LastAccessTime);
  152. File.SetLastWriteTime(fileName, fi.LastWriteTime);
  153. }
  154. public static void SetManifest(string exe, string manifestName)
  155. {
  156. Mutex x = new Mutex(false, MutexName);
  157. x.WaitOne();
  158. try
  159. {
  160. // Manifest file name
  161. string filename = Path.Combine(Paths.ManifestsDir, manifestName);
  162. if (File.Exists(filename) == false)
  163. {
  164. throw new FileNotFoundException(filename);
  165. }
  166. FileInfo fi = new FileInfo(exe);
  167. // Copy exe file to a temporary directory
  168. string exeTmp = IO.CreateTempFileNameByExt(".exe");
  169. IO.FileCopy(exe, exeTmp);
  170. // Create a batch file
  171. string batFileName = Path.Combine(Paths.TmpDirName, "exec_mt.cmd");
  172. StreamWriter bat = new StreamWriter(batFileName, false, Str.ShiftJisEncoding);
  173. bat.WriteLine("call \"{0}\"", Paths.VisualStudioVCBatchFileName);
  174. bat.WriteLine("echo on");
  175. bat.WriteLine("mt.exe -manifest \"{0}\" -outputresource:\"{1}\";1", filename, exeTmp);
  176. bat.WriteLine("EXIT /B %ERRORLEVEL%");
  177. bat.Close();
  178. Exception ex = null;
  179. int i;
  180. // Repeated 20 times in order to avoid locking the file by the anti-virus software
  181. for (i = 0; i < 20; i++)
  182. {
  183. try
  184. {
  185. // Execute
  186. Win32BuildUtil.ExecCommand(Paths.CmdFileName, string.Format("/C \"{0}\"", batFileName), true);
  187. ex = null;
  188. break;
  189. }
  190. catch (Exception ex2)
  191. {
  192. ex = ex2;
  193. }
  194. ThreadObj.Sleep(Secure.Rand31i() % 50);
  195. }
  196. if (ex != null)
  197. {
  198. throw new ApplicationException("mt.exe Manifest Processing for '" + exe + "' Failed.");
  199. }
  200. // Revert to the original file
  201. IO.FileCopy(exeTmp, exe);
  202. // Restore the date and time
  203. File.SetCreationTime(exe, fi.CreationTime);
  204. File.SetLastAccessTime(exe, fi.LastAccessTime);
  205. File.SetLastWriteTime(exe, fi.LastWriteTime);
  206. }
  207. finally
  208. {
  209. x.ReleaseMutex();
  210. }
  211. }
  212. }
  213. }
  214. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  215. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  216. // Join us: http://www.tsukuba.ac.jp/english/admission/