According to a forum post on WordReference, the original form may have been:
Dar donde se rompe el saco.
Theory number one
The "rip in the bag" would correspond to one's buttcrack.
Here's another source claiming the same origin for it:
En su origen era "que te den por donde se rompe el saco" (el "saco" era el calzón que se utilizaba antiguamente, en lugar de pantalones).
This one says that a "saco" was a type of pants.
And RAE does cite something similar as the meanings 4-6 of "saco":
- m. Vestidura tosca y áspera de paño burdo o sayal.
- m. Especie de gabán grande y, en general, vestidura holgada que no se ajusta al cuerpo.
- m. Vestido corto que usaban los antiguos romanos, excepto los varones consulares, en tiempo de guerra.
So it has had a meaning as different clothes.
Theory number two
It seems that the equivalent expression in German is
Fick dich ins Knie!
Multiple sources (eg. this) would quote 'Knie' here not to mean 'knee' but be a shortened version of 'Kniefiez', meaning an avaricious person. If the expression was borrowed, it could have been translated as saco as per the expression 'La avaricia rompe el saco.' That would explain the rather weird use of 'por donde' as combining the German 'ins' matching 'por' in this context and 'donde' from the Spanish expression.
The timeframes would make some sense with the German expression being born in the early 1940s in its long form and the first occurrence of the Spanish one in the CREA in the 1990s. Google NGram quotes one case in the year 1920 (statistical noise?) and a steady uptake from the 1960s onwards.
This is to be taken with a large pinch of salt: forum posts can't be called a credible source, and Spanish is not my first, second, third, fourth or even fifth language.