Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/smshex_r.../opt/sharedra...
File: fraudhunter.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""Fraud Hunter Tool for Chuck H."""
[1] Fix | Delete
import multiprocessing
[2] Fix | Delete
import argparse
[3] Fix | Delete
import os
[4] Fix | Delete
import signal
[5] Fix | Delete
import sys
[6] Fix | Delete
import rads
[7] Fix | Delete
[8] Fix | Delete
[9] Fix | Delete
def get_args():
[10] Fix | Delete
"""Parse arguments"""
[11] Fix | Delete
parser = argparse.ArgumentParser(description=__doc__)
[12] Fix | Delete
# fmt: off
[13] Fix | Delete
parser.add_argument(
[14] Fix | Delete
'-n', '--name', dest='filenames', nargs='+', required=True,
[15] Fix | Delete
help='File name(s) to look for',
[16] Fix | Delete
)
[17] Fix | Delete
parser.add_argument(
[18] Fix | Delete
'-e', '--exclude', dest='exc_user', nargs='+', default=[],
[19] Fix | Delete
help='User(s) to exclude',
[20] Fix | Delete
)
[21] Fix | Delete
parser.add_argument(
[22] Fix | Delete
'-x', '--max', type=int, help='Max file size (in bytes)'
[23] Fix | Delete
)
[24] Fix | Delete
parser.add_argument(
[25] Fix | Delete
'-m', '--min', type=int, help='Min file size (in bytes)'
[26] Fix | Delete
)
[27] Fix | Delete
# fmt: on
[28] Fix | Delete
args = parser.parse_args()
[29] Fix | Delete
if args.min > args.max:
[30] Fix | Delete
print("The minimum size needs to be larger than the maximum size")
[31] Fix | Delete
sys.exit(1)
[32] Fix | Delete
[33] Fix | Delete
return args.filenames, args.exc_user, args.max, args.min
[34] Fix | Delete
[35] Fix | Delete
[36] Fix | Delete
def targets(excluded):
[37] Fix | Delete
"""Get a list of users that aren't suspended, sys. users, or excluded"""
[38] Fix | Delete
return [
[39] Fix | Delete
user
[40] Fix | Delete
for user in rads.all_cpusers()
[41] Fix | Delete
if user not in rads.OUR_RESELLERS
[42] Fix | Delete
and not rads.cpuser_suspended(user)
[43] Fix | Delete
and user not in excluded
[44] Fix | Delete
]
[45] Fix | Delete
[46] Fix | Delete
[47] Fix | Delete
def check_size(filename, maxsize, minsize):
[48] Fix | Delete
"""Confirm filesize within argument constraints"""
[49] Fix | Delete
size = os.path.getsize(filename)
[50] Fix | Delete
if maxsize is not None:
[51] Fix | Delete
if size > maxsize:
[52] Fix | Delete
return False
[53] Fix | Delete
if minsize is not None:
[54] Fix | Delete
if size < minsize:
[55] Fix | Delete
return False
[56] Fix | Delete
return True
[57] Fix | Delete
[58] Fix | Delete
[59] Fix | Delete
def hunter(gdtuple):
[60] Fix | Delete
"""Print any file that matches the filenames"""
[61] Fix | Delete
user, filenames, maxsize, minsize = gdtuple
[62] Fix | Delete
try:
[63] Fix | Delete
homedir = rads.get_homedir(user)
[64] Fix | Delete
except rads.CpuserError as exc:
[65] Fix | Delete
print(exc, file=sys.stderr)
[66] Fix | Delete
return
[67] Fix | Delete
for root, _, files in os.walk(homedir, topdown=False):
[68] Fix | Delete
for name in files:
[69] Fix | Delete
if name in filenames:
[70] Fix | Delete
fullpath = os.path.join(root, name)
[71] Fix | Delete
if maxsize is None and minsize is None:
[72] Fix | Delete
print(fullpath)
[73] Fix | Delete
continue
[74] Fix | Delete
if check_size(fullpath, maxsize, minsize):
[75] Fix | Delete
print(fullpath)
[76] Fix | Delete
[77] Fix | Delete
[78] Fix | Delete
def start_the_hunt(target_users, filenames, maxsize, minsize):
[79] Fix | Delete
"""set up a multiprocessing queue to loop through accounts"""
[80] Fix | Delete
args = ((user, filenames, maxsize, minsize) for user in target_users)
[81] Fix | Delete
# shenanigans to deal with keyboard interrupts
[82] Fix | Delete
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
[83] Fix | Delete
with multiprocessing.Pool(multiprocessing.cpu_count()) as pool:
[84] Fix | Delete
signal.signal(signal.SIGINT, original_sigint_handler)
[85] Fix | Delete
try:
[86] Fix | Delete
results = pool.map_async(hunter, args)
[87] Fix | Delete
# dealing with Python bug https://bugs.python.org/issue8296
[88] Fix | Delete
results.get(10)
[89] Fix | Delete
except multiprocessing.TimeoutError:
[90] Fix | Delete
pass
[91] Fix | Delete
except KeyboardInterrupt:
[92] Fix | Delete
print("Caught KeyboardInterrupt.")
[93] Fix | Delete
pool.terminate()
[94] Fix | Delete
pool.close()
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
def main():
[98] Fix | Delete
'''Main function: get args'''
[99] Fix | Delete
filenames, excluded, maxsize, minsize = get_args()
[100] Fix | Delete
target_users = targets(excluded)
[101] Fix | Delete
[102] Fix | Delete
start_the_hunt(target_users, filenames, maxsize, minsize)
[103] Fix | Delete
[104] Fix | Delete
[105] Fix | Delete
if __name__ == "__main__":
[106] Fix | Delete
main()
[107] Fix | Delete
[108] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function