Use block.dd. You will be extracting the first partition entry from the master b
ID: 3794279 • Letter: U
Question
Use block.dd. You will be extracting the first partition entry from the master boot record that is contained in the file. The first partition entry starts at offset 0x1BE and ends at 0x1CD. Pull that chunk of bytes out of the file provided and send it to the server software that you will write. The server will listen for the chunk of data and print out the status of the drive, the partition type and the starting address of the partition as an integer.
You can refer to Wikipedia (https://en.wikipedia.org/wiki/Master_boot_record) for additional details about offsets
within the master boot record and partition table for the pieces of information you are looking for.
My first program to build upon:
Explanation / Answer
#!/usr/bin/python # This is client.py file
# import the struct module which converts binary data to and from Python data structures.
import struct
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
# open the master boot record file called block.dd and save into an array
f = open("block.dd", "rb")
mbr = bytearray()
# this opens the array and closes the file when done
try:
mbr = f.read(2048)
s.send(mbr)
finally:
f.close()
# this loads the content of the first partition entry at address 1BE (hex)
# looks for the status type and checks to see if it is active or not
status = mbr[0x1BE]
if status == 0x80:
print("Status: Active")
s.send("Status: Active")
else:
print("Status: Not active")
s.send("Status: Not active")
# this looks at the partition type (1 byte located at the address 1BE + 4) and prints it out
ptype= mbr[0x1BE+4]
print("Partition type: ", ptype)
s.send(ptype)
# this looks at the address of the first sector in the partition (1BE + 8) and prints it out
addr = struct.unpack("<i", mbr[0x1BE+8:0x1BE+12])
print("Address of the first sector in the partition: ", addr[0])
s.send(int(addr[0],0))
s.close() # Close the socket when done
#server.py
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
while(1):
print (c.recv(2048))
c.close() # Close the connection
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.