donderdag 25 juni 2009

A handy script for Cell location

No rocket science, but a handy python script for locating a cell. Given the LAC and Cell id, the script will open your default browser and pinpoint the location on google maps. This is nice to have when you are investigating data from a SIM card and want to do a quick lookup of the LAC and CID stored in the SIM.
Where do I get the coordinates? Google has a hidden (or at least undocumented) API at http://www.google.com/glm/mmap which you can query using the correct packet format.
Here is the relevant part of the script. Drop me an e-mail if you are too lazy to construct a working script from this snippet and I will send you mine.

byteString = pack(">hqh2sh6sh5sh3sBiiihiiiiii",21,0,len(country),country,len(device), device, len("1.3.1"), "1.3.1", len("Web"), "Web", 27, 0, 0, 3, 0, int(CID), int(LAC), 0, 0, 0, 0);
contentLength = len(byteString);
contentType = "application/binary";
headers = {"Content-Length" : str(contentLength), "Content-Type" : contentType};
#Create the http request object and submit it to the URL
httpReq = Request(mmUrl, byteString, headers);
try:
httpResp = urlopen(httpReq);
except HTTPError as e:
sys.stdout.write("failed!");
return -1, -1;
except URLError as e:
sys.stdout.write("failed!");
return -1, -1;
else:
pass;
bytes = httpResp.read();
if(len(bytes) != 25): #likely an invalid CID + LAC was submissed
print("The server returned an unknown response("+str(bytes)+").");
return -1, -1;
(a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes);
latCrd = latitude / 1000000.0;
longCrd = longitude / 1000000.0;
return latCrd, longCrd;