using Ionic.Zlib; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Numerics; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; namespace MinecraftProtocol { enum Packet { SpawnEntity, SpawnExperienceOrb, SpawnWeatherEntity, SpawnLivingEntity, SpawnPainting, SpawnPlayer, EntityAnimation, Statistics, AcknowledgePlayerDigging, BlockBreakAnimation, BlockEntityData, BlockAction, BlockChange, BossBar, ServerDifficulty, ChatMessage, MultiBlockChange, TabComplete, DeclareCommands, WindowConfirmation, CloseWindow, WindowItems, WindowProperty, SetSlot, SetCooldown, PluginMessage, NamedSoundEffect, Disconnect, EntityStatus, Explosion, UnloadChunk, ChangeGameState, OpenHorseWindow, KeepAlive, ChunkData, Effect, Particle, UpdateLight, JoinGame, MapData, TradeList, EntityPosition, EntityPositionandRotation, EntityRotation, EntityMovement, VehicleMove, OpenBook, OpenWindow, OpenSignEditor, CraftRecipeResponse, PlayerAbilities, CombatEvent, PlayerInfo, FacePlayer, PlayerPositionAndLook, UnlockRecipes, DestroyEntities, RemoveEntityEffect, ResourcePackSend, Respawn, EntityHeadLook, SelectAdvancementTab, WorldBorder, Camera, HeldItemChange, UpdateViewPosition, UpdateViewDistance, DisplayScoreboard, EntityMetadata, AttachEntity, EntityVelocity, EntityEquipment, SetExperience, UpdateHealth, ScoreboardObjective, SetPassengers, Teams, UpdateScore, SpawnPosition, TimeUpdate, Title, EntitySoundEffect, SoundEffect, StopSound, PlayerListHeaderAndFooter, NBTQueryResponse, CollectItem, EntityTeleport, Advancements, EntityProperties, EntityEffect, DeclareRecipes, Tags } class Program { static void Main(string[] args) { #region Data Types byte[] Cut(byte[] _data, int startindex, int endindex) { byte[] result = new byte[1 + endindex - startindex]; for (int i = startindex; i <= endindex; i++) { result[i - startindex] = _data[i]; } return result; } byte[] Add(byte[] A, byte[] B) { byte[] result = new byte[A.Length + B.Length]; int counter = 0; for (int i = 0; i < A.Length; i++) { result[counter] = A[i]; counter++; } for (int i = 0; i < B.Length; i++) { result[counter] = B[i]; counter++; } return result; } byte[] ToVarInt(int value) { List<byte> result = new List<byte>(); do { byte temp = (byte)(value & 0x7f); // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone value >>= 7; if (value > 0) { temp |= 128; } result.Add(temp); } while (value > 0); return result.ToArray(); } int FromVarInt(ref BinaryReader read) { bool more = true; int value = 0; int shift = 0; while (more) { byte lower7bits = read.ReadByte(); more = (lower7bits & 128) != 0; value |= (lower7bits & 0x7f) << shift; shift += 7; } return value; } byte[] ToVarLong(long value) { List<byte> result = new List<byte>(); do { byte temp = (byte)(value & 0x7f); // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone value >>= 7; if (value > 0) { temp |= 128; } result.Add(temp); } while (value > 0); return result.ToArray(); } long FromVarLong(ref BinaryReader read) { bool more = true; long value = 0; int shift = 0; while (more) { byte lower7bits = read.ReadByte(); more = (lower7bits & 128) != 0; value |= (lower7bits & 0x7f) << shift; shift += 7; } return value; } ulong ToPosition(ulong x, ulong y, ulong z) { return ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF); } (ulong x, ulong y, ulong z) FromPosition(ulong value) { ulong x = value >> 38; ulong y = value & 0xFFF; ulong z = (value << 26 >> 38); return (x, y, z); } byte[] ToString(string text) { byte[] StringData = Encoding.UTF8.GetBytes(text); List<byte> result = new List<byte>(); result.AddRange(ToVarInt(StringData.Length)); result.AddRange(StringData); return result.ToArray(); } //string FromString(byte[] bytes) //{ // //} #endregion #region Packets byte[] HandshakePacket(string ip, ushort port, int ProtocolVersion, int NextState) { List<byte> Data = new List<byte>(); Data.AddRange(ToVarInt(ProtocolVersion)); Data.AddRange(ToString(ip)); Data.AddRange(BitConverter.GetBytes(port)); Data.Add((byte)NextState); var packet = BuildPacket(0x0, Data.ToArray()); return packet; } byte[] LoginStartPacket(string Nickname) { return BuildPacket(0x00, ToString(Nickname).ToArray()); } byte[] ChatMessagePacket(string Text) { return BuildUnCompressedPacket(0x03, ToString(Text)); } byte[] KeepAlivePacket(long KeepAliveID) { return BuildUnCompressedPacket(0x0F, BitConverter.GetBytes(KeepAliveID)); } byte[] DisconnectPacket(string Reason) { return BuildUnCompressedPacket(0x00,ToString(Reason)); } #endregion #region Methods string Gen(int length) { string ABC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; string result = ""; Random random = new Random(); for (int i = 0; i < length; i++) { result += ABC[random.Next(ABC.Length)]; } return result; } String MinecraftShaDigest(String input) { var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input)); // Reverse the bytes since BigInteger uses little endian Array.Reverse(hash); BigInteger b = new BigInteger(hash); // very annoyingly, BigInteger in C# tries to be smart and puts in // a leading 0 when formatting as a hex number to allow roundtripping // of negative numbers, thus we have to trim it off. if (b < 0) { // toss in a negative sign if the interpreted number is negative return "-" + (-b).ToString("x").TrimStart('0'); } else { return b.ToString("x").TrimStart('0'); } } byte[] BuildPacket(int id, byte[] Data) { List<byte> result = new List<byte>(); result.AddRange(ToVarInt(ToVarInt(id).Length + Data.Length)); result.AddRange(ToVarInt(id)); result.AddRange(Data); return result.ToArray(); } byte[] BuildCompressedPacket(int id, byte[] Data) { byte[] PacketID = ToVarInt(id); byte[] data = Data; byte[] DataLength = ToVarInt(0); byte[] CompressedPacketID = ZlibStream.CompressBuffer(PacketID); byte[] CompressedData = ZlibStream.CompressBuffer(Data); byte[] PacketLength = ToVarInt(DataLength.Length + CompressedPacketID.Length + CompressedData.Length); List<byte> Packet = new List<byte>(); Packet.AddRange(PacketLength); Packet.AddRange(DataLength); Packet.AddRange(CompressedPacketID); Packet.AddRange(CompressedData); return Packet.ToArray(); } byte[] BuildUnCompressedPacket(int id, byte[] Data) { List<byte> result = new List<byte>(); result.AddRange(ToVarInt(1 + ToVarInt(id).Length + Data.Length)); result.AddRange(ToVarInt(0)); result.AddRange(ToVarInt(id)); result.AddRange(Data); return result.ToArray(); } (int id,byte[] Data) ReadCompressedPacket(ref BinaryReader read) { int PacketLength = read.ReadByte(); int DataLength = read.ReadByte(); if (DataLength == 0) { byte[] result = read.ReadBytes(PacketLength - 1); return (result[0],Cut(result,1,result.Length-1)); } else { byte[] result = read.ReadBytes(DataLength); result = ZlibStream.UncompressBuffer(result); return (result[0], Cut(result, 1, result.Length - 1)); } } (int id, byte[] Data) ReadUnCompressedPacket(ref BinaryReader read) { int PacketLength = FromVarInt(ref read); byte[] result = read.ReadBytes(PacketLength - 1); return (result[0], Cut(result, 1, result.Length - 1)); } string PingLegacy(string ip, ushort port) { byte[] _data = { 0xfe, 1 }; TcpClient tcp = new TcpClient(ip, port); tcp.GetStream().Write(_data, 0, _data.Length); return new StreamReader(tcp.GetStream()).ReadToEnd(); } long SendPing(string ip, ushort port, int ProtocolVersion) { TcpClient client = new TcpClient(ip, port); byte[] Handshake = HandshakePacket(ip, port, ProtocolVersion, 1); client.GetStream().Write(Handshake, 0, Handshake.Length); byte[] Ping = BuildPacket(0x01, new byte[] { }); client.GetStream().Write(Ping, 0, Ping.Length); byte[] ReciveBuffer = new byte[10]; client.GetStream().Read(ReciveBuffer, 0, ReciveBuffer.Length); return BitConverter.ToInt64(ReciveBuffer, 2); } (string version, int versionNum, int MaxPlayers, int OnlinePlayers) SendRequest(string ip, ushort port, int ProtocolVersion) { TcpClient client = new TcpClient(ip, port); byte[] Handshake = HandshakePacket(ip, port, ProtocolVersion, 1); client.GetStream().Write(Handshake, 0, Handshake.Length); byte[] Status = BuildPacket(0x00, new byte[] { }); client.GetStream().Write(Status, 0, Status.Length); byte[] ReciveBuffer = new byte[32767]; client.GetStream().Read(ReciveBuffer, 0, ReciveBuffer.Length); string Request = Encoding.UTF8.GetString(ReciveBuffer).Replace("\0", "").Substring(4); string version = Request.Split(',')[0].Split(':')[2].Replace("\"", ""); int versionNum = int.Parse(Request.Split(',')[1].Split(':')[1].Replace("}", "")); int MaxPlayers = int.Parse(Request.Split(',')[2].Split(':')[2]); int OnlinePlayers = int.Parse(Request.Split(',')[3].Split(':')[1].Replace("}", "")); return (version, versionNum, MaxPlayers, OnlinePlayers); } #endregion TcpClient Client = new TcpClient("95.217.204.154", 25570); //Connect Console.WriteLine("Connect..."); NetworkStream stream = Client.GetStream(); BinaryReader reader = new BinaryReader(stream); BinaryWriter writer = new BinaryWriter(stream); writer.Write(HandshakePacket("95.217.204.154", 25570, 754, 2)); //Send Handshake Console.WriteLine("Send Handshake..."); writer.Write(LoginStartPacket("L"+new Random().Next(999).ToString())); //Send Login Start Console.WriteLine("Send Login Start..."); Thread.Sleep(500); string Password = new Random().Next(99999,999999).ToString(); writer.Write(ChatMessagePacket($@"/register {Password} {Password}")); Thread.Sleep(500); writer.Write(ChatMessagePacket("!Yes minus three, yoohoo <3")); Console.ReadLine(); } } } Код using Ionic.Zlib; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Numerics; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; namespace MinecraftProtocol { enum Packet { SpawnEntity, SpawnExperienceOrb, SpawnWeatherEntity, SpawnLivingEntity, SpawnPainting, SpawnPlayer, EntityAnimation, Statistics, AcknowledgePlayerDigging, BlockBreakAnimation, BlockEntityData, BlockAction, BlockChange, BossBar, ServerDifficulty, ChatMessage, MultiBlockChange, TabComplete, DeclareCommands, WindowConfirmation, CloseWindow, WindowItems, WindowProperty, SetSlot, SetCooldown, PluginMessage, NamedSoundEffect, Disconnect, EntityStatus, Explosion, UnloadChunk, ChangeGameState, OpenHorseWindow, KeepAlive, ChunkData, Effect, Particle, UpdateLight, JoinGame, MapData, TradeList, EntityPosition, EntityPositionandRotation, EntityRotation, EntityMovement, VehicleMove, OpenBook, OpenWindow, OpenSignEditor, CraftRecipeResponse, PlayerAbilities, CombatEvent, PlayerInfo, FacePlayer, PlayerPositionAndLook, UnlockRecipes, DestroyEntities, RemoveEntityEffect, ResourcePackSend, Respawn, EntityHeadLook, SelectAdvancementTab, WorldBorder, Camera, HeldItemChange, UpdateViewPosition, UpdateViewDistance, DisplayScoreboard, EntityMetadata, AttachEntity, EntityVelocity, EntityEquipment, SetExperience, UpdateHealth, ScoreboardObjective, SetPassengers, Teams, UpdateScore, SpawnPosition, TimeUpdate, Title, EntitySoundEffect, SoundEffect, StopSound, PlayerListHeaderAndFooter, NBTQueryResponse, CollectItem, EntityTeleport, Advancements, EntityProperties, EntityEffect, DeclareRecipes, Tags } class Program { static void Main(string[] args) { #region Data Types byte[] Cut(byte[] _data, int startindex, int endindex) { byte[] result = new byte[1 + endindex - startindex]; for (int i = startindex; i <= endindex; i++) { result[i - startindex] = _data[i]; } return result; } byte[] Add(byte[] A, byte[] B) { byte[] result = new byte[A.Length + B.Length]; int counter = 0; for (int i = 0; i < A.Length; i++) { result[counter] = A[i]; counter++; } for (int i = 0; i < B.Length; i++) { result[counter] = B[i]; counter++; } return result; } byte[] ToVarInt(int value) { List<byte> result = new List<byte>(); do { byte temp = (byte)(value & 0x7f); // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone value >>= 7; if (value > 0) { temp |= 128; } result.Add(temp); } while (value > 0); return result.ToArray(); } int FromVarInt(ref BinaryReader read) { bool more = true; int value = 0; int shift = 0; while (more) { byte lower7bits = read.ReadByte(); more = (lower7bits & 128) != 0; value |= (lower7bits & 0x7f) << shift; shift += 7; } return value; } byte[] ToVarLong(long value) { List<byte> result = new List<byte>(); do { byte temp = (byte)(value & 0x7f); // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone value >>= 7; if (value > 0) { temp |= 128; } result.Add(temp); } while (value > 0); return result.ToArray(); } long FromVarLong(ref BinaryReader read) { bool more = true; long value = 0; int shift = 0; while (more) { byte lower7bits = read.ReadByte(); more = (lower7bits & 128) != 0; value |= (lower7bits & 0x7f) << shift; shift += 7; } return value; } ulong ToPosition(ulong x, ulong y, ulong z) { return ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF); } (ulong x, ulong y, ulong z) FromPosition(ulong value) { ulong x = value >> 38; ulong y = value & 0xFFF; ulong z = (value << 26 >> 38); return (x, y, z); } byte[] ToString(string text) { byte[] StringData = Encoding.UTF8.GetBytes(text); List<byte> result = new List<byte>(); result.AddRange(ToVarInt(StringData.Length)); result.AddRange(StringData); return result.ToArray(); } //string FromString(byte[] bytes) //{ // //} #endregion #region Packets byte[] HandshakePacket(string ip, ushort port, int ProtocolVersion, int NextState) { List<byte> Data = new List<byte>(); Data.AddRange(ToVarInt(ProtocolVersion)); Data.AddRange(ToString(ip)); Data.AddRange(BitConverter.GetBytes(port)); Data.Add((byte)NextState); var packet = BuildPacket(0x0, Data.ToArray()); return packet; } byte[] LoginStartPacket(string Nickname) { return BuildPacket(0x00, ToString(Nickname).ToArray()); } byte[] ChatMessagePacket(string Text) { return BuildUnCompressedPacket(0x03, ToString(Text)); } byte[] KeepAlivePacket(long KeepAliveID) { return BuildUnCompressedPacket(0x0F, BitConverter.GetBytes(KeepAliveID)); } byte[] DisconnectPacket(string Reason) { return BuildUnCompressedPacket(0x00,ToString(Reason)); } #endregion #region Methods string Gen(int length) { string ABC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; string result = ""; Random random = new Random(); for (int i = 0; i < length; i++) { result += ABC[random.Next(ABC.Length)]; } return result; } String MinecraftShaDigest(String input) { var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input)); // Reverse the bytes since BigInteger uses little endian Array.Reverse(hash); BigInteger b = new BigInteger(hash); // very annoyingly, BigInteger in C# tries to be smart and puts in // a leading 0 when formatting as a hex number to allow roundtripping // of negative numbers, thus we have to trim it off. if (b < 0) { // toss in a negative sign if the interpreted number is negative return "-" + (-b).ToString("x").TrimStart('0'); } else { return b.ToString("x").TrimStart('0'); } } byte[] BuildPacket(int id, byte[] Data) { List<byte> result = new List<byte>(); result.AddRange(ToVarInt(ToVarInt(id).Length + Data.Length)); result.AddRange(ToVarInt(id)); result.AddRange(Data); return result.ToArray(); } byte[] BuildCompressedPacket(int id, byte[] Data) { byte[] PacketID = ToVarInt(id); byte[] data = Data; byte[] DataLength = ToVarInt(0); byte[] CompressedPacketID = ZlibStream.CompressBuffer(PacketID); byte[] CompressedData = ZlibStream.CompressBuffer(Data); byte[] PacketLength = ToVarInt(DataLength.Length + CompressedPacketID.Length + CompressedData.Length); List<byte> Packet = new List<byte>(); Packet.AddRange(PacketLength); Packet.AddRange(DataLength); Packet.AddRange(CompressedPacketID); Packet.AddRange(CompressedData); return Packet.ToArray(); } byte[] BuildUnCompressedPacket(int id, byte[] Data) { List<byte> result = new List<byte>(); result.AddRange(ToVarInt(1 + ToVarInt(id).Length + Data.Length)); result.AddRange(ToVarInt(0)); result.AddRange(ToVarInt(id)); result.AddRange(Data); return result.ToArray(); } (int id,byte[] Data) ReadCompressedPacket(ref BinaryReader read) { int PacketLength = read.ReadByte(); int DataLength = read.ReadByte(); if (DataLength == 0) { byte[] result = read.ReadBytes(PacketLength - 1); return (result[0],Cut(result,1,result.Length-1)); } else { byte[] result = read.ReadBytes(DataLength); result = ZlibStream.UncompressBuffer(result); return (result[0], Cut(result, 1, result.Length - 1)); } } (int id, byte[] Data) ReadUnCompressedPacket(ref BinaryReader read) { int PacketLength = FromVarInt(ref read); byte[] result = read.ReadBytes(PacketLength - 1); return (result[0], Cut(result, 1, result.Length - 1)); } string PingLegacy(string ip, ushort port) { byte[] _data = { 0xfe, 1 }; TcpClient tcp = new TcpClient(ip, port); tcp.GetStream().Write(_data, 0, _data.Length); return new StreamReader(tcp.GetStream()).ReadToEnd(); } long SendPing(string ip, ushort port, int ProtocolVersion) { TcpClient client = new TcpClient(ip, port); byte[] Handshake = HandshakePacket(ip, port, ProtocolVersion, 1); client.GetStream().Write(Handshake, 0, Handshake.Length); byte[] Ping = BuildPacket(0x01, new byte[] { }); client.GetStream().Write(Ping, 0, Ping.Length); byte[] ReciveBuffer = new byte[10]; client.GetStream().Read(ReciveBuffer, 0, ReciveBuffer.Length); return BitConverter.ToInt64(ReciveBuffer, 2); } (string version, int versionNum, int MaxPlayers, int OnlinePlayers) SendRequest(string ip, ushort port, int ProtocolVersion) { TcpClient client = new TcpClient(ip, port); byte[] Handshake = HandshakePacket(ip, port, ProtocolVersion, 1); client.GetStream().Write(Handshake, 0, Handshake.Length); byte[] Status = BuildPacket(0x00, new byte[] { }); client.GetStream().Write(Status, 0, Status.Length); byte[] ReciveBuffer = new byte[32767]; client.GetStream().Read(ReciveBuffer, 0, ReciveBuffer.Length); string Request = Encoding.UTF8.GetString(ReciveBuffer).Replace("\0", "").Substring(4); string version = Request.Split(',')[0].Split(':')[2].Replace("\"", ""); int versionNum = int.Parse(Request.Split(',')[1].Split(':')[1].Replace("}", "")); int MaxPlayers = int.Parse(Request.Split(',')[2].Split(':')[2]); int OnlinePlayers = int.Parse(Request.Split(',')[3].Split(':')[1].Replace("}", "")); return (version, versionNum, MaxPlayers, OnlinePlayers); } #endregion TcpClient Client = new TcpClient("95.217.204.154", 25570); //Connect Console.WriteLine("Connect..."); NetworkStream stream = Client.GetStream(); BinaryReader reader = new BinaryReader(stream); BinaryWriter writer = new BinaryWriter(stream); writer.Write(HandshakePacket("95.217.204.154", 25570, 754, 2)); //Send Handshake Console.WriteLine("Send Handshake..."); writer.Write(LoginStartPacket("L"+new Random().Next(999).ToString())); //Send Login Start Console.WriteLine("Send Login Start..."); Thread.Sleep(500); string Password = new Random().Next(99999,999999).ToString(); writer.Write(ChatMessagePacket($@"/register {Password} {Password}")); Thread.Sleep(500); writer.Write(ChatMessagePacket("!Yes minus three, yoohoo <3")); Console.ReadLine(); } } } Лишний код можно удалить. Практиковался в веб программировании, надеюсь кому нибудь пригодится)