This commit is contained in:
Borislav Ivanov 2019-11-08 13:15:27 +02:00
Коммит 5f00ce0ea6
16 изменённых файлов: 1333 добавлений и 0 удалений

330
.gitignore поставляемый Normal file
Просмотреть файл

@ -0,0 +1,330 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/

Просмотреть файл

@ -0,0 +1,14 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<ItemGroup>
<SampleSolutions Include="..\**\*.sln"/>
</ItemGroup>
<Target Name="Build">
<Exec Command="nuget restore %(SampleSolutions.Identity)" />
<MsBuild Projects="%(SampleSolutions.Identity)" Targets="Build" Properties="Configuration=Release;Platform=Any CPU" BuildInParallel="True" />
</Target>
</Project>

Просмотреть файл

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29319.158
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CaptureTraffic", "CaptureTraffic\CaptureTraffic.csproj", "{9840C21B-CEAF-42EB-8D76-3619D49CEC3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9840C21B-CEAF-42EB-8D76-3619D49CEC3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9840C21B-CEAF-42EB-8D76-3619D49CEC3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9840C21B-CEAF-42EB-8D76-3619D49CEC3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9840C21B-CEAF-42EB-8D76-3619D49CEC3D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {73FDB4DB-59BE-4E21-8356-0C2884A23680}
EndGlobalSection
EndGlobal

Двоичные данные
CaptureTraffic/NetFramework/CaptureTraffic/BasicFormatsForCore.dll Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9840C21B-CEAF-42EB-8D76-3619D49CEC3D}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>CaptureTraffic</RootNamespace>
<AssemblyName>CaptureTraffic</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BCMakeCert, Version=2.0.9.0, Culture=neutral, PublicKeyToken=67cb91587178ac5a, processorArchitecture=MSIL">
<HintPath>..\packages\BCMakeCert.2.0.9\lib\net40\BCMakeCert.dll</HintPath>
</Reference>
<Reference Include="DotNetZip, Version=1.13.4.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.13.4\lib\net40\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="FiddlerCore, Version=5.0.0.0, Culture=neutral, PublicKeyToken=67cb91587178ac5a, processorArchitecture=MSIL">
<HintPath>..\packages\FiddlerCore.5.0.0\lib\net40\FiddlerCore.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Windows.Forms" />
<Reference Include="Telerik.NetworkConnections, Version=0.2.0.0, Culture=neutral, PublicKeyToken=67cb91587178ac5a, processorArchitecture=MSIL">
<HintPath>..\packages\Telerik.NetworkConnections.0.2.0\lib\net40\Telerik.NetworkConnections.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<None Include="BasicFormatsForCore.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
</Target>
</Project>

Просмотреть файл

@ -0,0 +1,378 @@
/*
* This demo program shows how to use the FiddlerCore library.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Fiddler;
using Telerik.NetworkConnections;
namespace CaptureTraffic
{
internal static class Program
{
// NOTE: In the next line, you can pass 0 for the port (instead of 8877) to have FiddlerCore auto-select an available port
private const ushort fiddlerCoreListenPort = 8877;
private static readonly ICollection<Session> sessions = new List<Session>();
private static readonly ReaderWriterLockSlim sessionsLock = new ReaderWriterLockSlim();
private static readonly string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private static void Main()
{
AttachEventListeners();
EnsureRootCertificate();
StartupFiddlerCore();
ExecuteUserCommands();
Quit();
}
private static void AttachEventListeners()
{
//
// It is important to understand that FiddlerCore calls event handlers on session-handling
// background threads. If you need to properly synchronize to the UI-thread (say, because
// you're adding the sessions to a list view) you must call .Invoke on a delegate on the
// window handle.
//
// If you are writing to a non-threadsafe data structure (e.g. List<T>) you must
// use a Monitor or other mechanism to ensure safety.
//
// Simply echo notifications to the console. Because Fiddler.CONFIG.QuietMode=true
// by default, we must handle notifying the user ourselves.
FiddlerApplication.OnNotification += (o, nea) => Console.WriteLine($"** NotifyUser: {nea.NotifyString}");
FiddlerApplication.Log.OnLogString += (o, lea) => Console.WriteLine($"** LogString: {lea.LogString}");
FiddlerApplication.BeforeRequest += session =>
{
// In order to enable response tampering, buffering mode MUST
// be enabled; this allows FiddlerCore to permit modification of
// the response in the BeforeResponse handler rather than streaming
// the response to the client as the response comes in.
session.bBufferResponse = false;
// Set this property if you want FiddlerCore to automatically authenticate by
// answering Digest/Negotiate/NTLM/Kerberos challenges itself
// session["X-AutoAuth"] = "(default)";
try
{
sessionsLock.EnterWriteLock();
sessions.Add(session);
}
finally
{
sessionsLock.ExitWriteLock();
}
};
/*
// The following event allows you to examine every response buffer read by Fiddler. Note that this isn't useful for the vast majority of
// applications because the raw buffer is nearly useless; it's not decompressed, it includes both headers and body bytes, etc.
//
// This event is only useful for a handful of applications which need access to a raw, unprocessed byte-stream
Fiddler.FiddlerApplication.OnReadResponseBuffer += (o, rrea) =>
{
// NOTE: arrDataBuffer is a fixed-size array. Only bytes 0 to iCountOfBytes should be read/manipulated.
//
// Just for kicks, lowercase every byte. Note that this will obviously break any binary content.
for (int i = 0; i < e.iCountOfBytes; i++)
{
if ((e.arrDataBuffer[i] > 0x40) && (e.arrDataBuffer[i] < 0x5b))
{
e.arrDataBuffer[i] = (byte)(e.arrDataBuffer[i] + (byte)0x20);
}
}
Console.WriteLine(String.Format("Read {0} response bytes for session {1}", e.iCountOfBytes, e.sessionOwner.id));
}
*/
/*
Fiddler.FiddlerApplication.BeforeResponse += session => {
// Console.WriteLine($"{session.id}:HTTP {session.responseCode} for {session.fullUrl}");
// Uncomment the following two statements to decompress/unchunk the
// HTTP response and subsequently modify any HTTP responses to replace
// instances of the word "Telerik" with "Progress". You MUST also
// set session.bBufferResponse = true inside the BeforeRequest event handler above.
//
//session.utilDecodeResponse(); session.utilReplaceInResponse("Telerik", "Progress");
};*/
FiddlerApplication.AfterSessionComplete += session =>
{
//Console.WriteLine($"Finished session: {oS.fullUrl}");
int sessionsCount = 0;
try
{
sessionsLock.EnterReadLock();
sessionsCount = sessions.Count;
}
finally
{
sessionsLock.ExitReadLock();
}
if (sessionsCount == 0)
return;
Console.Title = $"Session list contains: {sessionsCount} sessions";
};
// Tell the system console to handle CTRL+C by calling our method that
// gracefully shuts down the FiddlerCore.
//
// Note, this doesn't handle the case where the user closes the window with the close button.
Console.CancelKeyPress += (o, ccea) =>
{
Quit();
};
}
private static void EnsureRootCertificate()
{
BCCertMaker.BCCertMaker certProvider = new BCCertMaker.BCCertMaker();
CertMaker.oCertProvider = certProvider;
// On first run generate root certificate using the loaded provider, then re-use it for subsequent runs.
string rootCertificatePath = Path.Combine(assemblyDirectory, "..", "..", "RootCertificate.p12");
string rootCertificatePassword = "S0m3T0pS3cr3tP4ssw0rd";
if (!File.Exists(rootCertificatePath))
{
certProvider.CreateRootCertificate();
certProvider.WriteRootCertificateAndPrivateKeyToPkcs12File(rootCertificatePath, rootCertificatePassword);
}
else
{
certProvider.ReadRootCertificateAndPrivateKeyFromPkcs12File(rootCertificatePath, rootCertificatePassword);
}
// Once the root certificate is set up, ensure it's trusted.
if (!CertMaker.rootCertIsTrusted())
{
CertMaker.trustRootCert();
}
}
private static void StartupFiddlerCore()
{
FiddlerCoreStartupSettings startupSettings =
new FiddlerCoreStartupSettingsBuilder()
.ListenOnPort(fiddlerCoreListenPort)
.RegisterAsSystemProxy()
.ChainToUpstreamGateway()
.DecryptSSL()
.OptimizeThreadPool()
.Build();
FiddlerApplication.Startup(startupSettings);
FiddlerApplication.Log.LogString($"Created endpoint listening on port {CONFIG.ListenPort}");
}
private static void ExecuteUserCommands()
{
bool done = false;
do
{
Console.WriteLine("Enter a command [C=Clear; L=List; W=write SAZ; R=read SAZ; Q=Quit]:");
Console.Write(">");
ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine();
switch (char.ToLower(cki.KeyChar))
{
case 'c':
try
{
sessionsLock.EnterWriteLock();
sessions.Clear();
}
finally
{
sessionsLock.ExitWriteLock();
}
Console.Title = $"Session list contains: 0 sessions";
WriteCommandResponse("Clear...");
FiddlerApplication.Log.LogString("Cleared session list.");
break;
case 'l':
WriteSessions(sessions);
break;
case 'w':
string password = null;
Console.WriteLine("Password Protect this Archive (Y/N)?");
ConsoleKeyInfo yesNo = Console.ReadKey();
if ((yesNo.KeyChar == 'y') || (yesNo.KeyChar == 'Y'))
{
Console.WriteLine($"{Environment.NewLine}Enter the password:");
password = Console.ReadLine();
}
Console.WriteLine();
SaveSessionsToDesktop(sessions, password);
break;
case 'r':
ReadSessions(sessions);
int sessionsCount;
try
{
sessionsLock.EnterReadLock();
sessionsCount = sessions.Count;
}
finally
{
sessionsLock.ExitReadLock();
}
Console.Title = $"Session list contains: {sessionsCount} sessions";
break;
case 'q':
done = true;
break;
}
} while (!done);
}
private static void Quit()
{
WriteCommandResponse("Shutting down...");
FiddlerApplication.Shutdown();
}
private static void SaveSessionsToDesktop(IEnumerable<Session> sessions, string password)
{
string filename = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
Path.DirectorySeparatorChar + DateTime.Now.ToString("hh-mm-ss") + ".saz";
string response;
try
{
sessionsLock.EnterReadLock();
if (sessions.Any())
{
bool success = Utilities.WriteSessionArchive(filename, sessions.ToArray(), password, false);
response = $"{(success ? "Wrote" : "Failed to save")}: {filename}";
}
else
{
response = "No sessions have been captured.";
}
}
catch (Exception ex)
{
response = $"Save failed: {ex.Message}";
}
finally
{
sessionsLock.ExitReadLock();
}
WriteCommandResponse(response);
}
private static void ReadSessions(ICollection<Session> sessions)
{
string sazFilename = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Path.DirectorySeparatorChar +
"ToLoad.saz";
Session[] loaded = Utilities.ReadSessionArchive(sazFilename, false, "", (file, part) =>
{
Console.WriteLine($"Enter the password for { part } (or just hit Enter to cancel):");
string sResult = Console.ReadLine();
Console.WriteLine();
return sResult;
});
if (loaded == null || loaded.Length == 0)
{
WriteCommandResponse($"Could not load sessions from {sazFilename}");
return;
}
try
{
sessionsLock.EnterWriteLock();
for (int i = 0; i < loaded.Length; i++)
{
sessions.Add(loaded[i]);
}
}
finally
{
sessionsLock.ExitWriteLock();
}
WriteCommandResponse($"Loaded: {loaded.Length} sessions.");
}
private static void WriteCommandResponse(string s)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(s);
Console.ForegroundColor = oldColor;
}
private static void WriteSessions(IEnumerable<Session> sessions)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
StringBuilder sb = new StringBuilder($"Session list contains:{Environment.NewLine}");
try
{
sessionsLock.EnterReadLock();
foreach (Session s in sessions)
{
sb.AppendLine($"{s.id} {s.oRequest.headers.HTTPMethod} {Ellipsize(s.fullUrl, 60)}");
sb.AppendLine($"{s.responseCode} {s.oResponse.MIMEType}{Environment.NewLine}");
}
}
finally
{
sessionsLock.ExitReadLock();
}
Console.Write(sb.ToString());
Console.ForegroundColor = oldColor;
}
private static string Ellipsize(string text, int length)
{
if (Equals(text, null)) throw new ArgumentNullException(nameof(text));
const int minLength = 3;
if (length < minLength) throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(length)} cannot be less than {minLength}");
if (text.Length <= length) return text;
return text.Substring(0, length - minLength) + new string('.', minLength);
}
}
}

Просмотреть файл

@ -0,0 +1,12 @@
using System.Reflection;
[assembly: AssemblyTitle("FiddlerCoreDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FiddlerCoreDemo")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Просмотреть файл

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BCMakeCert" version="2.0.9" targetFramework="net40" />
<package id="DotNetZip" version="1.13.4" targetFramework="net40" />
<package id="FiddlerCore" version="5.0.0" targetFramework="net40" />
<package id="Telerik.NetworkConnections" version="0.2.0" targetFramework="net40" />
</packages>

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.telerik.com/" value="https://nuget.telerik.com/nuget" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

Просмотреть файл

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29319.158
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaptureTraffic", "CaptureTraffic\CaptureTraffic.csproj", "{C216EA3D-CE18-4A92-8909-C39140328065}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C216EA3D-CE18-4A92-8909-C39140328065}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C216EA3D-CE18-4A92-8909-C39140328065}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C216EA3D-CE18-4A92-8909-C39140328065}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C216EA3D-CE18-4A92-8909-C39140328065}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EFE54DFB-4801-4E60-A6B1-CC85FFBA8921}
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FiddlerCore" Version="5.0.0" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,372 @@
/*
* This demo program shows how to use the FiddlerCore library.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Fiddler;
using Telerik.NetworkConnections;
using BCCertMaker;
namespace CaptureTraffic
{
internal static class Program
{
// NOTE: In the next line, you can pass 0 for the port (instead of 8877) to have FiddlerCore auto-select an available port
private const ushort fiddlerCoreListenPort = 8877;
private static readonly ICollection<Session> sessions = new List<Session>();
private static readonly ReaderWriterLockSlim sessionsLock = new ReaderWriterLockSlim();
private static readonly string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private static void Main()
{
AttachEventListeners();
EnsureRootCertificate();
StartupFiddlerCore();
ExecuteUserCommands();
Quit();
}
private static void AttachEventListeners()
{
//
// It is important to understand that FiddlerCore calls event handlers on session-handling
// background threads. If you need to properly synchronize to the UI-thread (say, because
// you're adding the sessions to a list view) you must call .Invoke on a delegate on the
// window handle.
//
// If you are writing to a non-threadsafe data structure (e.g. List<T>) you must
// use a Monitor or other mechanism to ensure safety.
//
FiddlerApplication.Log.OnLogString += (o, lea) => Console.WriteLine($"** LogString: {lea.LogString}");
FiddlerApplication.BeforeRequest += session =>
{
// In order to enable response tampering, buffering mode MUST
// be enabled; this allows FiddlerCore to permit modification of
// the response in the BeforeResponse handler rather than streaming
// the response to the client as the response comes in.
session.bBufferResponse = false;
// Set this property if you want FiddlerCore to automatically authenticate by
// answering Digest/Negotiate/NTLM/Kerberos challenges itself
// session["X-AutoAuth"] = "(default)";
try
{
sessionsLock.EnterWriteLock();
sessions.Add(session);
}
finally
{
sessionsLock.ExitWriteLock();
}
};
/*
// The following event allows you to examine every response buffer read by Fiddler. Note that this isn't useful for the vast majority of
// applications because the raw buffer is nearly useless; it's not decompressed, it includes both headers and body bytes, etc.
//
// This event is only useful for a handful of applications which need access to a raw, unprocessed byte-stream
Fiddler.FiddlerApplication.OnReadResponseBuffer += (o, rrea) =>
{
// NOTE: arrDataBuffer is a fixed-size array. Only bytes 0 to iCountOfBytes should be read/manipulated.
//
// Just for kicks, lowercase every byte. Note that this will obviously break any binary content.
for (int i = 0; i < e.iCountOfBytes; i++)
{
if ((e.arrDataBuffer[i] > 0x40) && (e.arrDataBuffer[i] < 0x5b))
{
e.arrDataBuffer[i] = (byte)(e.arrDataBuffer[i] + (byte)0x20);
}
}
Console.WriteLine(String.Format("Read {0} response bytes for session {1}", e.iCountOfBytes, e.sessionOwner.id));
}
*/
/*
Fiddler.FiddlerApplication.BeforeResponse += session => {
// Console.WriteLine($"{session.id}:HTTP {session.responseCode} for {session.fullUrl}");
// Uncomment the following two statements to decompress/unchunk the
// HTTP response and subsequently modify any HTTP responses to replace
// instances of the word "Telerik" with "Progress". You MUST also
// set session.bBufferResponse = true inside the BeforeRequest event handler above.
//
//session.utilDecodeResponse(); session.utilReplaceInResponse("Telerik", "Progress");
};*/
FiddlerApplication.AfterSessionComplete += session =>
{
//Console.WriteLine($"Finished session: {oS.fullUrl}");
int sessionsCount = 0;
try
{
sessionsLock.EnterReadLock();
sessionsCount = sessions.Count;
}
finally
{
sessionsLock.ExitReadLock();
}
if (sessionsCount == 0)
return;
Console.Title = $"Session list contains: {sessionsCount} sessions";
};
// Tell the system console to handle CTRL+C by calling our method that
// gracefully shuts down the FiddlerCore.
//
// Note, this doesn't handle the case where the user closes the window with the close button.
Console.CancelKeyPress += (o, ccea) =>
{
Quit();
};
}
private static void EnsureRootCertificate()
{
BCCertMaker.BCCertMaker certProvider = new BCCertMaker.BCCertMaker();
CertMaker.oCertProvider = certProvider;
// On first run generate root certificate using the loaded provider, then re-use it for subsequent runs.
string rootCertificatePath = Path.Combine(assemblyDirectory, "..", "..", "RootCertificate.p12");
string rootCertificatePassword = "S0m3T0pS3cr3tP4ssw0rd";
if (!File.Exists(rootCertificatePath))
{
certProvider.CreateRootCertificate();
certProvider.WriteRootCertificateAndPrivateKeyToPkcs12File(rootCertificatePath, rootCertificatePassword);
}
else
{
certProvider.ReadRootCertificateAndPrivateKeyFromPkcs12File(rootCertificatePath, rootCertificatePassword);
}
// Once the root certificate is set up, ensure it's trusted.
if (!CertMaker.rootCertIsTrusted())
{
CertMaker.trustRootCert();
}
}
private static void StartupFiddlerCore()
{
FiddlerCoreStartupSettings startupSettings =
new FiddlerCoreStartupSettingsBuilder()
.ListenOnPort(fiddlerCoreListenPort)
.RegisterAsSystemProxy()
.ChainToUpstreamGateway()
.DecryptSSL()
.OptimizeThreadPool()
.Build();
FiddlerApplication.Startup(startupSettings);
FiddlerApplication.Log.LogString($"Created endpoint listening on port {CONFIG.ListenPort}");
}
private static void ExecuteUserCommands()
{
bool done = false;
do
{
Console.WriteLine("Enter a command [C=Clear; L=List; W=write SAZ; R=read SAZ; Q=Quit]:");
Console.Write(">");
ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine();
switch (char.ToLower(cki.KeyChar))
{
case 'c':
try
{
sessionsLock.EnterWriteLock();
sessions.Clear();
}
finally
{
sessionsLock.ExitWriteLock();
}
Console.Title = $"Session list contains: 0 sessions";
WriteCommandResponse("Clear...");
FiddlerApplication.Log.LogString("Cleared session list.");
break;
case 'l':
WriteSessions(sessions);
break;
case 'w':
string password = null;
Console.WriteLine("Password Protect this Archive (Y/N)?");
ConsoleKeyInfo yesNo = Console.ReadKey();
if ((yesNo.KeyChar == 'y') || (yesNo.KeyChar == 'Y'))
{
Console.WriteLine($"{Environment.NewLine}Enter the password:");
password = Console.ReadLine();
}
Console.WriteLine();
SaveSessionsToDesktop(sessions, password);
break;
case 'r':
ReadSessions(sessions);
int sessionsCount;
try
{
sessionsLock.EnterReadLock();
sessionsCount = sessions.Count;
}
finally
{
sessionsLock.ExitReadLock();
}
Console.Title = $"Session list contains: {sessionsCount} sessions";
break;
case 'q':
done = true;
break;
}
} while (!done);
}
private static void Quit()
{
WriteCommandResponse("Shutting down...");
FiddlerApplication.Shutdown();
}
private static void SaveSessionsToDesktop(IEnumerable<Session> sessions, string password)
{
string filename = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
Path.DirectorySeparatorChar + DateTime.Now.ToString("hh-mm-ss") + ".saz";
string response;
try
{
sessionsLock.EnterReadLock();
if (sessions.Any())
{
bool success = Utilities.WriteSessionArchive(filename, sessions.ToArray(), password);
response = $"{(success ? "Wrote" : "Failed to save")}: {filename}";
}
else
{
response = "No sessions have been captured.";
}
}
catch (Exception ex)
{
response = $"Save failed: {ex.Message}";
}
finally
{
sessionsLock.ExitReadLock();
}
WriteCommandResponse(response);
}
private static void ReadSessions(ICollection<Session> sessions)
{
string sazFilename = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Path.DirectorySeparatorChar + "ToLoad.saz";
Session[] loaded = Utilities.ReadSessionArchive(sazFilename, "", (file, part) =>
{
Console.WriteLine($"Enter the password for { part } (or just hit Enter to cancel):");
string sResult = Console.ReadLine();
Console.WriteLine();
return sResult;
});
if (loaded == null || loaded.Length == 0)
{
WriteCommandResponse($"Could not load sessions from {sazFilename}");
return;
}
try
{
sessionsLock.EnterWriteLock();
for (int i = 0; i < loaded.Length; i++)
{
sessions.Add(loaded[i]);
}
}
finally
{
sessionsLock.ExitWriteLock();
}
WriteCommandResponse($"Loaded: {loaded.Length} sessions.");
}
private static void WriteCommandResponse(string s)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(s);
Console.ForegroundColor = oldColor;
}
private static void WriteSessions(IEnumerable<Session> sessions)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
StringBuilder sb = new StringBuilder($"Session list contains:{Environment.NewLine}");
try
{
sessionsLock.EnterReadLock();
foreach (Session s in sessions)
{
sb.AppendLine($"{s.id} {s.oRequest.headers.HTTPMethod} {Ellipsize(s.fullUrl, 60)}");
sb.AppendLine($"{s.responseCode} {s.oResponse.MIMEType}{Environment.NewLine}");
}
}
finally
{
sessionsLock.ExitReadLock();
}
Console.Write(sb.ToString());
Console.ForegroundColor = oldColor;
}
private static string Ellipsize(string text, int length)
{
if (Equals(text, null)) throw new ArgumentNullException(nameof(text));
const int minLength = 3;
if (length < minLength) throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(length)} cannot be less than {minLength}");
if (text.Length <= length) return text;
return text.Substring(0, length - minLength) + new string('.', minLength);
}
}
}

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.telerik.com/" value="https://nuget.telerik.com/nuget" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

21
LICENSE Normal file
Просмотреть файл

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Telerik EAD
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

41
README.md Normal file
Просмотреть файл

@ -0,0 +1,41 @@
# Sample apps for Progress® Telerik® FiddlerCore Embedded Engine
[FiddlerCore](https://www.telerik.com/fiddler/fiddlercore) is a cross-platform .NET library by Progress Telerik which allows capture
and modification of HTTP/HTTPS traffic. Some of the most popular applications using FiddlerCore are
[Telerik Fiddler](https://www.telerik.com/fiddler) (.NET Framework-based and running on Windows) and
[Fiddler Everywhere](https://www.telerik.com/fiddler-everywhere) (.NET Core-based and running on Windows, Mac, and Linux).
This repository contains sample applications demonstrating possible usages of the FiddlerCore API.
## Demos description
Currently the following demo is provided:
### [Capture traffic](/CaptureTraffic)
The sample demonstrates the following concepts:
- Generate and install unique certificate that will be used for decrypting HTTPS traffic.
- Set proxy allowing to capture all traffic from the machine, and chaining to upstream proxy if needed. Reset the proxy on exit.
- Capture HTTP/S sessions and preserve them in a list.
- Save the sessions in a SAZ (Session Archive Zip) file, a standard archive file which can be password-protected and, if needed,
opened later with Fiddler.
- Open a SAZ file and list session information.
## Building a sample
Each demo is provided as a C# solution in two different flavors:
- .NET Core-based application targeting .NET Core 2.1 and using FiddlerCore for .NET Standard 2 (netstandard2.0).
- .NET Framework-based application targeting .NET Framework 4 and using FiddlerCore for .NET Framework 4 (net40).
To build the application, you would need the corresponding [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core/2.1)
or [.NET Framework SDK](https://dotnet.microsoft.com/download/visual-studio-sdks).
The APIs used are not so specific, so retargeting the sample applications, for example to .NET Core 3.0 or .NET Framework 4.8,
is possible.
FiddlerCore-related NuGet packages are referenced from the Telerik NuGet server. When prompted for credentials during NuGet
packages restore, use your [Telerik account](https://www.telerik.com/account) through you've obtained FiddlerCore.
## Contribution
Pull requests are welcome! Let's make these samples more understandable for everyone.