Here are the relevant sections of the Laws of The Game:
Law 5 - The Referee, Section 3 - Powers and Duties:
- punishes the more serious offence, in terms of sanction, restart, physical severity and tactical impact, when more than one offence occurs at the same time
Law 12 - Fouls and Misconduct, Section 1 - Direct Free Kick:
A direct free kick is awarded if a player commits any of the following offences against an opponent in a manner considered by the referee to be careless, reckless or using excessive force:
- kicks or attempts to kick
- tackles or challenges
- trips or attempts to trip
Careless is when a player shows a lack of attention or consideration when making a challenge or acts without precaution.
Law 12 - Fouls and Misconduct, Section 2 - Indirect Free Kick*:
A player may shield the ball by taking a position between an opponent and the ball if the ball is within playing distance and the opponent is not held off with the arms or body.
* - This explanation is included under Indirect Free Kick as it is most commonly considered when judging whether the indirect free kick offence of impeding the progress of an opponent without any contact being made has occurred, but is applicable to all offences under Law 12.
To summarise the situation you described, the defender has placed their foot between the ball and an attacker while the attacker is winding up a shot, resulting in the attacker kicking the defender moments later.
If the defender is genuinely attempting to play or shield the ball, the attacker has (at least) carelessly kicked an opponent, and this should result in a direct free kick to the defending team.
If the defender places their foot in a position to simply trip up the attacker or present a hard surface (e.g. the studs) for the attacker to kick, the defender has (at least) carelessly tripped, tackled or challenged an opponent, and this should result in a direct free kick to the attacking team.
Alternatively, if both players make contact with each other's feet from a similar position, while moving their feet towards the ball, this generally would not be considered careless and play would just continue. However, this doesn't seem to align with the scenario you've provided.
Only you as the referee can decide which category this falls into. If you somehow come to the conclusion that both players have fouled, the free kick should be awarded to the team of the player who was culpable for the most physical severity, as quoted above.
To address some of the specific points in your question:
If it all happens in a split second, you can hardly blame the attacker (even though the defender could get hurt)
You certainly can blame the attacker. As quoted, players must show attention or consideration when there is a possibility of a challenge occurring. This clip1 shows a player being sent off for what would have been an innocuous attempt to play the ball had an opponent not been around. However, due to not taking into account that an opponent could beat them to the ball, Nani committed a foul (and was sent-off due to its severity).
Neither am I sure that you can blame the defender. If he places his foot close enough to the ball, then I would be inclined to regard it as legitimate shielding
This is probably correct, provided the defender has done so without carelessly tripping or careless challenging (e.g. with studs raised towards) the attacker.
I need someone to blame in order for there to exist no legal situation in which players can get hurt
This is a misconception. Players are injured all the time from perfectly legal challenges. Players should be punished based on the amount of risk (i.e. careless, reckless or excessive force) to an opponent when making a challenge, not whether an opponent is injured as a result of the challenge.
Finally, as for whether the offence requires a caution, sending-off, or just a free kick - that is covered in this question.
1 - While this decision divided fans, most referees were of the opinion that the challenged endangered the safety of an opponent and the send-off for serious foul play was justified.