Ejemplo n.º 1
0
def run():
    is_timeout = False
    code = request.form.get('code')
    stdin = request.form.get('stdin')
    code_filename = "/tmp/" + str(uuid4())
    try:
        with open(code_filename, "w") as code_file:
            code_file.write(code)
        p = Popen(
            ['bbm', code_filename],
            stdout=PIPE,
            stdin=PIPE,
            stderr=PIPE
        )
        stdout, stderr = p.communicate(input=stdin.encode('utf-8'), timeout=15)
    except TimeoutExpired:
        is_timeout = True
        p.kill()
        stdout, stderr = p.communicate()
    finally:
        remove(code_filename)
    stdout = stdout.decode('utf-8')
    stderr = stderr.decode('utf-8')
    return jsonify({
        'stdout': stdout,
        'stderr': stderr,
        'is_timeout': is_timeout
    })
Ejemplo n.º 2
0
def runcmd(cmd, input=None, stringio=True, **kwargs):
    if six.PY2:
        from subprocess32 import Popen, PIPE
    else:
        from subprocess import Popen, PIPE
    timeout = kwargs.pop('timeout', None)
    if input: kwargs['stdin'] = PIPE
    if not 'bufsize' in kwargs: kwargs['bufsize']= -1
    if isinstance(cmd, six.string_types):
        import shlex
        cmd = shlex.split(cmd)
    elif kwargs.get('shell'):
        from six.moves import shlex_quote
        cmd = [shlex_quote(arg) for arg in cmd]
    process = Popen(cmd,universal_newlines=stringio,stdout=PIPE,stderr=PIPE,**kwargs)
    try:
        output, error = process.communicate(input=input, timeout=timeout)
    except TimeoutExpired:
        process.kill()
        output, error = process.communicate()
        raise TimeoutExpired(process.args, timeout, output=output)
    retcode = process.poll()
    if retcode:
        raise SubProcessError(retcode, process.args, output=output, error=error)
    return output, error
Ejemplo n.º 3
0
    def run_benchmark(self,
                      workload_script,
                      benchmark_iteration,
                      log_start_fn,
                      waited_time_limit=None):
        proc = Popen([
            "docker", "exec", self.head_container_id, "/bin/bash", "-c",
            "RAY_BENCHMARK_ENVIRONMENT=stress RAY_BENCHMARK_ITERATION={} RAY_REDIS_ADDRESS={}:6379 RAY_NUM_WORKERS={} python {}"
            .format(benchmark_iteration, self.head_container_ip,
                    self.num_workers, workload_script)
        ],
                     stdout=PIPE,
                     stderr=PIPE)

        log_start_fn(proc.pid)
        start_time = time.time()
        done = False
        while not done:
            try:
                (stdoutdata, stderrdata) = proc.communicate(
                    timeout=min(10, waited_time_limit))
                done = True
            except (subprocess32.TimeoutExpired):
                waited_time = time.time() - start_time
                if waited_time_limit and waited_time > waited_time_limit:
                    self.logger.log(
                        "killed", {
                            "pid": proc.pid,
                            "waited_time": waited_time,
                            "waited_time_limit": waited_time_limit
                        })
                    proc.kill()
                    return {"success": False, "return_code": None, "stats": {}}
                else:
                    self.logger.log(
                        "waiting", {
                            "pid": proc.pid,
                            "time_waited": waited_time,
                            "waited_time_limit": waited_time_limit
                        })
        m = re.search('^BENCHMARK_STATS: ({.*})$', stdoutdata, re.MULTILINE)
        if m:
            output_stats = json.loads(m.group(1))
        else:
            output_stats = {}

        print stdoutdata
        print stderrdata
        return {
            "success": proc.returncode == 0,
            "return_code": proc.returncode,
            "stats": output_stats
        }
Ejemplo n.º 4
0
class Voice:

    def __init__(self):
        self.subprocess = None

    def say(self,quote):
        self.stop()
        self.subprocess = Popen(["espeak", \
                                 "-s", "140",\
                                 quote])

    def stop(self):
        if self.subprocess != None:
            # FIXME how do we know the process really terminated?
            self.subprocess.kill()
        self.subprocess = None
Ejemplo n.º 5
0
 def run_compiler(self, language, filename, executable_name):
     args = ["g++" if language else "gcc", "-static", "-w", "-O2", filename, "-o",
             executable_name]
     self.log += ['Running: ' + ' '.join(args)]
     proc = Popen(args,
                  cwd=self.base_dir, stdin=PIPE, stdout=PIPE, stderr=PIPE)
     output = proc.communicate(timeout=self.COMPILE_TIMEOUT)
     self.log += [str(output[1])]
     if proc.poll() is None:
         try:
             self.log += ['Compile timeout.']
             proc.kill()
         except Exception:
             pass
     self.log += ["Compiler returns %d." % proc.returncode]
     if proc.returncode:
         raise CompileErrorException()
Ejemplo n.º 6
0
    def gen_XML_files(self):
        # Run source code analysis and generate xml files
        input_file = 'INPUT=' + self.filename
        output_dir = 'OUTPUT_DIRECTORY=' + self.outdir
        cmd_1 = ['cat', self.conf]
        p1 = Popen(cmd_1, stdout=PIPE)
        doxy_conf = p1.communicate()[0]
        doxy_conf = doxy_conf + input_file + '\n'
        doxy_conf = doxy_conf + output_dir

        cmd = 'doxygen -'
        cmd_2 = cmd.split()
        p2 = Popen(cmd_2, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        p2.stdin.write(doxy_conf)
        # On rare occasions, doxygen may 'hang' on input files, not delivering
        # any result. Use a timeout to work around such situations
        try:
            p2.communicate(timeout=5 * 60)  # Stop trying after 5 minutes
        except TimeoutExpired:
            log.warning("Doxygen got stuck, cancelling analysis run")
            p2.kill()
Ejemplo n.º 7
0
def check_output(*popenargs, **kwargs):
    r"""Run command with arguments and return its output as a byte string.
    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.
    The arguments are the same as for the Popen constructor.  Example:
    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'
    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.
    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'
    """

    timeout = kwargs.pop('timeout', None)
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')

    if _kill_processes.is_set():
        raise TerminateSignaled()

    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    _processes.append(process)

    try:
        output, unused_err = process.communicate(timeout=timeout)
        _processes.remove(process)

    except TimeoutExpired:
        _processes.remove(process)
        process.kill()
        output, unused_err = process.communicate()
        raise TimeoutExpired(process.args, timeout, output=output)

    retcode = process.poll()
    if retcode:
        raise CalledProcessError(retcode, process.args, output=output)
    return output
Ejemplo n.º 8
0
 def run_compiler(self, language, filename, executable_name):
     args = [
         "g++" if language else "gcc", "-static", "-w", "-O2", filename,
         "-o", executable_name
     ]
     self.log += ['Running: ' + ' '.join(args)]
     proc = Popen(args,
                  cwd=self.base_dir,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=PIPE)
     output = proc.communicate(timeout=self.COMPILE_TIMEOUT)
     self.log += [str(output[1])]
     if proc.poll() is None:
         try:
             self.log += ['Compile timeout.']
             proc.kill()
         except Exception:
             pass
     self.log += ["Compiler returns %d." % proc.returncode]
     if proc.returncode:
         raise CompileErrorException()
Ejemplo n.º 9
0
    def kill(self, exitCode=-1, gracePeriod=None, sig=None):
        """Kill process.

        "exitCode" this sets what the process return value will be.
        "gracePeriod" [deprecated, not supported]
        "sig" (Unix only) is the signal to use to kill the process. Defaults
            to signal.SIGKILL. See os.kill() for more information.
        """
        if gracePeriod is not None:
            import warnings
            warnings.warn("process.kill() gracePeriod is no longer used",
                          DeprecationWarning)

        # Need to ensure stdin is closed, makes it easier to end the process.
        if self.stdin is not None:
            self.stdin.close()

        if sys.platform.startswith("win"):
            # TODO: 1) It would be nice if we could give the process(es) a
            #       chance to exit gracefully first, rather than having to
            #       resort to a hard kill.
            #       2) May need to send a WM_CLOSE event in the case of a GUI
            #       application, like the older process.py was doing.
            Popen.kill(self)
        else:
            if sig is None:
                sig = signal.SIGKILL
            try:
                if self.__use_killpg:
                    os.killpg(self.pid, sig)
                else:
                    os.kill(self.pid, sig)
            except OSError as ex:
                if ex.errno != 3:
                    # Ignore:   OSError: [Errno 3] No such process
                    raise
            self.returncode = exitCode
Ejemplo n.º 10
0
    def kill(self, exitCode=-1, gracePeriod=None, sig=None):
        """Kill process.

        "exitCode" this sets what the process return value will be.
        "gracePeriod" [deprecated, not supported]
        "sig" (Unix only) is the signal to use to kill the process. Defaults
            to signal.SIGKILL. See os.kill() for more information.
        """
        if gracePeriod is not None:
            import warnings
            warnings.warn("process.kill() gracePeriod is no longer used",
                          DeprecationWarning)

        # Need to ensure stdin is closed, makes it easier to end the process.
        if self.stdin is not None:
            self.stdin.close()

        if sys.platform.startswith("win"):
            # TODO: 1) It would be nice if we could give the process(es) a
            #       chance to exit gracefully first, rather than having to
            #       resort to a hard kill.
            #       2) May need to send a WM_CLOSE event in the case of a GUI
            #       application, like the older process.py was doing.
            Popen.kill(self)
        else:
            if sig is None:
                sig = signal.SIGKILL
            try:
                if self.__use_killpg:
                    os.killpg(self.pid, sig)
                else:
                    os.kill(self.pid, sig)
            except OSError, ex:
                if ex.errno != 3:
                    # Ignore:   OSError: [Errno 3] No such process
                    raise
            self.returncode = exitCode
Ejemplo n.º 11
0
class UserProgram:
    def __init__(self, path):
        self.path = path
        self.process = None
        self.timeout = 1.0
        self.timer = None
        self.tle = False

    def execute(self):
        if not self.path:
            raise ValueError("Path cannot be empty.")
        self.process = Popen(self.path, stdin=PIPE, stdout=PIPE, stderr=PIPE)

    def start_timer(self):
        self.timer = Timer(self.timeout, self.time_limit_exceeded)
        self.timer.start()

    def stop_timer(self):
        self.timer.cancel()
        self.timer = None

    def read_line(self):
        return self.process.stdout.readline()

    def write_line(self, line):
        self.process.stdout.write(line)

    def kill_process(self):
        try:
            self.process.kill()
        except Exception:
            pass

    def time_limit_exceeded(self):
        self.tle = True
        self.kill_process()
Ejemplo n.º 12
0
def run2(command, check=True, timeout=None, *args, **kwargs):
    ''' Run a command.

        If check=True (the default),
        then if return code is not zero or there is stderr output,
        raise CalledProcessError. Return any output in the exception.

        If timeout (in seconds) is set and command times out, raise TimeoutError. '''

    ''' Parts from subprocess32.check_output(). '''

    raise Exception('Deprecated. Use the sh module.')

    # use subprocess32 for timeout
    from subprocess32 import Popen, CalledProcessError, TimeoutExpired

    process = Popen(command, stdout=stdout, stderr=stderr, *args, **kwargs)
    try:
        process.wait(timeout=timeout)
    except TimeoutExpired:
        print('TimeoutExpired') #DEBUG
        #print('stdout: %s, (%d)' % (str(stdout), len(str(stdout)))) #DEBUG
        #print('stderr: %s, (%d)' % (str(stderr), len(str(stderr)))) #DEBUG
        try:
            process.kill()
            process.wait()
        finally:
            print('after kill/wait') #DEBUG
            #print('stdout: %s, (%d)' % (str(stdout), len(str(stdout)))) #DEBUG
            #print('stderr: %s, (%d)' % (str(stderr), len(str(stderr)))) #DEBUG
            raise TimeoutExpired(process.args, timeout)

    if check:
        retcode = process.poll()
        if retcode:
            raise CalledProcessError(retcode, process.args)
Ejemplo n.º 13
0
class UserProgram:
    def __init__(self, path):
        self.path = path
        self.process = None
        self.timeout = 1.0
        self.timer = None
        self.tle = False

    def execute(self):
        if not self.path:
            raise ValueError("Path cannot be empty.")
        self.process = Popen(self.path, stdin=PIPE, stdout=PIPE, stderr=PIPE)

    def start_timer(self):
        self.timer = Timer(self.timeout, self.time_limit_exceeded)
        self.timer.start()

    def stop_timer(self):
        self.timer.cancel()
        self.timer = None

    def read_line(self):
        return self.process.stdout.readline()

    def write_line(self, line):
        self.process.stdout.write(line)

    def kill_process(self):
        try:
            self.process.kill()
        except Exception:
            pass

    def time_limit_exceeded(self):
        self.tle = True
        self.kill_process()
Ejemplo n.º 14
0
    def start_activities(self, package, csvpath, output_dir, lite=False):
        self.logger.info("Try to read csv " + csvpath)
        csvfile = open(csvpath, 'rb')
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in spamreader:
            activity = row[0]
            activity = str(activity).replace('\"', '')
            if 'com.google.ads.AdActivity' in activity:
                self.logger.error('Cannot start Activity: ' + activity)
                continue
            if self.start_activity(package, activity):
                if not lite:
                    UIExerciser.run_adb_cmd('logcat -c')
                    self.logger.info('clear logcat'
                                     )  # self.screenshot(output_dir, activity)

                    # UIExerciser.run_adb_cmd('shell "nohup /data/local/tcpdump -w /sdcard/' + package + current_time  + '.pcap &"')
                    # UIExerciser.run_adb_cmd('shell "nohup logcat -v threadtime -s "UiDroid_Taint" > /sdcard/' + package + current_time +'.log &"')

                    # cmd = 'adb -s ' + series + ' shell "nohup /data/local/tcpdump -w /sdcard/' + package + current_time + '.pcap &"'
                    self.logger.info('tcpdump begins')
                    cmd = 'adb -s ' + self.series + ' shell /data/local/tcpdump -w /sdcard/' + activity + '.pcap'
                    # os.system(cmd)
                    print cmd
                    process = Popen(cmd,
                                    stdout=PIPE,
                                    stderr=STDOUT,
                                    shell=True)
                time.sleep(2)
                # self.screenshot(output_dir, activity)
                for i in range(1, 3):
                    if not UIExerciser.check_dev_online(UIExerciser.series):
                        if UIExerciser.emu_proc:
                            UIExerciser.close_emulator(UIExerciser.emu_proc)
                            UIExerciser.emu_proc = UIExerciser.open_emu(
                                UIExerciser.emu_loc, UIExerciser.emu_name)
                        else:
                            raise Exception('Cannot start Activity ' +
                                            activity)
                    if Utilities.run_method(self.screenshot,
                                            180,
                                            args=[output_dir, activity,
                                                  False]):
                        break
                    else:
                        self.logger.warn("Timeout while dumping XML for " +
                                         activity)
                if not lite:
                    time.sleep(10)
                    process.kill()  # takes more time
                    out_pcap = output_dir + activity + '.pcap'
                    while not os.path.exists(
                            out_pcap) or os.stat(out_pcap).st_size < 2:
                        time.sleep(5)
                        cmd = 'pull /sdcard/' + activity + '.pcap ' + out_pcap
                        UIExerciser.run_adb_cmd(cmd)

                    taint_logs = []
                    Utilities.run_method(
                        TaintDroidLogHandler.collect_taint_log,
                        15,
                        args=[taint_logs])
                    with open(output_dir + activity + '.json', 'w') as outfile:
                        json.dump(taint_logs, outfile)
            else:
                time.sleep(2)
                self.logger.error('Cannot start Activity: ' + activity)
Ejemplo n.º 15
0
    def update(self, jobs):
        """
        Update job states to match their current state in PBS queue.

        :param jobs: A list of Job instances for jobs to be updated.
        """
        # Extract list of user names associated to the jobs
        _users = []
        for _service in G.SERVICE_STORE.values():
            if _service.config['username'] not in _users:
                _users.append(_service.config['username'])

        # We agregate the jobs by user. This way one qstat call per user is
        # required instead of on call per job
        _job_states = {}
        for _usr in _users:
            try:
                # Run qstat
                logger.log(VERBOSE, "@PBS - Check jobs state for user %s", _usr)
                _opts = ["/usr/bin/qstat", "-f", "-x", "-u", _usr]
                logger.log(VERBOSE, "@PBS - Running command: %s", _opts)
                _proc = Popen(_opts, stdout=PIPE, stderr=STDOUT)
                # Requires subprocess32 module from pip
                _output = _proc.communicate(timeout=conf.pbs_timeout)[0]
                logger.log(VERBOSE, _output)
                # Check return code. If qstat was not killed by signal Popen
                # will not rise an exception
                if _proc.returncode != 0:
                    raise OSError((
                        _proc.returncode,
                        "/usr/bin/qstat returned non zero exit code.\n%s" %
                        str(_output)
                    ))
            except TimeoutExpired:
                _proc.kill()
                logger.error("@PBS - Unable to check jobs state.",
                             exc_info=True)
                return
            except:
                logger.error("@PBS - Unable to check jobs state.",
                             exc_info=True)
                return

            # Parse the XML output of qstat
            try:
                _xroot = ET.fromstring(_output)
                for _xjob in _xroot.iter('Job'):
                    _xjid = _xjob.find('Job_Id').text
                    _xstate = _xjob.find('job_state').text
                    _xexit = _xjob.find('exit_status')
                    if _xexit is not None:
                        _xexit = _xexit.text
                    _job_states[_xjid] = (_xstate, _xexit)
            except:
                logger.error("@PBS - Unable to parse qstat output.",
                             exc_info=True)
                return
            logger.log(VERBOSE, _job_states)

        # Iterate through jobs
        for _job in jobs:
            # TODO rewrite to get an array JID -> queue from SchedulerQueue table with single SELECT
            _pbs_id = str(_job.scheduler.id)
            # Check if the job exists in the PBS
            if _pbs_id not in _job_states:
                _job.die('@PBS - Job %s does not exist in the PBS', _job.id())
            else:
                # Update job progress output
                self.progress(_job)
                _new_state = 'queued'
                _exit_code = 0
                _state = _job_states[_pbs_id]
                logger.log(VERBOSE, "@PBS - Current job state: '%s' (%s)",
                           _state[0], _job.id())
                # Job has finished. Check the exit code.
                if _state[0] == 'C':
                    _new_state = 'done'
                    _msg = 'Job finished succesfully'

                    _exit_code = _state[1]
                    if _exit_code is None:
                        _new_state = 'killed'
                        _msg = 'Job was killed by the scheduler'
                        _exit_code = ExitCodes.SchedulerKill

                    _exit_code = int(_exit_code)
                    if _exit_code > 256:
                        _new_state = 'killed'
                        _msg = 'Job was killed by the scheduler'
                    elif _exit_code > 128:
                        _new_state = 'killed'
                        _msg = 'Job was killed'
                    elif _exit_code > 0:
                        _new_state = 'failed'
                        _msg = 'Job finished with error code'

                    try:
                        _job.finish(_msg, _new_state, _exit_code)
                    except:
                        _job.die('@PBS - Unable to set job state (%s : %s)' %
                                 (_new_state, _job.id()), exc_info=True)
                # Job is running
                elif _state[0] == 'R' or _state[0] == 'E':
                    if _job.get_state() != 'running':
                        try:
                            _job.run()
                        except:
                            _job.die("@PBS - Unable to set job state "
                                     "(running : %s)" % _job.id(), exc_info=True)
                # Treat all other states as queued
                else:
                    if _job.get_state() != 'queued':
                        try:
                            _job.queue()
                        except:
                            _job.die("@PBS - Unable to set job state "
                                     "(queued : %s)" % _job.id(), exc_info=True)
Ejemplo n.º 16
0
     #pass old targets to derivative bank and update
     #last_targetT = targetT
     #last_targetH = targetH
     #targetT = params['tempDes']
     #targetH = params['humDes']
     #targetL = params['lightMode']
     #LtimeOn = params['timeOn']
     #LtimeOff = params['timeOff']
     #cameraInterval = params['cameraInterval']
     #change PID module setpoints to target
     pid_temp.SetPoint = targetT
     pid_hum.SetPoint = targetH
 except (KeyboardInterrupt):
     print(" ")
     print("Terminating Program...")
     heat_process.kill()
     heat_process.wait()
     hum_process.kill()
     hum_process.wait()
     fan_process.kill()
     fan_process.wait()
     light_process.kill()
     light_process.wait()
     camera_process.kill()
     camera_process.wait()
     water_process.kill()
     water_process.wait()
     sys.exit()
 except Exception as e:
     print(e)
     pass
Ejemplo n.º 17
0
                #pass old targets to derivative bank and update
                last_targetT = targetT
                last_targetH = targetH
                targetT = params['tempDes']
                targetH = params['humDes']
                targetL = params['lightMode']
                LtimeOn = params['timeOn']
                LtimeOff = params['timeOff']
                lightCameraInterval = params['cameraInterval']
                #change PID module setpoints to target
                pid_temp.SetPoint = targetT
                pid_hum.SetPoint = targetH
            except (KeyboardInterrupt):
                print(" ")
                print("Terminating Program...")
                heat_process.kill()
                heat_process.wait()
                hum_process.kill()
                hum_process.wait()
                fan_process.kill()
                fan_process.wait()
                light_camera_process.kill()
                light_camera_process.wait()
                sys.exit()
            except:
                pass
        #poll subprocesses if applicable and relaunch/update actuators
        poll_heat = heat_process.poll() #heat
        if poll_heat is not None:
            heat_process = Popen(['python', 'heatingElement.py', str(tempPID_out)], stdout=PIPE, stdin=PIPE, stderr=PIPE)
Ejemplo n.º 18
0
import time
import subprocess32
from subprocess32 import Popen, PIPE, STDOUT

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  #Button to GPIO23

running = False
try:
    while True:
        button_state = GPIO.input(23)
        if button_state == False:
            if running == False:
                print('starting...')
                running = True
                sensingfeedback = Popen(['python', 'sensingfeedback_v1.4.py'],
                                        stdout=PIPE,
                                        stdin=PIPE,
                                        stderr=PIPE)  #start python subprocess
                time.sleep(3.5)
            else:
                print('stopping...')
                running = False
                sensingfeedback.kill()
                sensingfeedback.wait()
                time.sleep(3.5)
except:
    GPIO.cleanup()
    print('Exited.')
Ejemplo n.º 19
0
def change_password(username, current_password, new_password):
    msg_to_web = None
    
    # Popen executes smbpasswd as a child program in a new process,
    # with arguments to the program smbpasswd in a list [].
    #
    # The -s option causes smbpasswd to read from stdin instead of prompting the user.
    # The -U [username] option allows a user to change his/her own password.
    #
    # stdin, stdout and stderr are assigned as PIPE so that we can
    # use communicate() to provide input to stdin of smbpasswd and
    # get message of both success and error back from the program.
    #
    # shell=False in order to avoid the security risk with shell 
    # injection from unsanitized input such as "input; rm -rf /".
    smbpasswd_proc = Popen([u"smbpasswd", u"-s", u"-r", SMB_SERVER, u"-U", username],
                                 stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=False)

    try:
        # Space, '.' and newline are inconsistently used in the output from smbpasswd
        # and therefore we strip those characters from the end of the output so that
        # we can do sane regex matching without fearing that one day someone will fix
        # this and break our application.
        smbpasswd_output = (smbpasswd_proc.communicate(
                                    input=(current_password + u'\n'
                                           + new_password   + u'\n'
                                           + new_password   + u'\n')
                                           .encode("UTF-8"), timeout=30)[0]
                                           ).rstrip(u' .\n') 
    except TimeoutExpired:
        smbpasswd_proc.kill()
        log_to_file(u"TIME_OUT: User: %s: subprocess.communicate timed out." % username)
        smbpasswd_output_to_logfile(smbpasswd_output)
        return u"The operation timed out. Please contact your system administrator."

    # According to the output from smbpasswd, decide what message should be shown 
    # in the log and on the web page. 
    if smbpasswd_output.endswith(u'NT_STATUS_LOGON_FAILURE'):
        msg_to_web = translate_message(u"change_password", u"1") 
        log_to_file("AUTH_FAIL: User: %s entered invalid USERNAME or PASSWORD." % username)
        smbpasswd_output_to_logfile(smbpasswd_output)

    # Not all configurations of samba provides this information.
    # "map to guest = bad user" is needed in /etc/samba/smb.conf to make this work.         
    elif smbpasswd_output.endswith(u'NT_STATUS_RPC_PROTOCOL_ERROR'):
        msg_to_web = translate_message(u"change_password", u"2")
        log_to_file(u"Error: User: %s: Incorrect USERNAME" % username)
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.endswith(u'NT_STATUS_UNSUCCESSFUL'):
        msg_to_web = translate_message(u"change_password", u"3")
        log_to_file(u"Error: Could not connect to the Samba server. " 
                    u"Server down or unreachable.")
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.endswith(u'NT_STATUS_INVALID_PARAMETER'):
        msg_to_web = translate_message(u"change_password", u"4")
        log_to_file(u"Error: Invalid parameter detected for smbpasswd.")
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.endswith(u'Error was : Password restriction'):
        msg_to_web = translate_message(u"change_password", u"5")
        log_to_file(u"Error: User: %s tried to change her/his password. But it did " 
                    u"not conform to the policy set by Samba" %  username)
        smbpasswd_output_to_logfile(smbpasswd_output)
    
    elif smbpasswd_output.startswith(u'Unable to find an IP address for'):
        msg_to_web = translate_message(u"change_password", u"6")
        log_to_file(u"ServerName_Error: Server name/address in gsmbpasswd.conf is invalid.")
        smbpasswd_output_to_logfile(smbpasswd_output)
     
    elif smbpasswd_output.startswith(u'Password changed for user'):
        msg_to_web = translate_message(u"change_password", u"7")
        log_to_file(u"SUCCESS: User: %s changed password successfully." % username)
        smbpasswd_output_to_logfile(smbpasswd_output)
        
    return msg_to_web
Ejemplo n.º 20
0
    def inspired_run(self, series, apk, examined, trigger_java_dir):
        self.trigger_java_dir = trigger_java_dir
        # apk = 'F:\\Apps\\COMMUNICATION\\com.mobanyware.apk'
        self.logger.info('base name: ' + os.path.basename(apk))
        apk_name, apk_extension = os.path.splitext(apk)

        self.logger.info(apk_name)
        if '_modified' not in apk_name:
            return
            # apk_modified = apk_name + '_modified.apk'
        else:
            apk_modified = apk
            apk_name = apk_name.replace('_modified', '')

        apk_name = os.path.basename(apk_name)

        if apk_name in examined:
            self.logger.error('Already examined ' + apk_name)
            return

        cmd = 'adb devices'
        os.system(cmd)
        self.logger.info(apk_modified)

        # current_time = time.strftime(ISOTIMEFORMAT, time.localtime())
        par_dir = os.path.basename(
            os.path.abspath(os.path.join(
                apk, os.pardir)))  # the parent folder of the apk

        package = self.get_package_name(self.aapt_loc, apk_modified)

        if not package:
            self.logger.error('Not a valid pkg.')
            return

        csvpath = self.get_csv_path(self.trigger_java_dir, par_dir, apk_name)
        if not os.path.isfile(csvpath):
            self.logger.error('tgt_Act.csv does not exist:' + csvpath)
            return

        output_dir = self.out_base_dir + par_dir + '/' + apk_name + '/'
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        filehandler = Utilities.set_file_log(
            self.logger, output_dir + 'COSMOS_TRIGGER_PY.log')
        self.logger.info('apk:' + apk_modified)
        self.logger.info('pkg:' + package)
        self.logger.info('csv: ' + csvpath)

        UIExerciser.uninstall_pkg(series, package)
        UIExerciser.install_apk(series, apk_modified)

        current_time = time.strftime(ISOTIMEFORMAT, time.localtime())
        UIExerciser.run_adb_cmd(
            'shell monkey -p com.lexa.fakegps --ignore-crashes 1')
        d = Device()
        d(text='Set location').click()

        UIExerciser.run_adb_cmd('logcat -c')
        self.logger.info(
            'clear logcat')  # self.screenshot(output_dir, activity)

        # UIExerciser.run_adb_cmd('shell "nohup /data/local/tcpdump -w /sdcard/' + package + current_time  + '.pcap &"')
        # UIExerciser.run_adb_cmd('shell "nohup logcat -v threadtime -s "UiDroid_Taint" > /sdcard/' + package + current_time +'.log &"')

        # cmd = 'adb -s ' + series + ' shell "nohup /data/local/tcpdump -w /sdcard/' + package + current_time + '.pcap &"'
        self.logger.info('tcpdump begins')
        cmd = 'adb -s ' + series + ' shell /data/local/tcpdump -w /sdcard/' + package + '_' + current_time + '.pcap'
        # os.system(cmd)
        print cmd
        process = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)

        UIExerciser.run_adb_cmd('shell monkey -p ' + package +
                                ' --ignore-crashes 1')
        for i in range(1, 3):
            if not UIExerciser.check_dev_online(UIExerciser.series):
                if UIExerciser.emu_proc:
                    UIExerciser.close_emulator(UIExerciser.emu_proc)
                    UIExerciser.emu_proc = UIExerciser.open_emu(
                        UIExerciser.emu_loc, UIExerciser.emu_name)
                else:
                    raise Exception('Cannot start the default Activity')
            if Utilities.run_method(self.screenshot,
                                    180,
                                    args=[output_dir, '', True, package]):
                break
            else:
                self.logger.warn(
                    "Time out while dumping XML for the default activity")

        # UIExerciser.adb_kill('logcat')
        # Utilities.adb_kill('tcpdump')
        # UIExerciser.run_adb_cmd('shell am force-stop fu.hao.uidroid')
        # os.system("TASKKILL /F /PID {pid} /T".format(pid=process.pid))
        time.sleep(10)
        process.kill()  # takes more time
        out_pcap = output_dir + package + '_' + current_time + '.pcap'
        try:
            while not os.path.exists(
                    out_pcap) or os.stat(out_pcap).st_size < 2:
                time.sleep(5)
                cmd = 'pull /sdcard/' + package + '_' + current_time + '.pcap ' + out_pcap
                UIExerciser.run_adb_cmd(cmd)
                process.kill()  # takes more time
        except:
            Utilities.logger.info('wait..')
            # if not os.path.exists(out_pcap):
            # raise Exception('The pcap does not exist.')
        # UIExerciser.run_adb_cmd('shell rm /sdcard/' + package + current_time + '.pcap')

        # UIExerciser.run_adb_cmd('pull /sdcard/' + package + current_time + '.log ' + output_dir)
        # UIExerciser.run_adb_cmd('shell rm /sdcard/' + package + current_time + '.log')
        taint_logs = []
        print 'f**k'
        Utilities.run_method(TaintDroidLogHandler.collect_taint_log,
                             15,
                             args=[taint_logs])
        with open(output_dir + package + '_' + current_time + '.json',
                  'w') as outfile:
            json.dump(taint_logs, outfile)

        self.start_activities(package, csvpath, output_dir)

        self.uninstall_pkg(series, package)

        filehandler.close()
        self.logger.removeHandler(filehandler)
        Utilities.kill_by_name('adb.exe')
Ejemplo n.º 21
0
    def flowintent_first_page(self, series, apk, examined):
        current_time = time.strftime(ISOTIMEFORMAT, time.localtime())
        self.logger.info('base name: ' + os.path.basename(apk))
        apk_name, apk_extension = os.path.splitext(apk)

        self.logger.info(apk_name)

        apk_name = os.path.basename(apk_name)

        if apk_name in examined:
            self.logger.error('Already examined ' + apk_name)
            return

        cmd = 'adb devices'
        os.system(cmd)
        self.logger.info(apk)

        # current_time = time.strftime(ISOTIMEFORMAT, time.localtime())
        par_dir = os.path.basename(
            os.path.abspath(os.path.join(
                apk, os.pardir)))  # the parent folder of the apk

        package = self.get_package_name(self.aapt_loc, apk)

        if not package:
            self.logger.error('Not a valid pkg.')
            return

        #self.start_taintdroid(series)

        output_dir = self.out_base_dir + par_dir + '/' + apk_name + '/'
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        filehandler = Utilities.set_file_log(
            self.logger, output_dir + 'UIExerciser_FlowIntent_FP_PY.log')
        self.logger.info('apk:' + apk)
        self.logger.info('pkg:' + package)

        UIExerciser.uninstall_pkg(series, package)
        UIExerciser.install_apk(series, apk)

        #self.run_adb_cmd('shell am start -n fu.hao.uidroid/.TaintDroidNotifyController')
        self.run_adb_cmd('shell "su 0 date -s `date +%Y%m%d.%H%M%S`"')
        UIExerciser.run_adb_cmd(
            'shell monkey -p com.lexa.fakegps --ignore-crashes 1')
        d = Device()
        d(text='Set location').click()

        UIExerciser.run_adb_cmd('logcat -c')
        self.logger.info(
            'clear logcat')  # self.screenshot(output_dir, activity)

        #UIExerciser.run_adb_cmd('shell "nohup /data/local/tcpdump -w /sdcard/' + package + current_time  + '.pcap &"')
        #UIExerciser.run_adb_cmd('shell "nohup logcat -v threadtime -s "UiDroid_Taint" > /sdcard/' + package + current_time +'.log &"')

        #cmd = 'adb -s ' + series + ' shell "nohup /data/local/tcpdump -w /sdcard/' + package + current_time + '.pcap &"'
        self.logger.info('tcpdump begins')
        cmd = 'adb -s ' + series + ' shell /data/local/tcpdump -w /sdcard/' + package + '_' + current_time + '.pcap'
        # os.system(cmd)
        print cmd
        process = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)

        UIExerciser.run_adb_cmd('shell monkey -p ' + package + '_' +
                                ' --ignore-crashes 1')
        for i in range(1, 3):
            if not UIExerciser.check_dev_online(UIExerciser.series):
                if UIExerciser.emu_proc:
                    UIExerciser.close_emulator(UIExerciser.emu_proc)
                    UIExerciser.emu_proc = UIExerciser.open_emu(
                        UIExerciser.emu_loc, UIExerciser.emu_name)
                else:
                    raise Exception('Cannot start the default Activity')
            if Utilities.run_method(self.screenshot,
                                    180,
                                    args=[output_dir, '', True, package]):
                break
            else:
                self.logger.warn(
                    "Time out while dumping XML for the default activity")

        #UIExerciser.adb_kill('logcat')
        #Utilities.adb_kill('tcpdump')
        #UIExerciser.run_adb_cmd('shell am force-stop fu.hao.uidroid')
        #os.system("TASKKILL /F /PID {pid} /T".format(pid=process.pid))
        time.sleep(60)
        process.kill()  # takes more time
        out_pcap = output_dir + package + current_time + '.pcap'
        while not os.path.exists(out_pcap) or os.stat(out_pcap).st_size < 2:
            time.sleep(5)
            cmd = 'pull /sdcard/' + package + '_' + current_time + '.pcap ' + out_pcap
            UIExerciser.run_adb_cmd(cmd)
            #if not os.path.exists(out_pcap):
            #raise Exception('The pcap does not exist.')
            UIExerciser.run_adb_cmd('shell rm /sdcard/' + package +
                                    current_time + '.pcap')

        #UIExerciser.run_adb_cmd('pull /sdcard/' + package + current_time + '.log ' + output_dir)
        #UIExerciser.run_adb_cmd('shell rm /sdcard/' + package + current_time + '.log')
        taint_logs = []
        Utilities.run_method(TaintDroidLogHandler.collect_taint_log,
                             15,
                             args=[taint_logs])
        with open(output_dir + package + '_' + current_time + '.json',
                  'w') as outfile:
            json.dump(taint_logs, outfile)

        self.uninstall_pkg(series, package)
        self.logger.info('End')

        filehandler.close()
        self.logger.removeHandler(filehandler)
        Utilities.kill_by_name('adb.exe')
Ejemplo n.º 22
0
def change_password(username, current_password, new_password):
    msg_to_web = None

    # Popen executes smbpasswd as a child program in a new process,
    # with arguments to the program smbpasswd in a list [].
    #
    # The -s option causes smbpasswd to read from stdin instead of prompting the user.
    # The -U [username] option allows a user to change his/her own password.
    #
    # stdin, stdout and stderr are assigned as PIPE so that we can
    # use communicate() to provide input to stdin of smbpasswd and
    # get message of both success and error back from the program.
    #
    # shell=False in order to avoid the security risk with shell
    # injection from unsanitized input such as "input; rm -rf /".
    smbpasswd_proc = Popen(
        [u"smbpasswd", u"-s", u"-r", SMB_SERVER, u"-U", username],
        stdout=PIPE,
        stdin=PIPE,
        stderr=STDOUT,
        shell=False)

    try:
        # Space, '.' and newline are inconsistently used in the output from smbpasswd
        # and therefore we strip those characters from the end of the output so that
        # we can do sane regex matching without fearing that one day someone will fix
        # this and break our application.
        smbpasswd_output = (smbpasswd_proc.communicate(
            input=(current_password + u'\n' + new_password + u'\n' +
                   new_password + u'\n').encode("UTF-8"),
            timeout=30)[0]).rstrip(u' .\n')
    except TimeoutExpired:
        smbpasswd_proc.kill()
        log_to_file(u"TIME_OUT: User: %s: subprocess.communicate timed out." %
                    username)
        smbpasswd_output_to_logfile(smbpasswd_output)
        return u"The operation timed out. Please contact your system administrator."

    # According to the output from smbpasswd, decide what message should be shown
    # in the log and on the web page.
    if smbpasswd_output.endswith(u'NT_STATUS_LOGON_FAILURE'):
        msg_to_web = translate_message(u"change_password", u"1")
        log_to_file(
            "AUTH_FAIL: User: %s entered invalid USERNAME or PASSWORD." %
            username)
        smbpasswd_output_to_logfile(smbpasswd_output)

    # Not all configurations of samba provides this information.
    # "map to guest = bad user" is needed in /etc/samba/smb.conf to make this work.
    elif smbpasswd_output.endswith(u'NT_STATUS_RPC_PROTOCOL_ERROR'):
        msg_to_web = translate_message(u"change_password", u"2")
        log_to_file(u"Error: User: %s: Incorrect USERNAME" % username)
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.endswith(u'NT_STATUS_UNSUCCESSFUL'):
        msg_to_web = translate_message(u"change_password", u"3")
        log_to_file(u"Error: Could not connect to the Samba server. "
                    u"Server down or unreachable.")
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.endswith(u'NT_STATUS_INVALID_PARAMETER'):
        msg_to_web = translate_message(u"change_password", u"4")
        log_to_file(u"Error: Invalid parameter detected for smbpasswd.")
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.endswith(u'Error was : Password restriction'):
        msg_to_web = translate_message(u"change_password", u"5")
        log_to_file(
            u"Error: User: %s tried to change her/his password. But it did "
            u"not conform to the policy set by Samba" % username)
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.startswith(u'Unable to find an IP address for'):
        msg_to_web = translate_message(u"change_password", u"6")
        log_to_file(
            u"ServerName_Error: Server name/address in gsmbpasswd.conf is invalid."
        )
        smbpasswd_output_to_logfile(smbpasswd_output)

    elif smbpasswd_output.startswith(u'Password changed for user'):
        msg_to_web = translate_message(u"change_password", u"7")
        log_to_file(u"SUCCESS: User: %s changed password successfully." %
                    username)
        smbpasswd_output_to_logfile(smbpasswd_output)

    return msg_to_web