def d_headers(): fpr('Diagnostics: Header decoder') print if raw_input(' Would you like to decode some headers [y/N]> ') in [ 'y', 'Y' ]: print fpr('Please provide an encoded part of the header:') #ehead = raw_input(' ') fpr('Use Ctrl-D with new line to continue.') fpr.blue('_' * (tc - 4)) print inputs = get_inputs(nl=True) fpr.info('_' * (tc - 4)) # decode multiline email headers ehead = coders.decode_mheaders(inputs) waitin() if ehead.keys(): fpr('Decoded Headers') fpr.info('_' * (tc - 4)) #print for k in ehead.keys(): print dhead, enc = decode_header(''.join(ehead[k]).replace( '?==?', '?= =?'))[0] if re.match('-no-hn-', k): fpr('Header-name: <None>') else: fpr('Header-Name: %s' % k) fpr('Header: %s' % dhead) ##print dhead fpr('Encoding: %s' % enc) print fpr.blue('_' * (tc - 4)) waitin() # _end_of_decode_multiline_headers else: fpr('Sorry! No header to decode') waitin() ehead.clear() else: waitin()
def parse_spfHeaders(h): if h: print h try: q = spf.query('0.0.0.0', '', '') q.mechanism = 'unknown' p = q.parse_header(h) ph = q.get_header(q.result, **p) print print p['identity'] fpr.blue(str(p)) fpr.warn(str(ph)) except Exception, e: fpr.err("Error: %s" % e)
def bancls2(thr=[],i=1,opt='replay'): bancls() def isActive(t): # print t.getName() if t.isAlive(): return fpr.CYAN+'Active'+fpr.RCL elif t.getName() in smtp[opt]['threads']['fail']: return fpr.RED+'Failed'+fpr.RCL else: return fpr.GREEN+'Finish'+fpr.RCL if thr: #main_thread = threading.currentThread() #for t in threading.enumerate(): # print 'Threads: Total ',len(thr) ,' Active ',len(threading.enumerate()),' Finish ',len(thr)-len(threading.enumerate()) # remember about MainThread, so -1 or +1 :) print ' Threads: Total ',fpr.GREEN,len(thr),fpr.RCL, \ ' Active ',fpr.CYAN,(len(threading.enumerate())-1),fpr.RCL, \ ' Finish ',fpr.GREEN,len(thr)-len(threading.enumerate())+1,fpr.RCL, \ ' Success ',fpr.GREEN,len(smtp[opt]['threads']['ok']),fpr.RCL, \ ' Failed',fpr.RED,len(smtp[opt]['threads']['fail']),fpr.RCL fpr.blue('_'*(tc-4)) print status = '' line = 80 s = 0 for t in thr: mlen = 14 #print (line % (s+mlen+1) ) if (line % (s+mlen+1) ) < line: status += " %-4s %s "% (t.getName(), isActive(t)) s += mlen + 1 else: status += "\n %-4s %s " % (t.getName(), isActive(t)) s = mlen + 1 #print s print '%s' % status fpr.blue('_'*(tc-4))
def viewRawContent(raw): dl = len(raw.splitlines()) if dl > tr: fpr('Content data is bigger than your screen (%s lines)' % dl) if raw_input( ' Would like to use system pager to view it [y/N]:> ') in [ 'y', 'Y' ]: fpr.blue('_' * (tc - 4)) print pager = Less(num_lines=tr) print raw | pager else: fpr.blue('_' * (tc - 4)) print fpr('%r' % raw) else: fpr.blue('_' * (tc - 4)) print fpr('%r' % raw) fpr.blue('_' * (tc - 4))
def viewTheMsg(s=0): # from core.ui.cless import Less # from core.msg.builder import msg_builder # try: # from cStringIO import StringIO # except: # from StringIO import StringIO # fs = StringIO() # # from email.MIMEBase import MIMEBase # from email.iterators import _structure # from email.message import Message msgdata = msg_builder() if DEBUG > 1: from pprint import pprint print '----' pprint(msgdata) print 'type:', type(msgdata) print 'repr:', repr(msgdata) print 'dir :', dir(msgdata) print '__len__():', msgdata.__len__() # no. of headers print '__str__():', msgdata.__str__() print 'len():', len(msgdata) print msgdata.get_payload() print '----' if hasattr(msgdata, 'as_string'): fpr.blue('_' * (tc - 4)) print #if msgdata != MIMEBase: if True: # view the message mime structure if s: #email.iterators._structure(msgdata, fs) if hasattr(msgdata, 'get_content_typ'): _structure(msgdata, fs) #fpr.blue('%s' % email.iterators._structure(msgdata) ) fpr.warn('%s' % fs.getvalue()) else: fpr.warn( 'For custom message currently MIME structure is not checked !' ) # view message data else: #print fpr.GRAY #dl = len(str(msgdata).splitlines()) dl = len(msgdata.as_string(unixfrom=False).splitlines()) if dl > tr: fpr('Message data is bigger than your screen (%s lines)' % dl) if raw_input( ' Would like to use system pager to view it [y/N]:> ' ) in ['y', 'Y']: fpr.blue('_' * (tc - 4)) print pager = Less(num_lines=tr) print msgdata.as_string(unixfrom=False) | pager else: fpr.blue('_' * (tc - 4)) print print msgdata.as_string(unixfrom=False) else: print msgdata.as_string(unixfrom=False) #print len(str(msgdata).splitlines()),tr #print msgdata #print fpr.RCL #else: # fpr.info('No content data was defined !') # if msgdata == Message: # print msgdata # elif smtp['use_mime'] == 0: # if s: # dbginfo('warn','MIME structure view is not supprted for customized content!') # # dl = len(msgdata.as_string(unixfrom=False).splitlines()) # print msgdata.as_string(unixfrom=False) # print 'len ', dl fpr.blue('_' * (tc - 4)) else: fpr.warn('No content data was defined !') fs.close()
def viewSmtpConv(s): #import re #import sys #from StringIO import StringIO #from core.ui.cless import Less if s: if raw_input( ' Would you like to view SMTP conversation [y/N]:> ') not in [ 'y', 'Y' ]: return True else: print dl = len(s.splitlines()) for line in s.splitlines(): if len(line) > tc: dl += len(line) / tc if dl > tr: fpr('SMTP conversation logs are much bigger than your screen (%s lines)' % dl) if raw_input( ' Would like to use system pager to view it [y/N]:> ') in [ 'y', 'Y' ]: fpr.blue('_' * (tc - 4)) print # use Less if output is much bigger than one screen pager = Less(num_lines=tr) # fetch output from fancy print as Less input must be a string # make copy of original stdout han redirect stdout to string sto = sys.stdout sys.stdout = StringIO() for line in s.splitlines(): if re.search('^send:', line): fpr.cyan(line) elif re.search('^reply:', line): fpr.green(line) elif re.search('^data:', line): fpr.purple(line) else: fpr.info(line) x = sys.stdout.getvalue() sys.stdout.close() sys.stdout = sto print x | pager return True #else: # fpr.blue( '_'*(tc-4)) # print # fpr.blue( '_'*(tc-4)) #else: fpr.blue('_' * (tc - 4)) print for line in s.splitlines(): if re.search('^send:', line): fpr.cyan(line) elif re.search('^reply:', line): fpr.green(line) elif re.search('^data:', line): fpr.purple(line) else: fpr.info(line) fpr.blue('_' * (tc - 4))
def att_custom_header(): #import re print if raw_input(' Would you like to include your own headers [y/N]> ') in [ 'y', 'Y' ]: while True: print fpr('Please provide a header in format: >> Header-name: header-value <<' ) fpr('Use Ctrl-D with new line to save it and continue.') fpr.blue('_' * (tc - 4)) print # inputs = [] # while True: # try: # line = raw_input("") # except EOFError: # break # inputs.append(line) # to preserve new line pass nl=True with get_inputs() inputs = get_inputs() if inputs != '': thead = {} ####### TODO: move this to def encode_mheaders() for (i, hline) in enumerate(inputs): #print hline m1 = re.match(r'(.*):(.*)', hline) # it is a header m2 = re.match(r'^(\s+)(.*)', hline) # it is a continuation of prev header if hline == '\n': key = '' elif m1: if m1.group(1): key = m1.group(1) thead.setdefault(key, []).append(m1.group(2)) elif m2: if key == '' and inputs[i - 1] != '\n': key = '-no-hn-' while key in thead.keys(): key += '-' thead.setdefault(key, []).append(m2.group(1) + m2.group(2)) ############# fpr.blue('_' * (tc - 4)) for k in thead.keys(): fpr.info('_' * (tc - 4)) print if re.match('-no-hn-', k): fpr('Header-name: <None>') else: fpr('Header-Name: %s' % k) fpr('Header: %s' % thead[k]) #fpr('Encoding: %s' % enc) fpr.info('_' * (tc - 4)) fpr.blue('_' * (tc - 4)) print #print theadi if thead.keys(): if raw_input(' Are your headers parsed correctly [Y/n]:> ' ) in ['n', 'N']: if raw_input( ' Would you like to add them one more time [Y/n]:> ' ) in ['n', 'N']: break else: waitin() else: # if RFC: format header-name, format header length, # rfcline = 1 # if raw_input(' Should lines follow the RFC or leave it as you put it [Y/n]> ') in ['n', 'N']: # rfcline = 0 #### for k in thead.keys(): smtp['headers']["h_%s" % k] = thead[k] #### dbglog(smtp['headers']) waitin() break else: fpr('No header was specified') waitin() break
def e_headers(): fpr('Diagnostics: Header encoder') print if raw_input(' Would you like to encode some headers [y/N]> ') in [ 'y', 'Y' ]: if True: print fpr('Please provide a header in format: >> Header-name: header-value <<' ) fpr('Use Ctrl-D to continue.') fpr.blue('_' * (tc - 4)) print # to preserve new line pass nl=True with get_inputs() inputs = get_inputs() if inputs != '': thead = {} ####### TODO: move this to def encode_mheaders() for (i, hline) in enumerate(inputs): #print hline m1 = re.match(r'(.*):(.*)', hline) # it is a header m2 = re.match(r'^(\s+)(.*)', hline) # it is a continuation of prev header if hline == '\n': key = '' elif m1: if m1.group(1): key = m1.group(1) thead.setdefault(key, []).append(m1.group(2)) elif m2: if key == '' and inputs[i - 1] != '\n': key = '-no-hn-' while key in thead.keys(): key += '-' thead.setdefault(key, []).append(m2.group(1) + m2.group(2)) ############# fpr.blue('_' * (tc - 4)) print fpr('Found headers') print for k in thead.keys(): fpr.info('_' * (tc - 4)) print if re.match('-no-hn-', k): fpr('Header-name: <None>') else: fpr('Header-Name: %s' % k) fpr('Header: %s' % thead[k]) #fpr('Encoding: %s' % enc) fpr.info('_' * (tc - 4)) if thead.keys(): print chset = raw_input( ' Define headers encoding or leave empty to use a default one\n [us-ascii]: ' ) or 'us-ascii' fpr.blue('_' * (tc - 4)) print #print if chset: #from email.message import Message #from email.header import Header for hn in thead.keys(): try: h = Header(''.join(thead[hn]), charset=chset, maxlinelen=76, header_name=hn) #print h.encode() print msg = Message() msg[hn] = h print msg.as_string() except UnicodeDecodeError, e: print fpr.err('UnicodeDecodeError: %s' % e) print fpr('Try to set proper encoder! ') print fpr.blue('_' * (tc - 4)) waitin() """
def e_base64(op): # load from file if op == '1': fn = '' while True: bancls() fpr('Diagnostics: Base64 encoder') print fpr('Please provide a file name:') fn = raw_input(' []> ') print inputs = get_file_content(fn) print if inputs: #inputs = f.readlines() # include \n #inputs = f.read().splitlines() # exclude \n dbginfo('debug', inputs) print encb64 = coders.encode_attch(inputs) if isinstance(encb64, dict): fpr.ok('File content was encoded successful') else: fpr.fail('File content was not encoded') waitin() return encb64 elif raw_input(' Return to menu [y/N]> ') in ['y', 'Y']: break waitin() # load from input - paste it if op == '2': bancls() fpr('Diagnostics: Base64 encoder') print if raw_input(' Would you like to encode some content [y/N]> ') in [ 'y', 'Y' ]: print fpr('Please provide some plain content') fpr('Use Ctrl-D with new line to continue.') print fpr.blue('_' * (tc - 4)) print inputs = get_inputs() fpr.blue('_' * (tc - 4)) if inputs: dbglog(inputs) print encb64 = coders.encode_attch('\n'.join(inputs)) if isinstance(encb64, dict): fpr.ok('Input content was encoded successful') else: fpr.fail('Input content was not encoded') waitin() return encb64 #print inputs waitin()
def chThrDep(t=None, rpm=None, mpt=None): #VERIFY: In Python 2.x : int/int --> int and int/float --> float In Python 3.x : int/int can result in a float from core.data import smtp, fpr, tc, tr from core.func import waitin r = get_rcpts('NoR') #Warunki Konieczne - Essential Conditions # to met WK1, WK2, WK3 if not t: t = smtp['replay']['threads'].get('not', None) elif t and not (1 <= int(t) <= r): if r > 1: t = r if r == 1: #special case pass if t: smtp['replay']['threads']['not'] = int(t) if not rpm: rpm = smtp['replay']['threads'].get('rpm', None) elif rpm and not (1 <= int(rpm) <= r): rpm = int(r) if rpm: fpr.ok('Saving RpM as %s' % rpm) smtp['replay']['threads']['rpm'] = int(rpm) if not mpt: mpt = smtp['replay']['threads'].get('mpt', None) elif mpt and not (1 <= int(mpt) <= r): mpt = int(r) if mpt: fpr.ok('Saving MpT as %s' % mpt) smtp['replay']['threads']['mpt'] = int(mpt) if r: # specific feature: the result depends from number of recipients # -------------------------------------------------------------------- # if t and not rpm and not mpt: fpr.warn('%s : %s : %s - %s' % (t, rpm, mpt, r)) # allow to set t if no rpm or mpt values and number of rcpt > 1 # but t must be <= r, so if t greater then r => it will be equal to r if r > 1: smtp['replay']['threads']['not'] = int(t) fpr.ok('Saving NoT as %s' % t) return t # special case when r == 1 , not related to our Equation # for r = 1 allow to set T > 1 but do not allow than set rpm or mpt # in this special case the sam message is flooding a sepcific rcpt # with this smae message in multiple threads/connection ! if r == 1: #pass smtp['replay']['threads']['not'] = int(t) fpr.ok('Saving NoT as %s' % t) return t # set initial elif rpm and not t and not mpt: fpr.warn('%s : %s : %s - %s' % (t, rpm, mpt, r)) if r > 1: smtp['replay']['threads']['rpm'] = int(rpm) #return rpm #return True if r == 1: fpr.warn('It doesn\'t make sense to define RpM when NoR = 1') return False # elif mpt and not t and not rpm: fpr.warn('%s : %s : %s - %s' % (t, rpm, mpt, r)) if r > 1: smtp['replay']['threads']['mpt'] = int(mpt) #return mpt #return True if r == 1: fpr.warn('It doesn\'t make sense to define MpT when NoR = 1') return False else: fpr.err('Recipients not defined. Build a list of recipients first.') fpr.err('Flush the values if the recipient list has been modified.') return # Some maths :) # # rpm - recipient per message # r - number of recipients (all) # m - number of messages (all) # # rpm = r / m ==> m = r / rpm # mpt - messages per thread # t - number of threads # m - number of messages (all) # # mpt = m / t ==> m = mpt * t # r / rpm = mpt * t # # ==> t = r / (rpm * mpt) # # ==> rpm = r / (mpt * t) # # ==> mpt = r / (rpm * t) fpr.info('_' * (tc - 4)) print fpr.warn('Your limits:') fpr.warn('t = %s : rpm = %s : mpt = %s - r = %s' % (smtp['replay']['threads'].get( 'not', None), smtp['replay']['threads'].get('rpm', None), smtp['replay']['threads'].get('mpt', None), r)) print fpr.err('Maximum possible limits to met Essential Conditions:') fpr.err('t = %s : rpm = %s : mpt = %s - r = %s' % (t, rpm, mpt, r)) fpr.info('_' * (tc - 4)) print def chEque(): print if r: # compare float # https://ubuntuforums.org/showthread.php?t=840665 def float_eq(a, b, epsilon=0.00000001): fpr.cyan('<float_eq> %s' % abs(a - b)) if abs(a - b) < epsilon: return True return False m1 = float(r) / rpm m2 = mpt * float(t) fpr.cyan('r/rpm = %s, mpt * t = %s' % (m1, m2)) if float_eq(m1, m2): fpr.green('Equetion is met') waitin() return True else: fpr.err('Equetion is false') waitin() return False def setReals(t, rpm, mpt): if t and rpm and mpt: smtp['replay']['threads']['reals'].setdefault('not', t) smtp['replay']['threads']['reals'].setdefault('rpm', rpm) smtp['replay']['threads']['reals'].setdefault('mpt', mpt) smtp['replay']['threads']['reals']['not'] = t smtp['replay']['threads']['reals']['rpm'] = rpm smtp['replay']['threads']['reals']['mpt'] = mpt fpr.ok('Reals were set!') if r: # count the mpt - based on rpm and t # -------------------------------------------------------------------- # if t and rpm and not mpt: fpr.green('%s : %s : %s - %s' % (t, rpm, mpt, r)) # m = r / rpm # mpt = m / t # I - validate MPT - MPT <= R mpt = float(r) / (rpm * t) fpr.green('counting mpt = %s' % mpt) chEque() # round up - if there is a remainder if (r % (rpm * t) > 0): mpt = r / (rpm * t) + (r % (rpm * t) > 0) fpr.green('rounding up mpt = %s' % mpt) # find the lowest T for rounded MpT to met RpM t_new = float(r) / (rpm * mpt) #+ (r % (rpm * mpt) > 0) fpr.green('counting new t = %s' % t_new) if (r % (rpm * mpt) > 0): # if remainder t_new = r / (rpm * mpt) + (r % (rpm * mpt) > 0) fpr.green('rounding up new t = %s' % t_new) fpr.err( 'realistic returns would be: *t = %s, rpm = %s, mpt = %s for r = %s' % (int(t_new), rpm, int(mpt), r)) setReals(int(t_new), rpm, int(mpt)) else: fpr.err( 'realistic returns would be: t = %s, rpm = %s, mpt = %s for r = %s' % (t, rpm, int(mpt), r)) setReals(t, rpm, int(mpt)) # count the rpm - for mpt and t # -------------------------------------------------------------------- # elif t and mpt and not rpm: fpr.blue('%s : %s : %s - %s' % (t, rpm, mpt, r)) # m = mpt * t # m = r / rpm # TODO # WK: validate RPM , RPM <= R rpm = float(r) / (mpt * t) fpr.blue('counting: rpm = %s' % rpm) chEque() # if there is a remainder if (r % (mpt * t) > 0): rpm = r / (mpt * t) + (r % (mpt * t) > 0) fpr.blue('rounding up rpm = %s' % rpm) # find the lowest T for rounded MpT to met RpM t_new = float(r) / (rpm * mpt) #+ (r % (rpm * mpt) > 0) fpr.green('counting new t = %s' % t_new) if (r % (rpm * mpt) > 0): # if remainder t_new = r / (rpm * mpt) + (r % (rpm * mpt) > 0) fpr.green('rounding up new t = %s' % t_new) fpr.err( 'realistic returns would be: *t = %s, rpm = %s, mpt = %s for r = %s' % (int(t_new), int(rpm), mpt, r)) setReals(int(t_new), int(rpm), mpt) else: fpr.err( 'realistic returns would be: t = %s, rpm = %s, mpt = %s for r = %s' % (t, int(rpm), mpt, r)) setReals(t, int(rpm), mpt) # count the mpt ( use t = 1 ) # -------------------------------------------------------------------- # elif rpm and not t and not mpt: t = 1 fpr.purple('assuming t eq 1 for equation') fpr.purple('%s : %s : %s - %s' % (t, rpm, mpt, r)) mpt = float(r) / rpm fpr.purple('counting: mpt = %s' % mpt) chEque() #if there is a remainder if (r % rpm > 0): mpt = r / rpm + (r % rpm > 0) fpr.purple('rounding up mpt = %s' % mpt) # find the lowest T for rounded MpT to met RpM t_new = float(r) / (rpm * mpt) #+ (r % (rpm * mpt) > 0) fpr.green('counting new t = %s' % t_new) if (r % (rpm * mpt) > 0): # if remainder t_new = r / (rpm * mpt) + (r % (rpm * mpt) > 0) fpr.green('rounding up new t = %s' % t_new) fpr.err( 'realistic returns would be: *t = %s, rpm = %s, mpt = %s for r = %s' % (int(t_new), rpm, mpt, r)) setReals(int(t_new), rpm, mpt) else: fpr.err( 'realistic returns would be: t = %s, rpm = %s, mpt = %s for r = %s' % (t, rpm, int(mpt), r)) setReals(t, rpm, int(mpt)) # count the rpm ( use t = 1 ) # -------------------------------------------------------------------- # elif mpt and not t and not rpm: t = 1 fpr.purple('assuming t eq 1 for equation') fpr.purple('%s : %s : %s - %s' % (t, rpm, mpt, r)) rpm = float(r) / mpt fpr.purple('counting: rpm = %s' % rpm) t = 1 chEque() #if there is a remainder if (r % mpt > 0): rpm = r / mpt + (r % mpt > 0) fpr.purple('rounding up rpm = %s' % rpm) # find the lowest T for rounded MpT to met RpM t_new = float(r) / (rpm * mpt) #+ (r % (rpm * mpt) > 0) fpr.green('counting new t = %s' % t_new) if (r % (rpm * mpt) > 0): # if remainder t_new = r / (rpm * mpt) + (r % (rpm * mpt) > 0) fpr.green('rounding up new t = %s' % t_new) fpr.err( 'realistic returns would be: *t = %s, rpm = %s, mpt = %s for r = %s' % (int(t_new), int(rpm), mpt, r)) setReals(int(t_new), int(rpm), mpt) else: fpr.err( 'realistic returns would be: t = %s, rpm = %s, mpt = %s for r = %s' % (t, int(rpm), mpt, r)) setReals(t, int(rpm), mpt) # count t - for rpm and mpt # -------------------------------------------------------------------- # elif mpt and rpm and not t: fpr.white('%s : %s : %s - %s' % (t, rpm, mpt, r)) print type(mpt) # add 1 to round up a result # (r % (rpm * mpt) > 0) it will return True which is 1 # if there is a remainder and 0 if not t = float(r) / (rpm * mpt) #+ (r % (rpm * mpt) > 0) fpr.white('counted: t = %s' % t) chEque() # round up if (r % (rpm * mpt) > 0): t = r / (rpm * mpt) + (r % (rpm * mpt) > 0) fpr.white('rounding up t = %s' % t) # validate WK # t = <1,r> # if t < 1 if (float(r) / (rpm * mpt)) < 1: fpr.info('Now to met equetion for t=1 it is not possible for %s (R) recipients '\ 'to send %s (MpT) messeges in one thread with %s (RpM) recipient in message' % (r,mpt,rpm)) print fpr.info('New RpM value can be counted based on existing MpT or new MpT ' \ 'value can be counted based on existing RpM' ) print fpr.info('For simplicity it is assumed that RpM limits is more important so MpT ' \ 'is going to be recounted to met an equation') print mpt_new = float(r) / rpm fpr.white('New MpT to met equetion is %s' % mpt_new) mpt = mpt_new chEque() # now just round up mpt_new = r / rpm + (r % rpm > 0) fpr.white('rounding up new mpt = %s' % mpt_new) fpr.err( 'realistic returns would be: t = %s, rpm = %s, *mpt = %s for r = %s' % (int(t), rpm, int(mpt_new), r)) setReals(int(t), rpm, int(mpt_new)) else: # so if t > 1 and it was counted fpr.err( 'realistic returns would be: t = %s, rpm = %s, mpt = %s for r = %s' % (int(t), rpm, int(mpt), r)) setReals(int(t), rpm, int(mpt)) # only true if r / rpm = mpt * t # -------------------------------------------------------------------- # elif t and rpm and mpt: fpr.cyan('%s : %s : %s - %s' % (t, rpm, mpt, r)) if not chEque(): fpr.info('_' * (tc - 4)) print fpr.warn('It is not recommended trying to set NoT with RpM and MpT. ' \ 'Suggested action would be to flush these values.' ) fpr.info('_' * (tc - 4)) print waitin() return False else: fpr.err( 'realistic returns would be: t = %s, rpm = %s, mpt = %s for r = %s' % (t, rpm, mpt, r)) setReals(t_new, rpm, mpt) # for test waitin()