private static void LoadPacketFileExecTask(IPacketContainer packets, PacketLogReader reader)
        {
            PacketObject packet;

            while ((packet = reader.ReadPacket()) != null)
            {
                packets.Add(packet);
            }

            reader.Close();

            DebugManager.MessageOut("LoadPacketFile - Complete");
        }
        private void OpenPacketLogMenuItem_Click(object sender, EventArgs e)
        {
            if (OpenPacketLogFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            _teraSniffer.Enabled = false;
            var server = new Server("Packet Log", "EU", null);

            HandleNewConnection(server);
            foreach (var message in PacketLogReader.ReadMessages(OpenPacketLogFileDialog.FileName))
            {
                HandleMessageReceived(message);
            }
        }
        private void PlayRecord(GateObject[] gates, PacketFilterController filter, PacketLogReader reader)
        {
            ProgressMax = (uint)reader.ProgressMax;
            ProgressNow = (uint)reader.ProgressNow;

            var packet_busy = LoadPlayPacket(reader, filter);
            var packet_next = (PacketObject)null;
            var delay_timer = new System.Diagnostics.Stopwatch();
            var delay_value = 0;

            while ((!cancel_req_) && (packet_busy != null))
            {
                /* 次のデータ送信までの遅延 */
                while ((delay_timer.IsRunning) && (delay_timer.ElapsedMilliseconds < delay_value))
                {
                    if (delay_timer.ElapsedMilliseconds > 10)
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                }

                /* パケット送信 */
                delay_timer.Restart();
                foreach (var gate in gates)
                {
                    gate.SendRequest(packet_busy.Data);
                }

                /* 次のパケットを取得 */
                packet_next = LoadPlayPacket(reader, filter);
                if (packet_next != null)
                {
                    delay_value = (int)(packet_next.MakeTime - packet_busy.MakeTime).TotalMilliseconds + SEND_TIMMING_MARGIN;
                }
                packet_busy = packet_next;

                ProgressNow = (uint)reader.ProgressNow;
            }

            ProgressNow = (uint)reader.ProgressNow;

            reader.Close();
        }
        private PacketObject LoadPlayPacket(PacketLogReader reader, PacketFilterController filter)
        {
            var packet = (PacketObject)null;

            while ((packet = reader.ReadPacket()) != null)
            {
                /* データパケット以外は無視 */
                if (packet.Attribute != PacketAttribute.Data)
                {
                    continue;
                }

                /* フィルターに合致しないパケットは無視 */
                if (!filter.Input(packet))
                {
                    continue;
                }

                return(packet);
            }

            return(null);
        }
        private void ExecTask(GateObject[] gates, PacketFilterController filter, PacketLogReader reader)
        {
            PlayRecord(gates, filter, reader);

            Success = (!cancel_req_);
        }
Exemple #6
0
        void HandleEntry(PacketLogReader reader, PacketLogEntry entry, PacketStatistics stats,
                         PacketSerializer serializer, StreamWriter result)
        {
            stats.TotalPackets++;

            var name = serializer.GameMessages.CodeToName[entry.MessageCode];

            if (_regexes.All(r => !r.IsMatch(name)))
            {
                stats.IgnoredPackets++;
                return;
            }

            result.WriteLine("[{0:yyyy-MM-dd HH:mm:ss:fff}] {1} {2}: {3} ({4} bytes)",
                             entry.Timestamp.ToLocalTime(), reader.Servers[entry.ServerId].Name,
                             entry.Direction.ToDirectionString(), name, entry.Payload.Count);

            var parsed  = serializer.Create(entry.MessageCode);
            var payload = entry.Payload.ToArray();

            if (payload.Length != 0)
            {
                if ((_hex == HexDumpMode.Unknown && parsed == null) || _hex == HexDumpMode.All)
                {
                    result.WriteLine();
                    result.WriteLine(new RawPacket(name)
                    {
                        Payload = payload,
                    });
                }

                if ((_analysis == AnalysisMode.Unknown && parsed == null) || _analysis == AnalysisMode.All)
                {
                    var arrays = PacketAnalysis.FindArrays(payload);

                    if (arrays.Any())
                    {
                        result.WriteLine();
                        result.WriteLine("Potential arrays:");
                        result.WriteLine();

                        foreach (var arr in arrays)
                        {
                            stats.PotentialArrays++;

                            result.WriteLine(arr);
                        }
                    }

                    var strings = PacketAnalysis.FindStrings(payload, _whiteSpace, _control, _length);

                    if (strings.Any())
                    {
                        result.WriteLine();
                        result.WriteLine("Potential strings:");
                        result.WriteLine();

                        foreach (var str in strings)
                        {
                            stats.PotentialStrings++;

                            result.WriteLine(str);
                        }
                    }
                }
            }

            stats.AddPacket(name, parsed != null, payload.Length);

            if (parsed != null && _parse)
            {
                stats.ParsedPackets++;

                serializer.Deserialize(payload, parsed);

                for (var i = 0; i < _roundtrips; i++)
                {
                    var payload2 = serializer.Serialize(parsed);
                    var len      = payload.Length;
                    var len2     = payload2.Length;

                    Assert.Check(len2 == len,
                                 $"Payload lengths for {name} don't match ({len2} versus {len}).");

                    if (i > 0)
                    {
                        Assert.Check(payload2.SequenceEqual(payload),
                                     $"Payloads for {name} don't match after roundtrip.");
                    }

                    if (i != _roundtrips - 1)
                    {
                        serializer.Deserialize(payload2, parsed);
                    }

                    payload = payload2;
                }

                result.WriteLine();
                result.WriteLine(parsed);
            }

            result.WriteLine();
        }
Exemple #7
0
        protected override int Invoke(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Expected exactly 1 argument");
                return(1);
            }

            var input = args[0];

            if (_output == null)
            {
                _output = Path.ChangeExtension(input, ParsedExtension);
            }

            if (_regexes.Count == 0)
            {
                _regexes.Add(new Regex(".*", FilterRegexOptions));
            }

            _log.Basic("Parsing {0}...", input);

            var stats = new PacketStatistics();

            using var reader = new PacketLogReader(input);

            if (_header)
            {
                _log.Info(string.Empty);
                _log.Info("Version: {0}", reader.Version);
                _log.Info("Compression: {0}", reader.CompressionLevel);
                _log.Info("Region: {0}", reader.Region);
                _log.Info("Client Version: {0}", reader.GameMessages.Version);
                _log.Info("Servers:");

                foreach (var srv in reader.Servers.Values)
                {
                    _log.Info("  {0} ({1}): {2} -> {3}", srv.Name, srv.Id, srv.RealEndPoint,
                              srv.ProxyEndPoint);
                }

                _log.Info(string.Empty);
            }

            PacketSerializer serializer;

            switch (_backend)
            {
            case PacketSerializerBackend.Reflection:
                serializer = new ReflectionPacketSerializer(reader.Region, reader.GameMessages,
                                                            reader.SystemMessages);
                break;

            case PacketSerializerBackend.Compiler:
                serializer = new CompilerPacketSerializer(reader.Region, reader.GameMessages,
                                                          reader.SystemMessages);
                break;

            default:
                throw Assert.Unreachable();
            }

            using var result = new StreamWriter(new FileStream(_output, FileMode.Create, FileAccess.Write));

            foreach (var entry in reader.EnumerateAll())
            {
                HandleEntry(reader, entry, stats, serializer, result);
            }

            _log.Basic("Parsed packets to {0}", _output);

            PrintStatistics(stats);

            return(0);
        }
Exemple #8
0
        public static int Run(string[] args)
        {
            try
            {
                if (!HandleArguments(ref args))
                {
                    return(0);
                }
            }
            catch (OptionException e)
            {
                Console.Error.WriteLine(e.Message);
                return(1);
            }

            if (args.Length != 1)
            {
                Console.Error.WriteLine("Expected exactly one input file argument.");
                return(1);
            }

            Log.Level           = LogLevel.Debug;
            Log.TimestampFormat = "HH:mm:ss:fff";

            var color = Console.ForegroundColor;

            Log.Loggers.Add(new ConsoleLogger(false,
                                              color, color, color, color, color));

            if (!Debugger.IsAttached)
            {
                AppDomain.CurrentDomain.UnhandledException += UnhandledException;
            }

            var input   = args[0];
            var output  = _output ?? Path.ChangeExtension(input, "txt");
            var regexes = _regexes.Select(x => new Regex(x, RegexOptions))
                          .DefaultIfEmpty(new Regex(".*", RegexOptions))
                          .ToArray();

            _log.Basic("Parsing {0}...", input);

            var stats = new PacketStatistics();

            using (var reader = new PacketLogReader(input))
            {
                if (_header)
                {
                    _log.Info(string.Empty);
                    _log.Info("Version: {0}", reader.Version);
                    _log.Info("Compressed: {0}", reader.Compressed);
                    _log.Info("Region: {0}", reader.Messages.Region);
                    _log.Info("Servers:");

                    foreach (var srv in reader.Servers.Values)
                    {
                        _log.Info("  {0} ({1}): {2} -> {3}", srv.Name, srv.Id,
                                  srv.RealEndPoint, srv.ProxyEndPoint);
                    }

                    _log.Info(string.Empty);
                }

                PacketSerializer serializer;

                switch (_backend)
                {
                case PacketSerializerBackend.Reflection:
                    serializer = new ReflectionPacketSerializer(
                        reader.Messages);
                    break;

                case PacketSerializerBackend.Compiler:
                    serializer = new CompilerPacketSerializer(
                        reader.Messages);
                    break;

                default:
                    throw Assert.Unreachable();
                }

                using (var result = new StreamWriter(new FileStream(output,
                                                                    FileMode.Create, FileAccess.Write)))
                    foreach (var entry in reader.EnumerateAll())
                    {
                        HandleEntry(reader, entry, regexes, stats, serializer,
                                    result);
                    }
            }

            _log.Basic("Parsed packets to {0}", output);

            PrintStats(stats);

            return(0);
        }
Exemple #9
0
 public DumpReader(ProcessorConfig config)
     : base(config)
 {
     m_Reader = new PacketLogReader(new FileStream(config.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
 }
Exemple #10
0
        public static int Run(string[] args)
        {
            try
            {
                if (!HandleArguments(ref args))
                {
                    return(0);
                }
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                return(1);
            }

            if (args.Length != 1)
            {
                Console.WriteLine("Expected exactly one input file argument.");
                return(1);
            }

            Log.Level           = LogLevel.Debug;
            Log.TimestampFormat = "HH:mm:ss:fff";

            var color = Console.ForegroundColor;

            Log.Loggers.Add(new ConsoleLogger(false,
                                              color, color, color, color));

            if (!Debugger.IsAttached)
            {
                AppDomain.CurrentDomain.UnhandledException += UnhandledException;
            }

            var input   = args[0];
            var output  = _output ?? Path.ChangeExtension(input, "txt");
            var regexes = _regexes.Select(x => new Regex(x, RegexOptions))
                          .DefaultIfEmpty(new Regex(".*", RegexOptions))
                          .ToArray();

            _log.Basic("Parsing {0}...", input);

            var reader     = new PacketLogReader(input);
            var serializer = new PacketSerializer(
                new OpCodeTable(true, reader.Region),
                new OpCodeTable(false, reader.Region));
            var stats = new PacketStatistics();

            using (var result = new StreamWriter(new FileStream(output,
                                                                FileMode.Create, FileAccess.Write)))
            {
                foreach (var entry in reader.EnumerateAll())
                {
                    stats.TotalPackets++;

                    var name = serializer.GameMessages.OpCodeToName[entry.OpCode];

                    if (regexes.Any(r => !r.IsMatch(name)))
                    {
                        stats.IgnoredPackets++;

                        continue;
                    }

                    stats.RelevantPackets++;

                    result.WriteLine("[{0:yyyy-MM-dd HH:mm:ss:fff}] {1} {2}: {3} ({4} bytes)",
                                     entry.Timestamp.ToLocalTime(), entry.ServerName,
                                     entry.Direction.ToDirectionString(), name,
                                     entry.Payload.Count);

                    var parsed  = serializer.Create(entry.OpCode);
                    var payload = entry.Payload.ToArray();

                    if (payload.Length != 0)
                    {
                        if ((_hex == HexDumpMode.Unknown && parsed == null) ||
                            _hex == HexDumpMode.All)
                        {
                            result.WriteLine();
                            result.WriteLine(new RawPacket(name)
                            {
                                Payload = payload
                            });
                        }

                        if ((_analysis == AnalysisMode.Unknown && parsed == null) ||
                            _analysis == AnalysisMode.All)
                        {
                            var arrays = PacketAnalysis.FindArrays(payload);

                            if (arrays.Any())
                            {
                                result.WriteLine();
                                result.WriteLine("Potential arrays:");
                                result.WriteLine();

                                foreach (var arr in arrays)
                                {
                                    stats.PotentialArrays++;

                                    result.WriteLine(arr);
                                }
                            }

                            var strings = PacketAnalysis.FindStrings(payload,
                                                                     _whiteSpace, _control, _length);

                            if (strings.Any())
                            {
                                result.WriteLine();
                                result.WriteLine("Potential strings:");
                                result.WriteLine();

                                foreach (var str in strings)
                                {
                                    stats.PotentialStrings++;

                                    result.WriteLine(str);
                                }
                            }
                        }
                    }
                    else
                    {
                        stats.EmptyPackets++;
                    }

                    if (parsed != null)
                    {
                        stats.KnownPackets++;

                        if (_parse)
                        {
                            stats.ParsedPackets++;

                            for (var i = 0; i < _roundtrips + 1; i++)
                            {
                                serializer.Deserialize(payload, parsed);

                                var payload2 = serializer.Serialize(parsed);

                                Assert.Check(payload2.Length == payload.Length,
                                             "Payload lengths must match after roundtrip.");

                                if (i > 0)
                                {
                                    Assert.Check(payload2.SequenceEqual(payload),
                                                 "Payloads must match after first roundtrip.");
                                }

                                payload = payload2;
                            }

                            result.WriteLine();
                            result.WriteLine(parsed);
                        }
                    }
                    else
                    {
                        stats.UnknownPackets++;
                    }

                    result.WriteLine();
                }
            }

            _log.Basic("Parsed packets to {0}", output);

            if (_stats)
            {
                void PrintValue(string name, int value)
                {
                    _log.Info("{0,17}: {1}", name, value);
                }

                void PrintPacketValue(string name, int value)
                {
                    _log.Info("{0,17}: {1} ({2:P2})", name, value,
                              (double)value / stats.RelevantPackets);
                }

                PrintValue("Total packets", stats.TotalPackets);
                PrintPacketValue("Relevant packets", stats.RelevantPackets);
                PrintPacketValue("Ignored packets", stats.IgnoredPackets);
                PrintPacketValue("Empty packets", stats.EmptyPackets);
                PrintPacketValue("Unknown packets", stats.UnknownPackets);
                PrintPacketValue("Known packets", stats.KnownPackets);
                PrintPacketValue("Parsed packets", stats.ParsedPackets);
                PrintValue("Potential arrays", stats.PotentialArrays);
                PrintValue("Potential strings", stats.PotentialStrings);
            }

            return(0);
        }