Sick of paying high prices for prescription drugs? Buying drugstore stocks may be the best revenge—and most profitable.

Big drugstore chains now make most of their money from prescription drugs or pharmacy services. In the next few years, these sales are expected to spike even higher, driven by costlier generic and specialty drugs. Spending on specialty medications increased 30.9 percent in 2014, the highest specialty drug trend ever recorded, according to Express-Scripts annual drug price trend study. It's expected to reach 44 percent of total drug spend by 2017.

And as baby boomers age, they'll be spending more money on drugs, too. About 40 percent of adults over 65 take five different prescriptions each week.

Blend Images | Getty Images

After marijuana and alcohol, prescription drugs are the most commonly misused substances by Americans age 14 and older.

"Drugs are one of the cheapest ways to treat most illnesses," said Evercore ISI analyst Ross Muken, adding, "They're very defensive businesses."

Securing a bigger chunk of the pharmacy market is the name of the game today. "Those customers are worth three to four times what a front-end customer is worth," said Raymond James analyst John Ransom.

Case in point: Rhode Island-based CVS Health reported that 98.9 percent of prescriptions in the most recent reported quarter were third-party coverage. "It's kind of recession-proof and has been trending up," said Scott Mushkin, analyst at Wolfe Research. Mushkin said that when he first started covering the company, percentage was in the high 80s—now it is almost always near 100 percent. "The economy does not matter to CVS," he said.

Note: Rite Aid, not in the S&P consumer staples sector, is up 13.5 percent year-to-date.

CVS gets nearly all its revenue from its drug sales and services, including a pharmacy benefits manager. CVS has transformed into the biggest prescription-drug dispenser, along with running health-care clinics.

One trump card is its specialty pharmacy, where revenue grew 28 percent in the second quarter. Muken expects specialty revenue to continue to grow in relative significance over the next several years versus other revenue components, since CVS makes more money from specialty classes of drugs.

CVS is consolidating the drug revenue streams as well. Earlier this year it bought Omnicare, which manages drugs for senior-living facilities, and Target's retail pharmacies. The upshot: CVS will soon operate one in seven retail pharmacies, according to Pembroke Consulting.

"CVS has a long-term structure that Warren Buffett would look for," Ransom said. "It's one U.S. health care stock to own."

When CVS opted to stop selling tobacco this year, it wasn't a surprise to those who have been watching the shift in its business mix. Tobacco contributed only slightly to its $140 billion in 2014 revenues, and the very public move only amplified its brand as a health care company.

"CVS has enormous cash flow, is buying back shares, and has made good acquisitions," Mushkin said.

Top 10 specialty drugs

Drug

% of Total Specialty Spend

Year over Year Spend Increase

Humira

11.9

27.8

Enbrel

8.2

11.5

Sovaldi

7.8

10,001.90

Copaxone

5

-0.1

Tecfidera

3.4

152.1

Avonex

2.6

-7.9

Atripla

2.4

2.6

Gleevec

2.2

12.3

Revlimid

2.2

11.7

Olysio

2.1

15,423.60

(Source: ExpressScripts, 2014 annual drug trends report)

Walgreens Boots Alliance also relies on its strong pharmacy sales. The company was created through a merger with the European pharmacy retailer Alliance Boots last year. Stefano Pessina, the European CEO who took over after the deal, is seen as one of the best retailers in Europe, said Morningstar analyst Vishnu Lekraj. "So there's a lot of momentum there," he said. It has shown in the stock price this year: While the consumer staples sector is down 3.8 percent amid market volatility, Walgreens shares remain up more than 18 percent and is one of the best-performing stocks in the consumer staples sector.

But it's more of an M&A and cost-cutting play with a weaker overall business model than CVS, Mushkin said. Activist investors, including prominent hedge funds like Jana Partners, had targeted the company before the merger and, according to press reports, mandated that Pessina be named CEO or else they wouldn't approve the deal. Jana Partners has seats on the Walgreens board, and founder Barry Rosenstein did tell CNBC in April that Pessina was one of the greatest entrepreneurs of all time and a major reason to invest a billion dollars in the company.

Walgreens, now the largest retail pharmacy drugstore chain, is seeing pharmacy sales growing in the mid-single digits. But Walgreens also has a few additional revenue lines it is ramping: It wants to focus on selling more on higher-end beauty products to pump up U.S. sales. And now, with a cross-border structure, it could become a major player in the global drug channel as well, Halper said.

To little fanfare, Rite Aid is using the same blueprint. The company, which teetered on the edge of bankruptcy just a few years ago, bought its way into the pharmacy benefits manager market this year for $2 billion when it acquired Envision Pharmaceutical Services. It's also remodeling its stores to include a wellness format.

"The chain wants to be like CVS, only a regional player," Muken said, who has a buy rating on the stock. "The aim." he said, "is to become a community hub." Rite Aid is also recovering after many years of underperforming," he explained.

Ransom said Rite Aid's management has done a good job cutting costs and fixing the balance sheet. The stock market has noticed: Its shares are up 30 percent in the past year, more than either Walgreens Boots or CVS. Still, the company has ground to make up, with much lower growth in same store prescription volume than CVS and Walgreens. "The company fell behind its peers," he said.

Bitter pills, by the numbers

The three most expensive specialty drug classes in 2014 were for inflammatory conditions, multiple sclerosis and oncology.

The class of drugs used to treat hepatitis C was not even among the top 10 specialty classes in 2013 (two of the three drugs were only on the market for one month in 2013). However, by 2014 the three top hepatitis C medications—Sovaldi, Olysio and Harvoni—made up 11.8 percent of total specialty spend.

The average inflammatory condition prescription was $2,913.33.

The average hepatitis C prescription was $16,373.40.

The unit cost for all top 10 specialty drugs increased by at least 6 percent in 2014.

The specialty drug spend trend line is expected to slow, but still increase between 21 percent and 23 percent annually in the 2015-2017 period.