Public paste
lol
By: lal | Date: Jan 14 2008 19:52 | Format: None | Expires: never | Size: 3.26 KB | Hits: 807

  1. import socket
  2. import struct
  3. class Network(object):
  4.     def __init__(self, sock=None):
  5.         if sock is None:
  6.             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7.         else :
  8.             self.sock = sock
  9.  
  10.         self.NetworkBuffer = ''
  11.         self.SpawnBuffer1 = None
  12.         self.SpawnBuffer2 = None
  13.        
  14.     def Connect(self, host, port):
  15.         self.sock.connect((host, port))
  16.  
  17.     def Send(self, Message):
  18.         TotalSent = 0
  19.         MessageLength = len(Message)
  20.         while TotalSent < MessageLength:
  21.             Sent = self.sock.send(Message[TotalSent:])
  22.             if Sent == 0:
  23.                 raise RuntimeError, "socket connection broken"
  24.             TotalSent = TotalSent + Sent
  25.    
  26.     def InjectPacket (self, sOpcode, sData, blnToServer, blnEncrypted):
  27.         sLen = struct.pack('<h',len(sData)/2).encode('hex-codec')
  28.         sOpcode = sOpcode[len(sOpcode)-2:] + sOpcode[:2]
  29.         print "sOpcode: " + sOpcode
  30.         if blnEncrypted == True:
  31.             if blnToServer == True:
  32.                 sPacket = sLen + sOpcode + "0300" + sData
  33.             else :
  34.                 sPacket = sLen + sOpcode + "0400" + sData
  35.         else :
  36.             if blnToServer == True:
  37.                 sPacket = sLen + sOpcode + "0100" + sData
  38.             else :
  39.                 sPacket = sLen + sOpcode + "0200" + sData
  40.         self.Send(sPacket.decode('hex-codec'))
  41.  
  42.     def PeekLength(self, sData):
  43.         try:
  44.             iLength = long(ord(sData[1:2])) * 256 + long(ord(sData[0:1]))
  45.             iLength = iLength + 6
  46.         except:
  47.             iLength = 0
  48.         return iLength
  49.                        
  50.     def EmptyBuffer(self):
  51.         rData = self.sock.recv(4096)
  52.         if rData == 0:
  53.             return 1
  54.         rData = self.NetworkBuffer + rData
  55.         while True:
  56.             iLength = self.PeekLength(rData)
  57.             sData = rData[:iLength]
  58.             if len(sData) == iLength:
  59.                 if not sData == '':
  60.                     self.rcvPacket(sData)
  61.                     rData = rData[iLength:]
  62.             if iLength == 0:
  63.                 break
  64.         self.NetworkBuffer = rData
  65.  
  66.     def rcvPacket(self, sData):
  67.         if ord(sData[4:5]) == 1:
  68.             bDirection = False
  69.         else :
  70.             bDirection = True
  71.  
  72.         sOpcode = self.StringToHex(sData[2:4])
  73.         sOpcode = sOpcode[2:] + sOpcode[:2]
  74.         sData = self.StringToHex(sData[6:])
  75.         sOpcode = sData[3:4] + sData[2:3]
  76.         sData = sData[6:]
  77.         #Here you can process the received packets
  78.         #The opcode is hex encoded, but the sData isn't.
  79.         #If you want sData to be hex encoded, just uncomment the next line
  80.         #sData = sData.encode('hex-codec')
  81.  
  82.     def InjectNotice(self, sMessage):
  83.         sData = struct.pack('<b h', 7, len(sMessage))
  84.         sText = ''
  85.         for letter in sMessage:
  86.             sText += letter.encode('hex-codec') + "00"
  87.         sPacket = sData.encode('hex-codec') + sText
  88.         self.InjectPacket('3667', sPacket, False, True)
  89.  
  90.  
  91. network = Network()
  92. network.Connect("127.0.0.1", 22580) #connect to nuConnector
  93. # while loop to keep emptying the network buffers.
  94. while 1:
  95.     Message = raw_input("Please enter a Notice.")
  96.     network.InjectNotice(Message)