That depends on where you live. In many places, the law doesn't punish victims for self-defense. You certainly can kill a person who is attacking you. In a few places, notably Texas, it is legal to kill a person to stop a theft or to recover stolen property, as long as there is no non-lethal way to do so.