diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ce6fdd --- /dev/null +++ b/.gitignore @@ -0,0 +1,340 @@ +## 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 +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +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/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.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 +*- Backup*.rdl + +# 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 personal settings +.cr/personal + +# 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/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b024f8f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Arcporter/Lib/Pyre"] + path = Arcporter/Lib/Pyre + url = https://github.com/themeldingwars/Pyre.git diff --git a/FauCap.sln b/FauCap.sln new file mode 100644 index 0000000..9bc3398 --- /dev/null +++ b/FauCap.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30001.183 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FauCap", "FauCap\FauCap.csproj", "{E758C195-C5AB-4F21-A4FD-6BE0B89A7295}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E758C195-C5AB-4F21-A4FD-6BE0B89A7295}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E758C195-C5AB-4F21-A4FD-6BE0B89A7295}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E758C195-C5AB-4F21-A4FD-6BE0B89A7295}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E758C195-C5AB-4F21-A4FD-6BE0B89A7295}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FDA76F97-0215-4B9A-B325-C46FD566D961} + EndGlobalSection +EndGlobal diff --git a/FauCap/FauCap.csproj b/FauCap/FauCap.csproj new file mode 100644 index 0000000..c74c518 --- /dev/null +++ b/FauCap/FauCap.csproj @@ -0,0 +1,18 @@ + + + + Exe + netcoreapp3.1 + freakbyte + TheMeldingWars + https://github.com/themeldingwars/FauCap + icon.ico + FauCap + + + + + + + + diff --git a/FauCap/GameSession.cs b/FauCap/GameSession.cs new file mode 100644 index 0000000..c2aee9c --- /dev/null +++ b/FauCap/GameSession.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; + +namespace FauCap +{ + [Serializable] + public class GameSession + { + public IPAddress RemoteIp; + public IPAddress LocalIp; + + public ushort MatrixPort; + public uint ProtocolVersion; + public uint SocketID; + public ushort StreamingProtocol; + public ushort SequenceStart; + public ushort GameServerPort; + + public List Packets; + + public GameSession() + { + Packets = new List(); + } + + public static void Write(string file, List sessions) + { + + using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)) + using (BinaryWriter bw = new BinaryWriter(fs)) + { + bw.Write("FCAP"); + bw.Write(sessions.Count); + foreach (GameSession s in sessions) + { + bw.Write(s.RemoteIp.ToString()); + bw.Write(s.LocalIp.ToString()); + bw.Write(s.MatrixPort); + bw.Write(s.ProtocolVersion); + bw.Write(s.SocketID); + bw.Write(s.StreamingProtocol); + bw.Write(s.SequenceStart); + bw.Write(s.GameServerPort); + + bw.Write(s.Packets.Count); + foreach (Packet p in s.Packets) + { + bw.Write(p.Time.Ticks); + bw.Write(p.FromServer); + bw.Write((ushort)p.Data.Length); + bw.Write(p.Data); + } + } + } + } + + public static List Read(string file) + { + List sessions = new List(); + + using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite)) + using (BinaryReader br = new BinaryReader(fs)) + { + if(br.ReadString() != "FCAP") + { + Console.WriteLine("Could not read faucap file, magic mismatch."); + return null; + } + + int sessionCount = br.ReadInt32(); + for (int i = 0; i < sessionCount; i++) + { + GameSession s = new GameSession(); + s.RemoteIp = IPAddress.Parse(br.ReadString()); + s.LocalIp = IPAddress.Parse(br.ReadString()); + s.MatrixPort = br.ReadUInt16(); + s.ProtocolVersion = br.ReadUInt32(); + s.SocketID = br.ReadUInt32(); + s.StreamingProtocol = br.ReadUInt16(); + s.SequenceStart = br.ReadUInt16(); + s.GameServerPort = br.ReadUInt16(); + + int packetCount = br.ReadInt32(); + for (int x = 0; x < packetCount; x++) + { + Packet p = new Packet(); + p.Time = new DateTime(br.ReadInt64()); + p.FromServer = br.ReadBoolean(); + p.Data = br.ReadBytes(br.ReadUInt16()); + s.Packets.Add(p); + } + sessions.Add(s); + } + } + return sessions; + } + + public class Packet + { + public DateTime Time; + public bool FromServer; + public byte[] Data; + public Packet() { } + public Packet(DateTime time, bool fromServer, byte[] data) + { + Time = time; + FromServer = fromServer; + Data = data; + } + } + } + +} diff --git a/FauCap/PacketUtil.cs b/FauCap/PacketUtil.cs new file mode 100644 index 0000000..456855f --- /dev/null +++ b/FauCap/PacketUtil.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace FauCap +{ + internal static class PacketUtil + { + public static bool IsControlPacket(Span data) + { + if (data.Length >= 4) + { + return MemoryMarshal.Read(data) == 0; + } + return false; + } + + public static bool IsHandshakePacket(Span data) + { + if (data.Length > 8) + { + switch (Handshake.ReadName(data)) + { + case "POKE": + case "HEHE": + case "KISS": + case "HUGG": + case "ABRT": + return true; + default: + return false; + } + + } + return false; + } + + public static class Handshake + { + public static string ReadName(Span data) + { + return Encoding.ASCII.GetString(data.Slice(4, 4)); + } + public static uint ReadProtocolVersion(Span data) + { + return MemoryMarshal.Read(data.Slice(8, 4)); + } + public static uint ReadSocketId(Span data) + { + return MemoryMarshal.Read(data.Slice(8, 4)); + } + public static ushort ReadStreamingProtocol(Span data) + { + return MemoryMarshal.Read(data.Slice(12, 2)); + } + public static ushort ReadSequenceStart(Span data) + { + return MemoryMarshal.Read(data.Slice(8, 2)); + } + public static ushort ReadGameServerPort(Span data) + { + return MemoryMarshal.Read(data.Slice(10, 2)); + } + } + } +} diff --git a/FauCap/Program.cs b/FauCap/Program.cs new file mode 100644 index 0000000..c9980bb --- /dev/null +++ b/FauCap/Program.cs @@ -0,0 +1,175 @@ +using SharpPcap; +using SharpPcap.LibPcap; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using static FauCap.PacketUtil; + +namespace FauCap +{ + class Program + { + + static List sessions; + private static Status status; + + private static string usage = ""; + + static void Main(string[] args) + { + + string inFile = ""; + string outFile = ""; + + + if (args.Length > 0 ) + { + if(File.Exists(args[0])) + { + inFile = args[0]; + if(args.Length > 1) + { + outFile = Path.GetFullPath(args[1]); + } + else + { + outFile = Path.ChangeExtension(args[0], "faucap"); + } + } + else + { + Console.WriteLine($"File {args[0]} does not exist."); + Console.WriteLine(usage); + return; + } + } + else + { + Console.WriteLine(usage); + return; + } + + sessions = new List(); + status = Status.Waiting; + + CaptureFileReaderDevice device; + try + { + device = new CaptureFileReaderDevice(inFile); + device.Open(); + Console.WriteLine($"Parsing pcap file {inFile}"); + } + catch (Exception e) + { + Console.WriteLine($"Could not parse {inFile}, is it a valid pcap capture?"); + return; + } + + device.OnPacketArrival += new PacketArrivalEventHandler(OnPacketArrival); + device.Capture(); + device.Close(); + + Console.WriteLine($"Done parsing {inFile}, " + (sessions.Count > 0 ? $"{sessions.Count} sessions was found, exporting faucap." : "but no game sessions was found.")); + + GameSession.Write(outFile, sessions); + + } + + static void OnPacketArrival(object sender, CaptureEventArgs e) + { + if (e.Packet.LinkLayerType == PacketDotNet.LinkLayers.Ethernet) + { + var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); + + var udpPacket = (PacketDotNet.UdpPacket)packet.Extract(); + + var ipPacket = (PacketDotNet.IPv4Packet)packet.Extract(); + + if (udpPacket == null) { return; } + byte[] data = udpPacket.PayloadData; + + DateTime time = e.Packet.Timeval.Date; + + if(IsHandshakePacket(data)) + { + switch (Handshake.ReadName(data)) + { + case "POKE": + status = Status.Poked; + GameSession session = new GameSession(); + session.Packets.Add(new GameSession.Packet(time, false, data)); + + session.LocalIp = ipPacket.SourceAddress; + session.RemoteIp = ipPacket.DestinationAddress; + session.MatrixPort = udpPacket.DestinationPort; + + session.ProtocolVersion = Handshake.ReadProtocolVersion(data); + + sessions.Add(session); + break; + + case "HEHE": + if(status != Status.Poked) + { + status = Status.Waiting; + break; + } + + status = Status.Laughed; + sessions.Last().SocketID = Handshake.ReadSocketId(data); + sessions.Last().Packets.Add(new GameSession.Packet(time, true, data)); + break; + + case "KISS": + if (status != Status.Laughed) + { + status = Status.Waiting; + break; + } + + status = Status.Kissed; + sessions.Last().StreamingProtocol = Handshake.ReadStreamingProtocol(data); + sessions.Last().Packets.Add(new GameSession.Packet(time, false, data)); + break; + + case "HUGG": + if (status != Status.Kissed) + { + status = Status.Waiting; + break; + } + + status = Status.Hugged; + sessions.Last().SequenceStart = Handshake.ReadSequenceStart(data); + sessions.Last().GameServerPort = Handshake.ReadGameServerPort(data); + sessions.Last().Packets.Add(new GameSession.Packet(time, true, data)); + break; + + case "ABRT": + if (status != Status.Waiting && sessions.Last().LocalIp != null) + { + sessions.Last().Packets.Add(new GameSession.Packet(time, ipPacket.DestinationAddress == sessions.Last().LocalIp, data)); + } + break; + } + } + else if(data != null && status == Status.Hugged && sessions.Last().SocketID == MemoryMarshal.Read(data)) + { + sessions.Last().Packets.Add(new GameSession.Packet(time, ipPacket.DestinationAddress == sessions.Last().LocalIp, data)); + } + + } + } + + public enum Status + { + Waiting, + Poked, + Laughed, + Kissed, + Hugged, + } + } +} diff --git a/FauCap/icon.ico b/FauCap/icon.ico new file mode 100644 index 0000000..25cb5a8 Binary files /dev/null and b/FauCap/icon.ico differ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff904bb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 The Melding Wars + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..05e4774 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ + +# FauCap + +*This tool was created for educational purposes only and should not be used by anyone.* + +Usage: FauCap.exe path/to/some.pcap [output file path] \ No newline at end of file diff --git a/publish.bat b/publish.bat new file mode 100644 index 0000000..d0ef08b --- /dev/null +++ b/publish.bat @@ -0,0 +1,6 @@ +dotnet clean +dotnet restore +dotnet build -c release +dotnet publish -c release -r win7-x64 -o FauCap/bin/dist/win-x64 --self-contained false +dotnet publish -c release -r linux-x64 -o FauCap/bin/dist/linux-x64 --self-contained false +dotnet publish -c release -r osx-x64 -o FauCap/bin/dist/osx-x64 --self-contained false \ No newline at end of file