A magnitude 6.4 earthquake rattled much of Pakistan and parts of neighboring Afghanistan on Thursday, sending panicked residents fleeing from homes and offices and frightening people in remote villages, Pakistani officials and the U.S. Geological Survey said.