Differences between version 2 and previous revision of CyberLeo/Scraps/cocknocker.
Other diffs: Previous Major Revision, Previous Author
| Newer page: | version 2 | Last edited on Tuesday, 29 May 2012 7:10:38 | by CyberLeo | Revert | 
| Older page: | version 1 | Last edited on Tuesday, 29 May 2012 7:05:47 | by CyberLeo | Revert | 
@@ -2,9 +2,9 @@
  
  
 require 'openssl' 
 require 'socket' 
 require 'zlib' 
-bad
_ports 
= [ 0, 25, 135, 139, 445 ] 
+BAD
_PORTS 
= [ 0, 25, 135, 139, 445 ] 
 def packet(generation, ip, time) 
   a = [ generation ] 
   a.concat(ip.split('.').map(&:to_i)) 
@@ -39,20 +39,24 @@
  
  
  
 def check(packet, bad_ports) 
   bad_ports - packet == bad_ports 
 end 
-key = OpenSSL::PKey::RSA.new
(192
)  
-ip = '127.0.0.1' 
 
-
time = Time.now.to_i 
+def knock
(ip, key
)  
+  
time = Time.now.to_i 
 
+  generation = 0 
 
+  packet = nil 
 
+  until packet && check(packet, BAD_PORTS) 
 
+    packet = fmt(encrypt(packet(generation, ip, time), key)) 
 
+    generation += 1 
 
+  end
 
-packet 
= nil
  
-generation = 0 
 
-until 
packet && check(packet, bad_ports)
  
-  packet = fmt
(encrypt(packet(generation
, ip, time), key)
)  
-  generation += 1
 
+  sock 
= UDPSocket.new
  
+  
packet.each {|port|
  
+    sock.send
('', 0
, ip, port
)  
+  }
 
 end 
-sock 
= UDPSocket
.new  
-packet.each {|port| 
 
-  sock.send('', 0, 
'10
.0.0.1', port
) 
 
-}
 
+key 
= OpenSSL::PKey::RSA
.new(192)
  
+ip = 
'127
.0.0.1' 
 
+ 
 
+knock(ip
) 
 </code> 
version 2
require 'openssl'
require 'socket'
require 'zlib'
BAD_PORTS = [ 0, 25, 135, 139, 445 ]
def packet(generation, ip, time)
  a = [ generation ]
  a.concat(ip.split('.').map(&:to_i))
  a << time
  p = a.pack('C5N')
  a << Zlib.crc32(p)
  a.pack('C5NN')
end
def encrypt(packet, key)
  key.private_encrypt(packet)
end
def fmt(packet)
  out = []
  idx = 0
  stack = packet.unpack('C*')
  while a = stack.shift
    b = stack.shift
    c = stack.shift
    one = ( a << 4 ) + ( b & 0xf )
    two = ( ( b & 0x0f ) << 8 ) + c
    out << ( ( one << 4 ) + idx )
    idx += 1
    out << ( ( two << 4 ) + idx )
    idx += 1
  end
  out
end
# Returns true if the packet contains no bad ports; false if it does
def check(packet, bad_ports)
  bad_ports - packet == bad_ports
end
def knock(ip, key)
  time = Time.now.to_i
  generation = 0
  packet = nil
  until packet && check(packet, BAD_PORTS)
    packet = fmt(encrypt(packet(generation, ip, time), key))
    generation += 1
  end
  sock = UDPSocket.new
  packet.each {|port|
    sock.send('', 0, ip, port)
  }
end
key = OpenSSL::PKey::RSA.new(192)
ip = '127.0.0.1'
knock(ip)
