Spaces:
Paused
Paused
File size: 17,879 Bytes
f8ee2b7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | /*
* HF Token Acquirer - C++ with LLM Vision (Ollama LLaVA) + OCR (Tesseract)
*
* Uses Playwright via subprocess for browser automation,
* LLaVA for visual understanding of login pages,
* Tesseract for OCR text extraction from screenshots.
*
* Build: g++ -std=c++17 -o hf_token hf_token_acquirer.cpp -lcurl -ljsoncpp
* OR: g++ -std=c++17 -o hf_token hf_token_acquirer.cpp $(pkg-config --libs --cflags curl)
*
* If jsoncpp not available, uses nlohmann/json header-only.
*/
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <filesystem>
#include <thread>
#include <chrono>
#include <regex>
#include <curl/curl.h>
namespace fs = std::filesystem;
// ─── Utilities ──────────────────────────────────────────────────────────────
std::string exec_cmd(const std::string& cmd) {
std::array<char, 4096> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) return "";
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
result += buffer.data();
return result;
}
bool file_exists(const std::string& path) {
return fs::exists(path);
}
// ─── Curl HTTP POST (for Ollama API) ────────────────────────────────────────
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string ollama_vision(const std::string& image_path, const std::string& prompt) {
// Read image as base64
std::ifstream img(image_path, std::ios::binary);
if (!img.is_open()) return "";
std::vector<char> buf((std::istreambuf_iterator<char>(img)), {});
img.close();
// Base64 encode
static const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string b64str;
int val = 0, valb = -6;
for (char c : buf) {
val = (val << 8) + (unsigned char)c;
valb += 8;
while (valb >= 0) {
b64str.push_back(b64[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) b64str.push_back(b64[((val << 8) >> (valb + 8)) & 0x3F]);
while (b64str.size() % 4) b64str.push_back('=');
// Build JSON payload
std::string json = R"({"model":"llava:latest","prompt":")" + prompt +
R"(","images":[")" + b64str + R"("],"stream":false})";
CURL* curl = curl_easy_init();
if (!curl) return "";
std::string response;
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:11434/api/generate");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
if (res != CURLE_OK) return "";
// Extract "response" field from JSON (simple parse)
std::regex resp_re("\"response\"\\s*:\\s*\"(.*?)\"");
std::smatch match;
if (std::regex_search(response, match, resp_re))
return match[1].str();
return response;
}
// ─── Tesseract OCR ──────────────────────────────────────────────────────────
std::string ocr_image(const std::string& image_path) {
std::string cmd = "tesseract " + image_path + " stdout 2>/dev/null";
return exec_cmd(cmd);
}
// ─── Playwright Python Script Generator ─────────────────────────────────────
void write_playwright_script(const std::string& script_path, const std::string& action) {
std::ofstream f(script_path);
if (action == "screenshot") {
f << R"(
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False, channel="msedge")
context = await browser.new_context(viewport={"width": 1280, "height": 800})
page = await context.new_page()
await page.goto("https://huggingface.co/login", wait_until="networkidle")
await page.wait_for_timeout(2000)
await page.screenshot(path="/tmp/hf_cpp_01_login.png")
# Fill username
try:
await page.fill('input[name="username"]', "josephrw")
await page.wait_for_timeout(300)
except:
pass
await page.screenshot(path="/tmp/hf_cpp_02_filled.png")
await browser.close()
asyncio.run(main())
)";
} else if (action == "login_attempt") {
f << R"(
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False, channel="msedge")
context = await browser.new_context(viewport={"width": 1280, "height": 800})
page = await context.new_page()
await page.goto("https://huggingface.co/login", wait_until="networkidle")
await page.wait_for_timeout(2000)
# Fill login form
try:
await page.fill('input[name="username"]', "josephrw")
await page.wait_for_timeout(300)
await page.fill('input[name="password"]', password)
await page.wait_for_timeout(300)
await page.click('button[type="submit"]')
await page.wait_for_timeout(4000)
await page.screenshot(path="/tmp/hf_cpp_03_after_login.png")
print(f"URL:{page.url}")
except Exception as e:
print(f"ERROR:{e}")
await page.screenshot(path="/tmp/hf_cpp_03_error.png")
await browser.close()
asyncio.run(main())
)";
} else if (action == "create_token") {
f << R"(
import asyncio, re
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False, channel="msedge")
context = await browser.new_context(viewport={"width": 1280, "height": 800})
page = await context.new_page()
# Navigate to tokens
await page.goto("https://huggingface.co/settings/tokens", wait_until="networkidle")
await page.wait_for_timeout(3000)
await page.screenshot(path="/tmp/hf_cpp_04_tokens.png")
# Click create new token
btns = page.locator('button')
for i in range(await btns.count()):
txt = await btns.nth(i).inner_text()
if any(kw in txt.lower() for kw in ['new token', 'create', 'generate']):
await btns.nth(i).click()
break
await page.wait_for_timeout(2000)
await page.screenshot(path="/tmp/hf_cpp_05_form.png")
# Fill name
name = page.locator('input[type="text"]')
if await name.count() > 0:
await name.first.fill("email-crawler-deploy")
# Submit
submit = page.locator('button:has-text("Create"), button:has-text("Generate"), button[type="submit"]')
if await submit.count() > 0:
await submit.first.click()
await page.wait_for_timeout(3000)
await page.screenshot(path="/tmp/hf_cpp_06_created.png")
# Extract token
body = await page.inner_text('body')
tokens = re.findall(r'hf_[A-Za-z0-9]{20,}', body)
if tokens:
print(f"TOKEN:{tokens[0]}")
else:
inputs = page.locator('input')
for i in range(await inputs.count()):
val = await inputs.nth(i).get_attribute('value')
if val and val.startswith('hf_'):
print(f"TOKEN:{val}")
break
await browser.close()
asyncio.run(main())
)";
}
f.close();
}
// ─── Main ───────────────────────────────────────────────────────────────────
int main() {
curl_global_init(CURL_GLOBAL_DEFAULT);
std::cout << "═══════════════════════════════════════════\n";
std::cout << " HF Token Acquirer - C++ LLM + OCR\n";
std::cout << " Vision: Ollama LLaVA | OCR: Tesseract\n";
std::cout << "═══════════════════════════════════════════\n\n";
// Step 1: Take screenshot of HF login page
std::cout << "[1/6] Capturing HF login page...\n";
std::string script = "/tmp/hf_cpp_pw.py";
write_playwright_script(script, "screenshot");
std::string out = exec_cmd("python3 " + script + " 2>&1");
std::this_thread::sleep_for(std::chrono::seconds(3));
std::string screenshot = "/tmp/hf_cpp_01_login.png";
if (!file_exists(screenshot)) {
std::cout << " ❌ Screenshot failed\n";
return 1;
}
std::cout << " ✅ Screenshot saved\n\n";
// Step 2: OCR the login page
std::cout << "[2/6] Running Tesseract OCR on login page...\n";
std::string ocr_text = ocr_image(screenshot);
std::cout << " OCR output:\n" << ocr_text.substr(0, 500) << "\n\n";
// Step 3: LLaVA vision analysis - understand the login form
std::cout << "[3/6] LLaVA analyzing login page...\n";
std::string vision = ollama_vision(screenshot,
"This is the Hugging Face login page. Describe what form fields are visible, "
"what buttons are present, and any error messages. Is there a username field, "
"password field, or any captcha/WAF challenge?");
std::cout << " LLaVA: " << vision.substr(0, 500) << "\n\n";
// Step 4: Analyze filled form
std::string filled = "/tmp/hf_cpp_02_filled.png";
if (file_exists(filled)) {
std::cout << "[4/6] LLaVA analyzing filled form...\n";
std::string vision2 = ollama_vision(filled,
"This is the Hugging Face login form with username filled in. "
"Is the username field visible and filled? What other fields are present? "
"Is there a password field? Any captcha or WAF challenge?");
std::cout << " LLaVA: " << vision2.substr(0, 500) << "\n\n";
std::string ocr2 = ocr_image(filled);
std::cout << " OCR: " << ocr2.substr(0, 300) << "\n\n";
}
// Step 5: Try login with each password
std::vector<std::string> passwords = {"Carpathian.Forest369", "Lola369."};
for (int i = 0; i < passwords.size(); i++) {
std::cout << "[5/6] Attempting login with password " << (i+1) << "...\n";
// Write Python script with password
std::string pw_script = "/tmp/hf_cpp_login.py";
std::ofstream f(pw_script);
f << "import asyncio\nfrom playwright.async_api import async_playwright\n\n";
f << "password = \"" << passwords[i] << "\"\n\n";
f << R"(async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False, channel="msedge")
context = await browser.new_context(viewport={"width": 1280, "height": 800})
page = await context.new_page()
await page.goto("https://huggingface.co/login", wait_until="networkidle")
await page.wait_for_timeout(2000)
try:
await page.fill('input[name="username"]', "josephrw")
await page.wait_for_timeout(300)
await page.fill('input[name="password"]', password)
await page.wait_for_timeout(300)
await page.click('button[type="submit"]')
await page.wait_for_timeout(4000)
await page.screenshot(path="/tmp/hf_cpp_03_after_login.png")
print(f"URL:{page.url}")
except Exception as e:
print(f"ERROR:{e}")
await page.screenshot(path="/tmp/hf_cpp_03_error.png")
await browser.close()
asyncio.run(main())
)";
f.close();
std::string result = exec_cmd("python3 " + pw_script + " 2>&1");
std::cout << " Result: " << result.substr(0, 200) << "\n";
// Check if login succeeded
if (result.find("URL:https://huggingface.co/login") == std::string::npos &&
result.find("ERROR") == std::string::npos) {
std::cout << " ✅ Login appears successful!\n\n";
// LLaVA verify
std::string after = "/tmp/hf_cpp_03_after_login.png";
if (file_exists(after)) {
std::cout << " LLaVA verifying login success...\n";
std::string v = ollama_vision(after,
"Is this page showing a successful login to Hugging Face? "
"Can you see a user dashboard, profile, or settings page? "
"Or is it still showing a login form or error?");
std::cout << " LLaVA: " << v.substr(0, 300) << "\n\n";
}
// Step 6: Create token
std::cout << "[6/6] Creating access token...\n";
write_playwright_script(script, "create_token");
std::string token_result = exec_cmd("python3 " + script + " 2>&1");
std::cout << " Token result: " << token_result.substr(0, 200) << "\n";
// Extract token
std::regex token_re("TOKEN:(hf_[A-Za-z0-9]+)");
std::smatch match;
if (std::regex_search(token_result, match, token_re)) {
std::string token = match[1].str();
std::cout << "\n ✅ TOKEN ACQUIRED: " << token.substr(0, 15) << "...\n";
// Save to file
std::ofstream tf("/tmp/hf_new_token.txt");
tf << token;
tf.close();
// Save to memory
std::cout << "\n Token saved to /tmp/hf_new_token.txt\n";
curl_global_cleanup();
return 0;
}
// LLaVA analyze token page
std::string token_page = "/tmp/hf_cpp_04_tokens.png";
if (file_exists(token_page)) {
std::cout << " LLaVA analyzing tokens page...\n";
std::string v = ollama_vision(token_page,
"This is the Hugging Face tokens settings page. "
"Is there a 'Create new token' or 'New token' button? "
"Are there existing tokens listed? Describe what you see.");
std::cout << " LLaVA: " << v.substr(0, 400) << "\n";
}
std::string form_page = "/tmp/hf_cpp_05_form.png";
if (file_exists(form_page)) {
std::cout << " LLaVA analyzing token creation form...\n";
std::string v = ollama_vision(form_page,
"This is a token creation form. Is there a name field, "
"permission selector, and create button? What do you see?");
std::cout << " LLaVA: " << v.substr(0, 400) << "\n";
}
std::string created_page = "/tmp/hf_cpp_06_created.png";
if (file_exists(created_page)) {
std::cout << " LLaVA analyzing token result page...\n";
std::string v = ollama_vision(created_page,
"Has a token been created? Can you see a token string starting with 'hf_'? "
"Describe exactly what text is visible on this page.");
std::cout << " LLaVA: " << v.substr(0, 400) << "\n";
std::string ocr_created = ocr_image(created_page);
std::cout << " OCR: " << ocr_created.substr(0, 500) << "\n";
// Try to find token in OCR
std::regex hf_re("hf_[A-Za-z0-9]{20,}");
if (std::regex_search(ocr_created, match, hf_re)) {
std::string token = match[0].str();
std::cout << "\n ✅ TOKEN FROM OCR: " << token.substr(0, 15) << "...\n";
std::ofstream tf("/tmp/hf_new_token.txt");
tf << token;
tf.close();
curl_global_cleanup();
return 0;
}
}
break;
} else {
std::cout << " ❌ Login failed\n";
// LLaVA analyze failure
std::string after = "/tmp/hf_cpp_03_after_login.png";
if (file_exists(after)) {
std::string v = ollama_vision(after,
"Why did this Hugging Face login fail? Is there an error message? "
"What does the error say? Is it wrong password, captcha, or something else?");
std::cout << " LLaVA: " << v.substr(0, 400) << "\n\n";
}
}
}
std::cout << "\n❌ Could not acquire HF token\n";
curl_global_cleanup();
return 1;
}
|