Ejemplo n.º 1
0
def main(options):
    """
        Read the specified memory region and record statistics
    """

    # Initilize our experiment
    client = MemorySensorPhysical(options.target, 
                                      cache_timeout=0,
                                      use_threading=True)
    READ_SIZE = int(options.read_size)*85
    start_addr = options.startaddr   
        
    # Read memory
    offset = 0
   
    read_rates = []
    total_read = 0
    count = 0
    
    output = open(options.output_file, "w+")
    output.write("Memory Address,Bytes Read,Time Elapsed,Bytes/Second\n")

    for addr in xrange(start_addr,2**30, READ_SIZE):
        
        try:

            time_start = time.time()
            data = client.read(addr, READ_SIZE)
            time_elapsed = (time.time() - time_start)
            
            total_read += len(data)
            count += 1

            rate = (len(data)/time_elapsed)
            print "Addr: 0x%08X,  Read: %d bytes, %f bytes/sec" % (addr,
                                                                   len(data),
                                                                   rate)
            output.write("0x%08X,%d,%f,%f\n" % (addr, len(data), time_elapsed,
                                                rate))
            read_rates.append(rate)

        except:
            import traceback
            traceback.print_exc()
            # Just finish up
            break
        
    output.close()
Ejemplo n.º 2
0
def main(options):
    """
        Read the specified memory region and record statistics
    """

    # Initilize our experiment
    client = MemorySensorPhysical(options.target,
                                  cache_timeout=0,
                                  use_threading=True)
    READ_SIZE = int(options.read_size) * 85
    start_addr = options.startaddr

    # Read memory
    offset = 0

    read_rates = []
    total_read = 0
    count = 0

    output = open(options.output_file, "w+")
    output.write("Memory Address,Bytes Read,Time Elapsed,Bytes/Second\n")

    for addr in xrange(start_addr, 2**30, READ_SIZE):

        try:

            time_start = time.time()
            data = client.read(addr, READ_SIZE)
            time_elapsed = (time.time() - time_start)

            total_read += len(data)
            count += 1

            rate = (len(data) / time_elapsed)
            print "Addr: 0x%08X,  Read: %d bytes, %f bytes/sec" % (
                addr, len(data), rate)
            output.write("0x%08X,%d,%f,%f\n" %
                         (addr, len(data), time_elapsed, rate))
            read_rates.append(rate)

        except:
            import traceback
            traceback.print_exc()
            # Just finish up
            break

    output.close()
Ejemplo n.º 3
0
    def __init__(self, base, config, layered=False, **kwargs):
        addrspace.BaseAddressSpace.__init__(self, base, config, **kwargs)
        self.as_assert(base == None or layered, 'Must be first address space')

        if config.LOPHI_CACHE:
            logger.info("LO-PHI Cache ENABLED.")
            cache_timeout = 1
        else:
            cache_timeout = 0

        if config.LOCATION.startswith("lophi://"):
            location = config.LOCATION[8:]
            self.client = MemorySensorPhysical(location,
                                               cache_timeout=cache_timeout)
        elif config.LOCATION.startswith("vmi://"):
            location = config.LOCATION[6:]
            self.client = MemorySensorVirtual(location,
                                              cache_timeout=cache_timeout)
            self.dtb = self.get_cr3()
        else:
            raise (
                "Not a valid LO-PHI URN. (lophi:// for physical and vmi:// for virtual)"
            )

        self.fname = location
        self.name = location
        self.cache = False
        self.cache_data = {}
        self.address = 0

        if config.RAM_SIZE is None:
            print "ERROR/LO-PHI: No RAM size defined. (e.g. --ram_size=12GB)"
            sys.exit(0)

        self.size = self.parse_byte_amount(config.RAM_SIZE)

        self.config = config

        self._exclusions = sorted(
            [])  # no info about the HW, nothing to exclude
Ejemplo n.º 4
0
def import_from_config(config_file, config_type=None):
    """
        This will import our config file into a LoPhiConfig or Machine class 
        for each item in the config
        
        This is only done for physical machines.  Virtual machines are handled
        by libvirt.
        
        @param config_file: Config file on disk
        @param config_type: Type of config file to parse
                        controller, machine, sensor
        @return: List of classes of the appropriate type in dictionary 
                referenced by their name
    """
    if config_type is None:
        logger.error("ERROR: Must specify type of config to import.")
        return None

    Config = ConfigParser.ConfigParser()
    Config.read(config_file)

    config_list = {}
    for config in Config.sections():
        name = config

        logging.debug("Intializing config for %s..." % name)

        config = None

        if config_type == "machine":
            # Create our config
            config = MachineConfig(name, Config)

            # What type of machine?
            config_list[name] = PhysicalMachine(config)

        elif config_type == "controller":
            config = ControllerConfig(name, Config)
            config_list[name] = config

        elif config_type == "sensor":
            # These will only be physical sensors
            # Virtual sensors are all derived from the VM name

            sensor_type = int(Config.get(name, "type"))
            if sensor_type == G.SENSOR_TYPES.NETWORK:
                interface = Config.get(name, "interface")
            else:
                sensor_ip = Config.get(name, "ip")
                sensor_port = int(Config.get(name, "port"))

            if sensor_type == G.SENSOR_TYPES.CONTROL:
                config_list[name] = ControlSensorPhysical(sensor_ip,
                                                          sensor_port,
                                                          name=name)
            elif sensor_type == G.SENSOR_TYPES.DISK:
                config_list[name] = DiskSensorPhysical(sensor_ip,
                                                       sensor_port,
                                                       name=name)
            elif sensor_type == G.SENSOR_TYPES.MEMORY:
                config_list[name] = MemorySensorPhysical(sensor_ip,
                                                         sensor_port,
                                                         name=name)
            elif sensor_type == G.SENSOR_TYPES.CPU:
                config_list[name] = CPUSensorPhysical(sensor_ip,
                                                      sensor_port,
                                                      name=name)
            elif sensor_type == G.SENSOR_TYPES.NETWORK:
                config_list[name] = NetworkSensorPhysical(interface, name=name)
            else:
                logging.error("Unrecognized sensor type. (%d)" % sensor_type)

        elif config_type == "images":

            # Setup an empty map
            config_list = {
                G.MACHINE_TYPES.PHYSICAL: {},
                G.MACHINE_TYPES.KVM: {},
                G.MACHINE_TYPES.XEN: {}
            }

            if Config.has_section("physical"):
                for (profile, image) in Config.items("physical"):
                    config_list[G.MACHINE_TYPES.PHYSICAL][
                        profile.lower()] = image

            if Config.has_section("virtual"):
                for (profile, image) in Config.items("virtual"):
                    config_list[G.MACHINE_TYPES.KVM][profile.lower()] = image
                    config_list[G.MACHINE_TYPES.XEN][profile.lower()] = image

        else:
            logging.error("Unknown config file. (%s)" % (config_type))
            return None

        logging.debug(config_list)

    return config_list
Ejemplo n.º 5
0
def main(options):

    if options.replay_file is not None:
        cap_reader = CaptureReader(options.replay_file)

        for (ts, data) in cap_reader:
            print "Time: ", ts, "s"
            print MemorySensorPacket(data)

        return

    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        client = MemorySensorPhysical(options.target,
                                      cache_timeout=0,
                                      use_threading=False)
    else:
        client = MemorySensorVirtual(options.target)

    READ_SIZE = int(options.read_size)
    start_addr = options.startaddr

    # Create our output file
    try:
        os.makedirs(os.path.dirname(options.output))
    except:
        pass

    try:
        mcap_writer = None
        if options.loop_forever == True:
            logger.debug("Creating capture file.")
            options.output += ".mcap"
            mcap_writer = CaptureWriter(options.output)
            mcap_writer.start()
        else:
            logger.debug("Creating dump file.")
            options.output += ".mfd"
            output_file = open(options.output, "w+")
    except:
        print "ERROR: Could not open output file."
        sys.exit(0)

    # Read memory
    count = 0
    start = time.time()
    sensor_packet = MemorySensorPacket()

    while True:

        try:
            # Get memory from remote system

            # Read memory
            data = client.read(start_addr, READ_SIZE)

            # Write to file?
            if not options.loop_forever:
                output_file.write(data)
            else:
                sensor_packet.address = start_addr
                sensor_packet.data = data
                sensor_packet.length = READ_SIZE
                mcap_writer.put(sensor_packet)

            # Just read once?
            if not options.loop_forever:
                break
            else:
                print "Completed read #%d" % count
                count += 1
        except:
            # Just finish up
            break
    end = time.time()

    # Do we have an mcap file to close?
    if mcap_writer is not None:
        mcap_writer.stop()
    else:
        # Close output file
        output_file.close()

    print "Memory dump (%d bytes) written to %s. Took %s seconds." % (
        len(data), options.output, end - start)
Ejemplo n.º 6
0
def main(options):    

    if options.replay_file is not None:
        cap_reader = CaptureReader(options.replay_file)
         
        for (ts, data) in cap_reader:
            print "Time: ", ts, "s"
            print MemorySensorPacket(data)
            
        return

    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        client = MemorySensorPhysical(options.target, 
                                      cache_timeout=0,
                                      use_threading=False)
    else:
        client = MemorySensorVirtual(options.target)

    READ_SIZE = int(options.read_size)
    start_addr = options.startaddr   
        
    # Create our output file
    try:
        os.makedirs(os.path.dirname(options.output))
    except:
        pass

    try:
        mcap_writer = None
        if options.loop_forever == True:
            logger.debug("Creating capture file.")
            options.output += ".mcap"
            mcap_writer = CaptureWriter(options.output)
            mcap_writer.start()
        else:
            logger.debug("Creating dump file.")
            options.output += ".mfd"
            output_file = open(options.output, "w+")
    except:
        print "ERROR: Could not open output file."
        sys.exit(0)

    # Read memory
    count = 0
    start = time.time()
    sensor_packet = MemorySensorPacket()
   
    while True:
        
        try:
            # Get memory from remote system
               
            # Read memory
            data = client.read(start_addr, READ_SIZE)
     
            # Write to file?
            if not options.loop_forever:
                output_file.write(data)
            else:
                sensor_packet.address = start_addr
                sensor_packet.data = data
                sensor_packet.length = READ_SIZE
                mcap_writer.put(sensor_packet)
    
            # Just read once?
            if not options.loop_forever:
                break
            else:
                print "Completed read #%d"%count
                count += 1
        except:
            # Just finish up
            break
    end = time.time()
    
    # Do we have an mcap file to close?
    if mcap_writer is not None:
        mcap_writer.stop()
    else:
        # Close output file
        output_file.close()

    print "Memory dump (%d bytes) written to %s. Took %s seconds." % (len(data),options.output,end-start)