1- from  os  import  _exit 
2- from  random  import  randint 
31import  socket 
4- import  threading 
52import  sys 
3+ import  threading 
64import  time 
5+ from  os  import  _exit 
6+ from  random  import  randint 
7+ 
78from  scapy .all  import  * 
9+ from  scapy .layers .inet  import  IP , UDP 
10+ 
811from  api  import  getLocalHostIP 
912
1013localHostIP  =  getLocalHostIP ()
1114localHostPort  =  randint (1024 , 65535 )
1215
1316try :
1417    TargetAddr  =  str (sys .argv [1 ])
18+     verboseMode  =  str (sys .argv [2 ])
1519except :
1620    TargetAddr  =  input ("Target IP: " )
21+     verboseMode  =  input ("Show verbose info(y/n): " )
22+ 
23+ try :
24+     timeout  =  int (sys .argv [3 ])
25+ except :
26+     timeout  =  None 
27+ 
28+ try :
29+     fileName  =  str (sys .argv [4 ])
30+ except :
31+     fileName  =  "" 
32+ 
1733motdData  =  b'\x01 \x00 \x00 \x00 \x00 $\r \x12 \xd3 \x00 \xff \xff \x00 \xfe \xfe \xfe \xfe \xfd \xfd \xfd \xfd \x12 4Vx\n ' 
1834serverCount  =  0 
1935
2036
21- def  sendPacket (startNum , count ):
37+ def  getIpList (ip : str ):
38+     ipList  =  []
39+     if  os .path .exists (TargetAddr ):
40+         with  open (TargetAddr , "r" ) as  file :
41+             for  ip  in  file .readlines ():
42+                 ipList .append (ip [:- 1 ])
43+             return  ipList 
44+     try :
45+         int (ip [- 1 ])
46+         # ip = "8.8.1-5.8-11" 
47+         processedIpSegs  =  [[], [], [], []]
48+         ipSegIndex  =  0 
49+         ipSegs  =  ip .split ("." )  # 8.8.0-255.0-5 -> [8, 8, 0-255, 0-5] 
50+         for  ipSeg  in  ipSegs :
51+             if  "-"  in  ipSeg :
52+                 segList  =  ipSeg .split ("-" )  # 0-5 -> [0, 5] 
53+                 # range(0, 5)  -> [0, 1, 2, 3, 4] 
54+                 for  i  in  range (int (segList [0 ]), int (segList [1 ])+ 1 ):
55+                     processedIpSegs [ipSegIndex ].append (str (i ))
56+             else :
57+                 processedIpSegs [ipSegIndex ].append (ipSeg )
58+             ipSegIndex  +=  1 
59+ 
60+         # [['8'], ['8'], ['1', '2', '3', '4'], ['8', '9', '10']] 
61+         for  processedIpSegA  in  processedIpSegs [0 ]:
62+             for  processedIpSegB  in  processedIpSegs [1 ]:
63+                 for  processedIpSegC  in  processedIpSegs [2 ]:
64+                     for  processedIpSegD  in  processedIpSegs [3 ]:
65+                         ipList .append (
66+                             f"{ processedIpSegA }  .{ processedIpSegB }  .{ processedIpSegC }  .{ processedIpSegD }  " )
67+         return  ipList 
68+     except :
69+         return  [ip ]
70+ 
71+ 
72+ def  sendPacket (startNum , count , ip ):
2273    port  =  startNum 
2374    while  True :
75+         if  stopThread :
76+             break 
2477        Time  =  time .strftime ('%H:%M:%S' )
25-         if  port  %  1000  ==  0 :
78+         if  port  %  1000  ==  0   and   verboseMode   ==   "y" :
2679            print (f"[{ Time }   I] Scaning port: { str (port )}   ~ { str (port  +  1000 )}  " )
27-         send (IP (src = localHostIP , dst = TargetAddr ) /  UDP (sport = localHostPort , dport = port ) / 
80+         send (IP (src = localHostIP , dst = ip ) /  UDP (sport = localHostPort , dport = port ) / 
2881             motdData ,
2982             verbose = False )
3083        if  port  ==  65535 :
31-             print (f"[{ Time }   I] Port { startNum }   ~ 65535 Done" )
84+             if  verboseMode  ==  "y" :
85+                 print (f"[{ Time }   I] Port { startNum }   ~ 65535 Done" )
3286            while  True :
87+                 if  threading .enumerate ().__len__ () ==  2 :
88+                     break 
3389                time .sleep (1 )
3490        elif  port  ==  startNum  +  count  -  1 :
35-             print (f"[{ Time }   I] Port { startNum }   ~ { startNum  +  count }   Done" )
36-             time .sleep (10 )
37-             print ("BE Server Count: "  +  str (serverCount ))
38-             print ("BDS Count: "  +  str (bdsCount ))
39-             print ("NK Count: "  +  str (nkCount ))
40-             print ("Geyser Count: "  +  str (geyserCount ))
41-             print ("Skipped Count: "  +  str (skipped ))
42-             print ("Error Count: "  +  str (error ))
43-             print ("Total Player Count: "  +  str (totalPlayerCount ))
44-             _exit (0 )
91+             if  verboseMode  ==  "y" :
92+                 print (f"[{ Time }   I] Port { startNum }   ~ { startNum  +  count }   Done" )
93+             break 
4594        port  +=  1 
4695
4796
48- t1  =  threading .Thread (target = sendPacket , args = (0 , 10000 ))
49- t2  =  threading .Thread (target = sendPacket , args = (10000 , 10000 ))
50- t3  =  threading .Thread (target = sendPacket , args = (20000 , 10000 ))
51- t4  =  threading .Thread (target = sendPacket , args = (30000 , 10000 ))
52- t5  =  threading .Thread (target = sendPacket , args = (40000 , 10000 ))
53- t6  =  threading .Thread (target = sendPacket , args = (50000 , 10000 ))
54- t7  =  threading .Thread (target = sendPacket , args = (60000 , 5535 ))
55- t1 .setDaemon (True )
56- t2 .setDaemon (True )
57- t3 .setDaemon (True )
58- t4 .setDaemon (True )
59- t5 .setDaemon (True )
60- t6 .setDaemon (True )
61- t7 .setDaemon (True )
62- t1 .start ()
63- t2 .start ()
64- t3 .start ()
65- t4 .start ()
66- t5 .start ()
67- t6 .start ()
68- t7 .start ()
97+ def  startThreads ():
98+     global  stopThread 
99+     ipList  =  getIpList (TargetAddr )
100+     for  ip  in  ipList :
101+         time .sleep (1 )
102+         stopThread  =  False 
103+         print ()
104+         print (
105+             f"[{ time .strftime ('%H:%M:%S' )}   I] Scaning target: { ip }  " )
106+         print ()
107+         t1  =  threading .Thread (target = sendPacket , args = (0 , 10000 , ip ))
108+         t2  =  threading .Thread (target = sendPacket , args = (10000 , 10000 , ip ))
109+         t3  =  threading .Thread (target = sendPacket , args = (20000 , 10000 , ip ))
110+         t4  =  threading .Thread (target = sendPacket , args = (30000 , 10000 , ip ))
111+         t5  =  threading .Thread (target = sendPacket , args = (40000 , 10000 , ip ))
112+         t6  =  threading .Thread (target = sendPacket , args = (50000 , 10000 , ip ))
113+         t7  =  threading .Thread (target = sendPacket , args = (60000 , 5535 , ip ))
114+         t1 .setDaemon (True )
115+         t2 .setDaemon (True )
116+         t3 .setDaemon (True )
117+         t4 .setDaemon (True )
118+         t5 .setDaemon (True )
119+         t6 .setDaemon (True )
120+         t7 .setDaemon (True )
121+         t1 .start ()
122+         t2 .start ()
123+         t3 .start ()
124+         t4 .start ()
125+         t5 .start ()
126+         t6 .start ()
127+         t7 .start ()
128+         tmpServerCount  =  serverCount 
129+         if  timeout :
130+             time .sleep (timeout )
131+         if  tmpServerCount  ==  serverCount :
132+             stopThread  =  True 
133+         while  threading .enumerate ().__len__ () !=  2 :  # main and itself 
134+             time .sleep (1 )
135+ 
136+     print ("BE Server Count: "  +  str (serverCount ))
137+     print ("BDS Count: "  +  str (bdsCount ))
138+     print ("NK Count: "  +  str (nkCount ))
139+     print ("Geyser Count: "  +  str (geyserCount ))
140+     print ("Skipped Count: "  +  str (skipped ))
141+     print ("Error Count: "  +  str (error ))
142+     print ("Total Player Count: "  +  str (totalPlayerCount ))
143+     _exit (0 )
144+ 
145+ 
146+ t  =  threading .Thread (target = startThreads )
147+ t .setDaemon (True )
148+ t .start ()
69149
70150bdsCount  =  0 
71151nkCount  =  0 
@@ -127,6 +207,10 @@ def sendPacket(startNum, count):
127207        totalPlayerCount  +=  int (infos [4 ])
128208        print (f"[{ date }   C] { str (serverCount )}  " )
129209        print (f"[{ date }   P] { totalPlayerCount }  " )
210+         if  fileName :
211+             with  open (fileName , "a+" ) as  file :
212+                 file .write (
213+                     f"{ date }   | { serverCount }   | { addr [0 ]}   | { addr [1 ]}   | { infos [1 ]}   | { infos [3 ]}   | { infos [4 ]} \n " )
130214        if  len (infos ) ==  10  or  len (infos ) ==  6 :
131215            nkCount  +=  1 
132216        elif  re .search (b"edicated" , data ):
@@ -136,7 +220,9 @@ def sendPacket(startNum, count):
136220        elif  re .search (b"eyser" , data ):
137221            geyserCount  +=  1 
138222        sk_rec .close ()
139-     except :
140-         print (f"[{ time .strftime ('%H:%M:%S' )}   R] An error occurred, skipped." )
223+     except  OSError :
224+         pass 
225+     except  Exception  as  info :
226+         print (f"[{ time .strftime ('%H:%M:%S' )}   R] { info }  , skipped." )
141227        error  +=  1 
142228        pass 
0 commit comments