# iPodHE Server Clone
# (c) Kohr-Ah Productions - Jan. 2007
#
# Usage:
#	1. Edit your hostfile (in %windir%\system32\drivers\etc\hosts) and add the following line:
#	   127.0.0.1	activation.yeda.co.il
#   2. Make sure that you don't have any webserver or other program that listens on port 80 currently running
#	3. Run server.py
#	4. Run iPodHEInstall.exe and follow the instructions
#	5. Your'e done!

import socket

def GenerateCode(Serial):
	Out = ""
	j = 0
	for i in xrange(len(Serial)):
		r = len(Serial)&3 
		if ((i % r) == 0):
			r = i
			if (i == 0):
				r = 1
			Out += chr(((ord(Serial[i]) + j)%r) + i + ord('A'))
			j+=1
		r = i + j
		if (r == 0):
			r = 1
		Out += chr(ord('Z') - ((ord(Serial[i]) - i)%r))
		j += 1
		i += 1
	Out = Out[::-1]

	return "6" + Out[:6] + "7" + Out[6:12] + "8" + Out[12:] 

data_packet = "\x03\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x50\x6f\x64\x69\x75\x6d\x20\x53\x61\x6e\x73\x10\x00\x00\x00\x86\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00\x43\x68\x69\x63\x61\x67\x6f\x0c\x00\x00\x00\x83\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00\x43\x68\x69\x63\x61\x67\x6f\x0c\x00\x00\x00\x84\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x50\x6f\x64\x69\x75\x6d\x20\x53\x61\x6e\x73\x0e\x00\x00\x00\x85\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x09\x00\x00\x00\x45\x73\x70\x79\x20\x53\x61\x6e\x73\x0a\x00\x00\x00\x87\x00\x00\x00\x03\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00\x50\x6f\x64\x69\x75\x6d\x20\x53\x61\x6e\x73\x12\x00\x00\x00\x88\x00\x00\x00"

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("0.0.0.0", 1911))
serversocket.listen(1)

print "[+] Listening for incoming connections..."

(clientsocket, address) = serversocket.accept()

print "[+] Got connection from %s" % address[0]

protocol_info = clientsocket.recv(20)
print "[+] Recieved protocol info: %s" % protocol_info

serial_no = clientsocket.recv(12)
print "[+] Recieved ipod serial: %s" % serial_no

response_code = GenerateCode(serial_no.rstrip("\x00"))

print "[+] Sending code... %s" % response_code
clientsocket.send("CODE:" + response_code + "\x00")

print "[+] Sending data..."
clientsocket.send(data_packet)

print "[+] Closing connection..."
clientsocket.close()
