Bytescanner api improved

Iniciado por Sanko, Septiembre 20, 2013, 11:28:05 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Septiembre 20, 2013, 11:28:05 AM Ultima modificación: Marzo 16, 2014, 08:55:59 PM por Expermicid
Hace unos dias me dio por pegarle una ojeada a la "api" en python de bytescanner y le hice unas cuantas mejoras al code, aqui les dejo el mio y abajo el code original :

Código: python
#ByteScanner "API" coded by YAKIT
#code debugging / improved code / by Sanko

import pycurl, cStringIO, errno, sys, hashlib, json

#Defining Buffers
storage = cStringIO.StringIO()
PostFileStorage = cStringIO.StringIO()
ScanResultStorage = cStringIO.StringIO()
ScanHistoryStorage = cStringIO.StringIO()
ChangeLogStorage = cStringIO.StringIO()
AVersionStorage = cStringIO.StringIO()

#Defining curl
curl = pycurl.Curl()
curl.setopt(curl.WRITEFUNCTION, storage.write)
curl.setopt(curl.COOKIEFILE, '')

class bs_Api():

def __init__(self):
"""
self.__login('[email protected]', 'yourpassword')
self.__upload_file('test.exe')
self.__Result_ID(id)
self.__Scan_History()
self.__Change_Log()
self.__Check_AV_Version()
self.__Export_to('extension', id)
"""
def __login(self, email, password):
#trying to log in
try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/client2api.php')
curl.setopt(curl.POSTFIELDS, 'action=Auth&email=%s&password=%s'%(email, password))
curl.perform()
#storing the json data returned like login_data
self.login_data = json.loads(storage.getvalue())

except:
print "An error has ocurred triying to log in bytescanner"

#checking if you are logged
if self.login_data['success'] == False:
print ("[x] "+self.login_data['error']['title'] + "\tReason: " + self.login_data['error']['reason'])
sys.exit(errno.EACCES)

self.dicc_login = {"ID":self.login_data['root'][0]['id'],
   "Email":self.login_data['root'][0]['email'],
   "Creation Date":self.login_data['root'][0]['created'],
   "Last Login":self.login_data['root'][0]['lastlogin'],
   "Last IP Logged":self.login_data['root'][0]['lastloginip']}
#print self.dicc_login

def __upload_file(self, sFilename):
#reading the file to upload
try:
with open(sFilename): pass

except IOError:
print "Unable to open %s" % sFilename

postData=[('action', 'BinaryFileTransfer'),
  ('binaryFile', (pycurl.FORM_FILE, sFilename))]
 
try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/startup.php')
curl.setopt(curl.HTTPPOST, postData)
curl.setopt(curl.WRITEFUNCTION, PostFileStorage.write)
curl.perform()
self.upload_data = json.loads(PostFileStorage.getvalue())

except:
print "An error has ocurred uploading the file"

#checking
if self.upload_data['success'] != True:
print ("[x] "+self.upload_data['error']['title'] + "\tReason: " + self.upload_data['error']['reason'])
sys.exit(errno.EACCES)

self.dicc_upload = {"Result ID":str(self.upload_data['root']),
"MD5":hashlib.md5(open(sFilename).read()).hexdigest(),
"SHA1":hashlib.sha1(open(sFilename).read()).hexdigest()}
#print self.dicc_upload

def __Result_ID(self, r_ID):
#obtaining json result
try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/api2client.php?action=scanResult&ResultID=%s'% r_ID)
curl.setopt(curl.WRITEFUNCTION, ScanResultStorage.write)
curl.perform()
self.result_data = json.loads(ScanResultStorage.getvalue())

except:
print "An error has ocurred obtaining the json data"

#checking
if self.result_data['success'] != True:
print ("[x] "+self.result_data['error']['title'] + "\tReason: " + self.result_data['error']['reason'])
sys.exit(errno.EACCES)

#print self.result_data

def __Scan_History(self):
try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/api2client.php?action=ScanHistory')
curl.setopt(curl.WRITEFUNCTION, ScanHistoryStorage.write)
curl.perform()
self.history_data = json.loads(ScanHistoryStorage.getvalue())

except:
print "An error has ocurred obtaining your Scan History"

#print self.history_data

def __Change_Log(self):
try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/api2client.php?action=ChangeLog')
curl.setopt(curl.WRITEFUNCTION, ChangeLogStorage.write)
curl.perform()
self.ChangeLog_data = json.loads(ChangeLogStorage.getvalue())

except:
print "An error has ocurred obtaining the ChangeLog"

#print self.ChangeLog_data

def __Check_AV_Version(self):
try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/api2client.php?action=AvVersion')
curl.setopt(curl.WRITEFUNCTION, AVersionStorage.write)
curl.perform()
self.AVersion_data = json.loads(AVersionStorage.getvalue())

except:
print "An error has ocurred obtaining the ChangeLog"

#print self.AVersion_data

def __Export_to(self, ext_to, r_ID):
buf = cStringIO.StringIO()
ext_dicc = {'png':'Png', 'xls':'Excel', 'pdf':'Pdf'}

try:
curl.setopt(curl.URL, 'http://www.bytescanner.com/export4client.php?action=Export2%s&ResultID=%s' %(ext_dicc[ext_to], r_ID))
curl.setopt(curl.WRITEFUNCTION, buf.write)
f = open('bs_report.%s'%(ext_to), 'w')
f.write(curl.perform())
f.close()

except:
print "An error has ocurred saving the %s report" % ext_dicc[ext_to]


code original:
Código: python
#Author: Ersan YAKIT
#Email : [email protected]

import pycurl, json
import cStringIO
import errno, sys

storage = cStringIO.StringIO()
PostFileStorage = cStringIO.StringIO()
ScanResultStorage = cStringIO.StringIO()

BYTESCANNER_EMAIL= "[email protected]"
BYTESCANNER_PASSWORD= "yourbytescannerpassword"

c = pycurl.Curl()
c.setopt(c.WRITEFUNCTION, storage.write)
c.setopt(c.COOKIEFILE, '') #we want to store cookie into memory

#STEP I ( Login into system )
c.setopt(c.URL, 'http://www.bytescanner.com/client2api.php')
c.setopt(c.POSTFIELDS, 'action=Auth&email='+BYTESCANNER_EMAIL+'&password='+ BYTESCANNER_PASSWORD)
c.perform()

data = json.loads(storage.getvalue())

if data['success'] != True:
print ("[x] "+data['error']['title'] + "\tReason: " + data['error']['reason'])
sys.exit(errno.EACCES)

print ("[+] Logged In\n")
print("Welcome, "+data['root'][0]['email']+"\nLast Login: "+ data['root'][0]['lastlogin']+"\nLast Login Ip: "+ data['root'][0]['lastloginip']+"\n" )


#STEP II ( Get File Name from user. )
scanFile= raw_input("Enter Scan File Path: ")
try:
with open(scanFile): pass
except IOError:
print 'Unable to open :'+scanFile
print("[+] Transfering file "+ scanFile +"\n")
## sending File To server!
postData=[
('action', 'BinaryFileTransfer'),
('binaryFile', (pycurl.FORM_FILE, scanFile))
]
c.setopt(c.URL, 'http://www.bytescanner.com/startup.php')
c.setopt(c.HTTPPOST, postData)
c.setopt(c.WRITEFUNCTION, PostFileStorage.write)
c.perform()
data = json.loads(PostFileStorage.getvalue())
if data['success'] != True:
print ("[x] "+data['error']['title'] + "\tReason: " + data['error']['reason'])
sys.exit(errno.EACCES)
print("[+] File transfer completed. Result ID: "+str(data['root']) )

#STEP III recieve scan results
print("[+] Starting request for scan results...\n");

c.setopt(c.URL, 'http://www.bytescanner.com/api2client.php?action=scanResult&ResultID='+str(data['root']) )
c.setopt(c.WRITEFUNCTION, ScanResultStorage.write)
c.perform()

data = json.loads(ScanResultStorage.getvalue())
if data['success'] != True:
print ("[x] "+data['error']['title'] + "\tReason: " + data['error']['reason'])
sys.exit(errno.EACCES)
print ('{0:30}\t{1:40}\t{2:30}\t{3:30}\n'.format("Antivirus Provider", "Antivirus Ver", "Scan Time", "Scan Result"))

for tmpIndex in data['root']:
print ('{0:30}\t{1:40}\t{2:30}\t{3:30}'.format(
tmpIndex['malwareScanProvider'], 
tmpIndex['malwareScanProviderVer'],
tmpIndex['malwareScanProviderResultTime'], tmpIndex['malwareScanProviderResult']
))

storage.close()
PostFileStorage.close()
ScanResultStorage.close()
print("[+] Scan Completed...\n")


PD : EL retorno se almacena en un diccionario, asi que cuando importen la clase acuerdense de parsear la informacion.
Saludos
Sigueme en Twitter : @Sankosk
Estos nuevos staff no tienen puta idea XD

Hey esta muy bueno tu aporte... Ojala alguien hiciera eso con scapy o un tuto