Sometimes wars are a punishment for sin. The Old Testament explicitly links various invasions of Israel to the nation’s sin, especially the sin of idolatry (Jgs 2:14-15, 5:8, 2 Kgs 15:37, 1 Chr 5:26). This principle is applied to Gentile nations as well. The Pentateuch (the first five books of the Bible; Torah to the Jews) teaches that the Canaanite people, who lived in the Promised Land before Israel, were going to be judged by God for their sins. This was why God let the Israelites conquer the Canaanites (Lv 18:24-28).
Sometimes wars may not be punishments for sin. Luke 13:1-5 establishes a general principle for evaluating the cause of disaster. Christ tells us that simply because a given group of people was stricken with disaster, this does not mean they were worse sinners than those who were spared. Christ teaches that all of us must repent or we too shall perish (Lk 13:5).
Nor does this mean that there are no differences between one man’s sins and another’s. We are all sinners (1 Jn 1:8), but we are not equally grievous sinners. Some sins are worse than others (1 Jn 5:16-17), and some people deserve more punishment than others (Lk 12:47-48).