There have already been good answers explaining that your strategy is neither illegal nor profitable, but no one has explained what forces the stock price to drop ex-dividend.
Dividend arbitrage. Arbitrage means (more or less) riskless profit.
Here's an unrealistic, simplified version:
Let's say there's a stock selling for $100 today. Let's say it will pay a $1 dividend in one month. And let's pretend you can enter into a forward contract to buy or sell a share on the ex-dividend date for $100. Then you should buy as many shares today as you can and enter into a forward contract to sell them ex-dividend. Then you'll buy at 100, sell at 100, and collect the dividend. Riskless profit! Many other people know this, and so they will do the same thing. This arbitrage will cause the (spot) price of the stock to rise and the forward price to fall until (not surprisingly) the difference between the two is $1.
Another simple arbitrage will cause the forward stock price on the ex-dividend date to equal the actual stock price on the ex-dividend date.
Thus, the actual stock price will drop by $1 when the dividend is paid.
This only works if your stock has a forward contract available that matures on the stock's ex-dividend date. In reality, that might not be true, but there are typically stock options of varying expirations. And it turns out that they can be used for similar purposes, although it might not be obvious at first.
As a practical matter, the size of a dividend is typically smaller than the daily volatility of the stock, so it's hard to see the drop in all the noise. But when a stock pays a large special dividend like Microsoft did in 2004, the drop can be quite noticeable.
If the technical details of this dividend arbitrage aren't clear to you, just rest assured there are plenty of people out there who know a lot more about this than you and who have a lot of resources. It's useful to keep in mind the saying: "There's a patsy in every game. If you look around and don't know who it is, it's you."