internal bool Attach(bool bCollectGWInfo) { if (!this._bIsAttached) { if (bCollectGWInfo) { this.CollectConnectoidAndGatewayInfo(); } WinINETProxyInfo oNewInfo = new WinINETProxyInfo(); oNewInfo.bUseManualProxies = true; oNewInfo.bAllowDirect = true; oNewInfo.sHttpProxy = KPCONFIG.sKProxyListenHostPort; if (KPCONFIG.bCaptureCONNECT) { oNewInfo.sHttpsProxy = KPCONFIG.sKProxyListenHostPort; } else if (this.piPrior != null) { oNewInfo.sHttpsProxy = this.piPrior.sHttpsProxy; } if ((this.piPrior != null) && this.piPrior.bUseManualProxies) { oNewInfo.sFtpProxy = this.piPrior.sFtpProxy; oNewInfo.sSocksProxy = this.piPrior.sSocksProxy; } if (KPCONFIG.bCaptureFTP) { oNewInfo.sFtpProxy = KPCONFIG.sKProxyListenHostPort; } oNewInfo.sHostsThatBypass = KPCONFIG.sHostsThatBypassKProxy; if (KPCONFIG.bHookWithPAC) { if (KProxyApplication.Prefs.GetBoolPref("KProxy.proxy.pacfile.usefileprotocol", true)) { oNewInfo.sPACScriptLocation = "file://" + KPCONFIG.GetPath("Pac"); } else { oNewInfo.sPACScriptLocation = "http://" + KPCONFIG.sKProxyListenHostPort + "/proxy.pac"; } } if (!KPCONFIG.bIsViewOnly) { if (this.oAllConnectoids.HookConnections(oNewInfo)) { this._bIsAttached = true; KProxyApplication.OnKProxyAttach(); this.WriteAutoProxyPACFile(true); } else { KProxyApplication.DoNotifyUser("Failed to register KProxy as the system proxy.", "Error"); _setDynamicRegistryKey(false); return(false); } _setDynamicRegistryKey(true); } } return(true); }
//internal object _GetTransportContext() //{ // if (base._httpsStream != null) // { // return base._httpsStream.get_TransportContext(); // } // return null; //} private X509Certificate AttachClientCertificate(Session oS, object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers) { if (localCertificates.Count > 0) { this.MarkAsAuthenticated(oS.LocalProcessID); oS.oFlags["x-client-cert"] = localCertificates[0].Subject + " Serial#" + localCertificates[0].GetSerialNumberString(); return(localCertificates[0]); } if ((remoteCertificate != null) || (acceptableIssuers.Length >= 1)) { X509Certificate certificate = this._GetDefaultCertificate(); if (certificate != null) { this.MarkAsAuthenticated(oS.LocalProcessID); oS.oFlags["x-client-cert"] = certificate.Subject + " Serial#" + certificate.GetSerialNumberString(); return(certificate); } if (KPCONFIG.bShowDefaultClientCertificateNeededPrompt && KProxyApplication.Prefs.GetBoolPref("KProxy.network.https.clientcertificate.ephemeral.prompt-for-missing", true)) { KProxyApplication.DoNotifyUser("The server [" + targetHost + "] requests a client certificate.\nPlease save a client certificate using the filename:\n\n" + KPCONFIG.GetPath("DefaultClientCertificate"), "Client Certificate Requested"); KProxyApplication.Prefs.SetBoolPref("KProxy.network.https.clientcertificate.ephemeral.prompt-for-missing", false); } } return(null); }
internal bool SecureClientPipe(string sHostname, HTTPResponseHeaders oHeaders) { X509Certificate2 certificate; try { certificate = CertMaker.FindCert(sHostname, true); } catch (Exception exception) { //KProxyApplication.Log.LogFormat("KProxy.https> Failed to obtain certificate for {0} due to {1}", new object[] { sHostname, exception.Message }); certificate = null; } try { if (certificate == null) { KProxyApplication.DoNotifyUser("Unable to find Certificate for " + sHostname, "HTTPS Interception Failure"); oHeaders.HTTPResponseCode = 0x1f6; oHeaders.HTTPResponseStatus = "502 KProxy unable to generate certificate"; } if (KPCONFIG.bDebugSpew) { KProxyApplication.DebugSpew("SecureClientPipe for: " + this.ToString() + " sending data to client:\n" + Utilities.ByteArrayToHexView(oHeaders.ToByteArray(true, true), 0x20)); } base.Send(oHeaders.ToByteArray(true, true)); if (oHeaders.HTTPResponseCode != 200) { KProxyApplication.DebugSpew("SecureClientPipe returning FALSE because HTTPResponseCode != 200"); return(false); } base._httpsStream = new SslStream(new NetworkStream(base._baseSocket, false), false); base._httpsStream.AuthenticateAsServer(certificate, _bWantClientCert, KPCONFIG.oAcceptedClientHTTPSProtocols, false); return(true); } catch (Exception exception2) { //KProxyApplication.Log.LogFormat("Secure client pipe failed: {0}{1}.", new object[] { exception2.Message, (exception2.InnerException == null) ? string.Empty : (" InnerException: " + exception2.InnerException.Message) }); KProxyApplication.DebugSpew("Secure client pipe failed: " + exception2.Message); try { base.End(); } catch { } } return(false); }
private void ScanPathForTranscoders(string sPath) { try { if (Directory.Exists(sPath)) { Evidence securityEvidence = Assembly.GetExecutingAssembly().Evidence; bool boolPref = KProxyApplication.Prefs.GetBoolPref("KProxy.debug.extensions.verbose", false); if (boolPref) { //KProxyApplication.Log.LogFormat("Searching for Transcoders under {0}", new object[] { sPath }); } foreach (FileInfo info in new DirectoryInfo(sPath).GetFiles()) { if (info.FullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !info.FullName.StartsWith("_", StringComparison.OrdinalIgnoreCase)) { Assembly assembly; if (boolPref) { //KProxyApplication.Log.LogFormat("Looking for Transcoders inside {0}", new object[] { info.FullName.ToString() }); } try { if (KPCONFIG.bRunningOnCLRv4) { assembly = Assembly.LoadFrom(info.FullName); } else { assembly = Assembly.LoadFrom(info.FullName, securityEvidence); } } catch (Exception exception) { KProxyApplication.LogAddonException(exception, "Failed to load " + info.FullName); continue; } this.ScanAssemblyForTranscoders(assembly); } } } } catch (Exception exception2) { KProxyApplication.DoNotifyUser(string.Format("[Kavprot Proxy] Failure loading Transcoders: {0}", exception2.Message), "Transcoders Load Error"); } }
private static void VistaClearTracks(bool bClearFiles, bool bClearCookies) { int num = 0; if (bClearCookies) { num = 2; } if (bClearFiles) { num = 0x100c; } try { using (Process.Start("rundll32.exe", "inetcpl.cpl,ClearMyTracksByProcess " + num.ToString())) { } } catch (Exception exception) { KProxyApplication.DoNotifyUser("Failed to launch ClearMyTracksByProcess.\n" + exception.Message, "Error"); } }
private static ICertificateProvider LoadOverrideCertProvider() { string stringPref = KProxyApplication.Prefs.GetStringPref("KProxy.certmaker.assembly", KPCONFIG.GetPath("App") + "CertMaker.dll"); if (File.Exists(stringPref)) { Assembly assembly; try { assembly = Assembly.LoadFrom(stringPref); if (!Utilities.KProxyMeetsVersionRequirement(assembly, "Certificate Maker")) { return(null); } } catch (Exception exception) { KProxyApplication.LogAddonException(exception, "Failed to load CertMaker" + stringPref); return(null); } foreach (Type type in assembly.GetExportedTypes()) { if ((!type.IsAbstract && type.IsPublic) && (type.IsClass && typeof(ICertificateProvider).IsAssignableFrom(type))) { try { return((ICertificateProvider)Activator.CreateInstance(type)); } catch (Exception exception2) { KProxyApplication.DoNotifyUser(string.Format("[Kavprot Proxy] Failure loading {0} CertMaker from {1}: {2}\n\n{3}\n\n{4}", new object[] { type.Name, assembly.CodeBase, exception2.Message, exception2.StackTrace, exception2.InnerException }), "Load Error"); } } } } return(null); }
internal static void EnsureFoldersExist() { try { if (!Directory.Exists(GetPath("Captures"))) { Directory.CreateDirectory(GetPath("Captures")); } if (!Directory.Exists(GetPath("Requests"))) { Directory.CreateDirectory(GetPath("Requests")); } if (!Directory.Exists(GetPath("Responses"))) { Directory.CreateDirectory(GetPath("Responses")); } if (!Directory.Exists(GetPath("Scripts"))) { Directory.CreateDirectory(GetPath("Scripts")); } } catch (Exception exception) { KProxyApplication.DoNotifyUser(exception.ToString(), "Folder Creation Failed"); } try { if ((!KProxyApplication.Prefs.GetBoolPref("KProxy.script.delaycreate", true) && !File.Exists(GetPath("CustomRules"))) && File.Exists(GetPath("SampleRules"))) { File.Copy(GetPath("SampleRules"), GetPath("CustomRules")); } } catch (Exception exception2) { KProxyApplication.DoNotifyUser(exception2.ToString(), "Initial file copies failed"); } }
private bool ScanAssemblyForTranscoders(Assembly assemblyInput) { bool flag = false; bool boolPref = KProxyApplication.Prefs.GetBoolPref("KProxy.debug.extensions.verbose", false); try { if (!Utilities.KProxyMeetsVersionRequirement(assemblyInput, "Importers and Exporters")) { //KProxyApplication.Log.LogFormat("Assembly {0} did not specify a RequiredVersionAttribute. Aborting load of transcoders.", new object[] { assemblyInput.CodeBase }); return(false); } foreach (Type type in assemblyInput.GetExportedTypes()) { if ((!type.IsAbstract && type.IsPublic) && type.IsClass) { if (typeof(ISessionImporter).IsAssignableFrom(type)) { try { if (!this.AddToImportOrExportCollection(this.m_Importers, type)) { //KProxyApplication.Log.LogFormat("WARNING: SessionImporter {0} from {1} failed to specify any ImportExportFormat attributes.", new object[] { type.Name, assemblyInput.CodeBase }); } else { flag = true; if (boolPref) { //KProxyApplication.Log.LogFormat(" Added SessionImporter {0}", new object[] { type.FullName }); } } } catch (Exception exception) { KProxyApplication.DoNotifyUser(string.Format("[Kavprot Proxy] Failure loading {0} SessionImporter from {1}: {2}\n\n{3}\n\n{4}", new object[] { type.Name, assemblyInput.CodeBase, exception.Message, exception.StackTrace, exception.InnerException }), "Extension Load Error"); } } if (typeof(ISessionExporter).IsAssignableFrom(type)) { try { if (!this.AddToImportOrExportCollection(this.m_Exporters, type)) { //KProxyApplication.Log.LogFormat("WARNING: SessionExporter {0} from {1} failed to specify any ImportExportFormat attributes.", new object[] { type.Name, assemblyInput.CodeBase }); } else { flag = true; if (boolPref) { //KProxyApplication.Log.LogFormat(" Added SessionExporter {0}", new object[] { type.FullName }); } } } catch (Exception exception2) { KProxyApplication.DoNotifyUser(string.Format("[Kavprot Proxy] Failure loading {0} SessionExporter from {1}: {2}\n\n{3}\n\n{4}", new object[] { type.Name, assemblyInput.CodeBase, exception2.Message, exception2.StackTrace, exception2.InnerException }), "Extension Load Error"); } } } } } catch (Exception exception3) { KProxyApplication.DoNotifyUser(string.Format("[Kavprot Proxy] Failure loading Importer/Exporter from {0}: {1}", assemblyInput.CodeBase, exception3.Message), "Extension Load Error"); return(false); } return(flag); }
internal bool Start(int iPort, bool bAllowRemote) { if (KPCONFIG.bIsViewOnly) { return(false); } bool flag = false; try { flag = (bAllowRemote && KPCONFIG.bEnableIPv6) && Socket.OSSupportsIPv6; } catch (Exception exception) { KProxyApplication.DoNotifyUser(string.Concat(new object[] { "A Microsoft .NET configuration file (listed below) is corrupt and contains invalid data. You can often correct this error by installing updates from WindowsUpdate and/or reinstalling the .NET Framework.\n\n", exception.Message, "\nSource: ", exception.Source, "\n", exception.StackTrace, "\n\n", exception.InnerException, "\nKProxy v", Application.ProductVersion, (8 == IntPtr.Size) ? " (x64) " : " (x86) ", " [.NET ", Environment.Version, " on ", Environment.OSVersion.VersionString, "] " }), ".NET Configuration Error", MessageBoxIcon.Hand); this.oAcceptor = null; return(false); } try { if (flag) { this.oAcceptor = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); if (Environment.OSVersion.Version.Major > 5) { this.oAcceptor.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.PacketInformation | SocketOptionName.KeepAlive, 0); } } else { this.oAcceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } if (KPCONFIG.ForceExclusivePort) { this.oAcceptor.ExclusiveAddressUse = true; } if (bAllowRemote) { if (flag) { this.oAcceptor.Bind(new IPEndPoint(IPAddress.IPv6Any, iPort)); } else { this.oAcceptor.Bind(new IPEndPoint(IPAddress.Any, iPort)); } } else { this.oAcceptor.Bind(new IPEndPoint(IPAddress.Loopback, iPort)); } this.oAcceptor.Listen(50); } catch (SocketException exception2) { string str = string.Empty; string sTitle = "KProxy Cannot Listen"; switch (exception2.ErrorCode) { case 0x273f: case 0x2741: if (flag) { str = "\nThis often means that you've enabled IPv6 support inside Tools > Kavprot Proxy Options, but your computer has IPv6 disabled."; } break; case 0x2740: case 0x271d: str = "\nThis is usually due to another service running on this port. Run NETSTAT -A at a command prompt.\nIf you don't want to stop using the other program, simply change the port used by KProxy.\nClick Tools > KProxy Options > Connections, select a new port, and restart KProxy."; sTitle = "Kavprot Proxy Port in Use"; break; } this.oAcceptor = null; KProxyApplication.DoNotifyUser(string.Format("Unable to bind to port [Localhost:{0}]. ErrorCode: {1}.\n{2}\n\n{3}\n\n{4}", new object[] { KPCONFIG.ListenPort, exception2.ErrorCode, str, exception2.ToString(), string.Concat(new object[] { "KProxy v", Application.ProductVersion, " [.NET ", Environment.Version, " on ", Environment.OSVersion.VersionString, "]" }) }), sTitle, MessageBoxIcon.Hand); return(false); } catch (Exception exception3) { this.oAcceptor = null; KProxyApplication.ReportException(exception3); return(false); } try { this.oAcceptor.BeginAccept(new AsyncCallback(this.AcceptConnection), null); } catch (Exception exception4) { this.oAcceptor = null; //KProxyApplication.Log.LogFormat("Kavprot Proxy BeginAccept() Exception: {0}", new object[] { exception4.Message }); return(false); } return(true); }
public static string GetPath(string sWhatPath) { string folderPath; switch (sWhatPath) { case "App": return(Path.GetDirectoryName(Application.ExecutablePath) + @"\"); case "AutoKProxys_Machine": return(Path.GetDirectoryName(Application.ExecutablePath) + @"\Scripts\"); case "AutoKProxys_User": return(sUserPath + @"Scripts\"); case "AutoResponderDefaultRules": return(sUserPath + @"\AutoResponder.xml"); case "Captures": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.captures", sUserPath + @"Captures\")); case "CustomRules": return(sScriptPath); case "DefaultClientCertificate": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.defaultclientcert", sUserPath + "ClientCertificate.cer")); case "KProxyRootCert": return(sUserPath + "Root.cer"); case "Filters": return(sUserPath + @"Filters\"); case "Inspectors": return(Path.GetDirectoryName(Application.ExecutablePath) + @"\Inspectors\"); case "PerUser-ISA-Config": folderPath = "C:"; try { folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); } catch { } return(folderPath + @"\microsoft\firewall client 2004\management.ini"); case "PerMachine-ISA-Config": folderPath = "C:"; try { folderPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); } catch { } return(folderPath + @"\microsoft\firewall client 2004\management.ini"); case "MakeCert": folderPath = KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.makecert", Path.GetDirectoryName(Application.ExecutablePath) + @"\MakeCert.exe"); if (!File.Exists(folderPath)) { folderPath = "MakeCert.exe"; } return(folderPath); case "MyDocs": folderPath = "C:"; try { folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); } catch (Exception exception) { KProxyApplication.DoNotifyUser("Initialization Error", "Failed to retrieve path to your My Documents folder.\nThis generally means you have a relative environment variable.\nDefaulting to C:\\\n\n" + exception.Message); } return(folderPath); case "Pac": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.pac", sUserPath + @"Scripts\BrowserPAC.js")); case "Requests": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.requests", sUserPath + @"Captures\Requests\")); case "Responses": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.responses", sUserPath + @"Captures\Responses\")); case "Root": return(sUserPath); case "SafeTemp": folderPath = "C:"; try { folderPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); } catch (Exception exception2) { KProxyApplication.DoNotifyUser("Failed to retrieve path to your Internet Cache folder.\nThis generally means you have a relative environment variable.\nDefaulting to C:\\\n\n" + exception2.Message, "GetPath(SafeTemp) Failed"); } return(folderPath); case "SampleRules": return(Path.GetDirectoryName(Application.ExecutablePath) + @"\Scripts\SampleRules.js"); case "Scripts": return(sUserPath + @"Scripts\"); case "TemplateResponses": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.templateresponses", Path.GetDirectoryName(Application.ExecutablePath) + @"\ResponseTemplates\")); case "TextEditor": return(KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.texteditor", m_TextEditor)); case "Transcoders_Machine": return(Path.GetDirectoryName(Application.ExecutablePath) + @"\ImportExport\"); case "Transcoders_User": return(sUserPath + @"ImportExport\"); case "WINDIFF": { string stringPref = KProxyApplication.Prefs.GetStringPref("KProxy.KPCONFIG.path.comparetool", null); if (string.IsNullOrEmpty(stringPref)) { if (m_CompareTool == "windiff.exe") { if (File.Exists(GetPath("App") + "windiff.exe")) { return(GetPath("App") + "windiff.exe"); } RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinMerge.exe", false); if (key != null) { string str3 = (string)key.GetValue(string.Empty, null); key.Close(); if (str3 != null) { return(str3); } } } return(m_CompareTool); } return(stringPref); } } return("C:"); }