Legality does not factor into this equation: a pirate who kills lots of ships with higher rank will increase their rank appropriately. What makes them a pirate is their ability to steal and sell cargo from other vessels.
Your Pilot's Federation Combat Rank increases based on rank disparity, so kills in anarchies count. This is separate from reputation with factions (which is also logged in anarchies) and crimes (which are not logged in anarchies).